首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android应用开发中的条件弹出消息

Android应用开发中的条件弹出消息
EN

Stack Overflow用户
提问于 2020-09-10 03:11:37
回答 2查看 40关注 0票数 0

首先,我对Android Studio和Android应用程序开发非常陌生。我想在我的应用程序中显示一个有条件的弹出窗口,只有当条件不满足时弹出窗口才会出现。

代码语言:javascript
复制
    Double billAmount = new Double(etBillAmount.getText().toString());
    Double TipPercentage = new Double(etTipPercentage.getText().toString());

    // The popup should be given when the Tip percentage is greater than 100 or else continue with 
       the code
    if (TipPercentage > 100)
    {
        System.out.println("Please enter Tip Percentage between 0 to 100. Thank you");
        System.exit(0);
    }
    Double totalBill;
    Double totalTip;

    totalTip = TipPercentage*billAmount/100;
    totalBill = totalTip + billAmount;

    tvTotal.setText("$ " + totalTip.toString());   
    tvTip.setText("$ " + totalBill.toString());

这是代码,当if语句中的条件不满足时,我想弹出一条消息。请帮助我解决它只在线性布局格式,如果可能的话。

请帮助我提前感谢大家:)!

EN

回答 2

Stack Overflow用户

发布于 2020-09-10 03:28:27

对于弹出消息,您可以使用Toast

代码语言:javascript
复制
Toast.makeText(this, "Please enter Tip Percentage between 0 to 100. Thank you", Toast.LENGTH_SHORT).show();

因此,您需要在if代码块中添加toast,而不是system.out.println

代码语言:javascript
复制
if (TipPercentage > 100){
Toast.makeText(this, "Please enter Tip Percentage between 0 to 100. Thank you", Toast.LENGTH_SHORT).show();
}

或者,您也可以使用警报对话框Too.For警报对话框点击此链接https://developer.android.com/guide/topics/ui/dialogs

票数 0
EN

Stack Overflow用户

发布于 2020-09-10 14:47:18

代码语言:javascript
复制
the code
if (TipPercentage > 100){
System.out.println("Please enter Tip Percentage between 0 to 100. Thank you");
System.exit(0);}else{
dialogOpenTop(NotificationDialogActivity.this,"pass string");
}}
public void dialogOpenTop(final Context context, String toke) {
final Dialog dialog = new Dialog(NotificationDialogActivity.this);           dialog.setContentView(R.layout.alert);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            dialog.setCancelable(false);
            dialog.setTitle("Demo for bottom dialog");
            LinearLayout someLayout = dialog.findViewById(R.id.someLayout);
            Button btnOk = dialog.findViewById(R.id.btnOk);
}
layout
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/scrollViewLuogo"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:descendantFocusability="beforeDescendants"
    android:focusableInTouchMode="true"
    android:fitsSystemWindows="false"
    >
    <LinearLayout
        android:focusableInTouchMode="true"
        android:padding="@dimen/margin_10dp"
        android:id="@+id/someLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="@drawable/dialog_background"
        android:orientation="vertical">
        <TextView
            android:padding="@dimen/margin_10dp"
            android:textStyle="bold"
            android:id="@+id/title_message"
            android:layout_gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Your message"

            android:layout_marginBottom="4dp"
            android:textSize="18sp" />

        <ScrollView android:id="@+id/scroll_view"
            android:layout_width="match_parent"
            android:fitsSystemWindows="true"
            android:fadeScrollbars="false"
            style="@style/scroll_style"
            android:layout_height="match_parent"
            tools:ignore="UselessParent">

            <LinearLayout android:id="@+id/scroll_layout"
                android:padding="@dimen/margin_10dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" >




                <ImageView
                    android:layout_below="@+id/title_message"
                    android:adjustViewBounds="true"
                    android:id="@+id/btnAlert"
                    android:layout_gravity="center"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:visibility="visible" />


                <TextView

                    android:paddingTop="@dimen/margin_10dp"
                    android:paddingBottom="@dimen/margin_10dp"
                    android:layout_below="@+id/btnAlert"
                    android:layout_gravity="center"
                    android:id="@+id/text"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="YOur message"
                    android:textSize="18sp" />

                <Button
                    android:id="@+id/btnOk"
                    android:minWidth="@dimen/margin_0dp"
                    android:minHeight="@dimen/margin_0dp"
                    android:textSize="@dimen/text_20sp"
                    android:paddingTop="2dp"
                    android:paddingBottom="2dp"
                    android:paddingLeft="@dimen/margin_height_50dp"
                    android:paddingRight="@dimen/margin_height_50dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
           android:layout_gravity="center"
                    android:layout_marginTop="2dp"
                    android:background="@drawable/dialog_button"
                    android:focusable="true"
                    android:focusedByDefault="true"
                    android:text="Ok" />

                <Button
                    android:visibility="gone"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Next actvity"
                    android:id="@+id/nxt"/>
            </LinearLayout>
        </ScrollView>
    </LinearLayout>
</LinearLayout>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63818111

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档