警报对话框(Alert Dialog Box)

2019-06-23 11:39发布

我在我的代码中的警告对话框,我想作为取消按钮被按下该对话框的值赋给一个变量。

我实际的代码 -

private void clicked() {
    AlertDialog.Builder builder = new AlertDialog.Builder(Feedback.this);
    builder.setTitle("FEEDBACK");
    builder.setSingleChoiceItems(options, -1,
            new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    switch (which) {
                    case 0:
                        String url = "http://killmathsapp.blogspot.in/";
                        Intent i = new Intent(Intent.ACTION_VIEW);
                        i.setData(Uri.parse(url));
                        startActivity(i);
                        break;

                    case 1:
                        Intent s = new Intent(Intent.ACTION_SEND);
                        s.setType("plain/text");
                        s.putExtra(android.content.Intent.EXTRA_EMAIL,
                                new String[] { "pankaj_88_88@yahoo.com" });
                        s.putExtra(android.content.Intent.EXTRA_SUBJECT,
                                "feedback from app");
                        startActivity(s);
                        break;

                    }

                }
            });
    AlertDialog alert = builder.create();
    alert.setCancelable(true);
    alert.show();
}

Answer 1:

在对话框警报消息非常简单的方法...

       public void onClick(View v) 
        {
            if(somthing_wrong)
                          {
           Dialog d=new Dialog(AmitregistrationActivity.this);
           d.setContentView(R.layout.detail);
           d.setTitle("Warning....");
           d.show();
                  }
                else{
                  //Code that you want to execute 
                       }
             }

还必须创建这样一个detail.xml文件

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:src="@drawable/detail" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="26dp"
    android:layout_marginTop="15dp"
    android:layout_toRightOf="@+id/imageView1"
    android:text="Please fill all Details..." android:textSize="20dp"/>


Answer 2:

AlertDialog.Builder alert=new AlertDialog.Builder(this);
        alert.setTitle("FeedBack");
        alert.setNegativeButton("No",
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog,
                            int which) {
                        // TODO Auto-generated method stub
                       dialog.dismiss();
                    //do Set variable here or any other code    
                    }
                });
        alert.setPositiveButton("Yes",
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog,
                            int which) {
                    //do Set variable here or any other code    
                    }
                }).show();


Answer 3:

此代码是你所需要的。 只是无论你需要启动的警告对话框中插入。 我还没有想出如何自动启动键盘,但它不应该是困难的。

AlertDialog.Builder alert = new AlertDialog.Builder(this);
                alert.setTitle(multiLangTranslation(R.string.manualshippermessage));
                final EditText input = new EditText(this);
                input.setInputType(InputType.TYPE_CLASS_NUMBER);
                input.setRawInputType(Configuration.KEYBOARD_12KEY);
                alert.setView(input);  
                alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                  //Put actions for OK button here
                  }
                });
                alert.setNegativeButton(multiLangTranslation(R.string.cancel), new DialogInterface.OnClickListener() {
                  public void onClick(DialogInterface dialog, int whichButton) {
                      //Put actions for CANCEL button here, or leave in blank
                  }
                });
                alert.show();


文章来源: Alert Dialog Box