如何将警告对话框设置为if和else语句

时间:2014-12-01 14:35:15

标签: android android-alertdialog

我有2个EditText来获得String,点击Button后,它会将此char的{​​{1}}转换为String 。然后,它会对int的所有int求和,当chars的总和为int时,(totals&1 == 0)显示“这是奇数”。在Dialog后,第二个else,显示“即使是”。我需要设置Dialog来显示。

AlertDialog

3 个答案:

答案 0 :(得分:2)

AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage("that is odd");
builder.setCancelable(true);
AlertDialog alert = builder.create();
alert.show();

只写#34;即使是#34;而不是"这是奇怪的"对于第二个对话框。

答案 1 :(得分:0)

为此目的使用Toast,对于单个消息来说,它们更容易和快速。

    Toast.makeText(this, "Your Message here", 
   Toast.LENGTH_LONG).show();

答案 2 :(得分:0)

我发现我像这样使用AlertDialog:

 btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                AlertDialog.Builder builder = new AlertDialog.Builder(StartActivity.this);
                builder.setMessage("that is odd");
                builder.setCancelable(true);
                AlertDialog alert = builder.create();

                String dataF = ed1.getText().toString();
                char[] charArray = dataF.toCharArray();
                int totalF = 0;
                for (char ch: charArray) {
                    totalF += characterMap.get(ch);
                }

                String dataM = ed2.getText().toString();
                char[] charArr = dataM.toCharArray();
                int totalM = 0;
                for (char ch2: charArr) {
                    totalM += characterMap.get(ch2);
                }

                int sum = totalF + totalM;
                int totals = sum % 5;

                if ((totals & 1) == 0)

                    alert.show();

                else {

                    AlertDialog.Builder builder1 = new AlertDialog.Builder(StartActivity.this);
                    builder1.setMessage("that is even");
                    builder1.setCancelable(true);
                    AlertDialog alert1 = builder1.create();
                    alert1.show();

                }

            }
        });