2012-04-09 220 views
0

我的代碼中有一個警告對話框,我想爲該對話框中的取消按鈕按下一個變量賦值。警報對話框

我實際的代碼 -

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[] { "[email protected]" }); 
         s.putExtra(android.content.Intent.EXTRA_SUBJECT, 
           "feedback from app"); 
         startActivity(s); 
         break; 

        } 

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

回答

0
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(); 
+1

我沒有通過使用alert.setOnCancelListener(新DialogInterface.OnCancelListener(){ \t \t \t \t \t \t公共無效onCancel(DialogInterface對話框){ \t \t \t \t // TODO自動生成方法存根 \t \t \t \t點擊= 0; \t \t \t \t \t \t \t} \t \t}); – 2012-04-09 07:16:41

+0

它確定任何工作。我認爲給這種類型將有助於其他有關dialoge處理。 – Vin99999 2012-04-11 08:30:17

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"/> 
0

此代碼是什麼你需要。只需將其插入需要啓動警報對話框的任何位置即可。 我還沒有想出如何自動啓動鍵盤,但它不應該是困難的。

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();