Android 覆盖AlertDialog里的按钮事件并显示Toast

 

网上找到的多数是:

控制其不消失,和消失。

//                	/** 假设对话框已经关闭,欺骗系统,以保持输入窗口**/
                	try {
                		Field field = this.getClass().getSuperclass().getDeclaredField( "mShowing" );
                		field.setAccessible( true );
                		field.set(this, bSucceed);
                	} catch (Exception e){
                		e.printStackTrace();
                	}

 

还可以

OnClicklistener覆盖DialogInterface.OnClicklistener

//可以使用AlerDialog.Builder,并显示一个Toast。//只要你覆盖按钮的OnClickListener,就可以触发Toast,显示在对话框上。//并决定对话框是否消失 public void showToastOnDialog(final Context context) { 
     AlertDialog.Builder builder = new AlertDialog.Builder(context); 
     builder.setTitle("Dialog title"); 
     builder.setMessage("Dialog message"); 
     builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       // Do nothing, you will be overriding this anyway 
      } 
     }); 
     builder.setNegativeButton(android.R.string.cancel, 
       new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       // You can implement code here, because you wont be 
       // overriding this 
      } 
     }); 
     final AlertDialog dialog = builder.create(); 
     // Make sure you show the dialog first before overriding the 
     // OnClickListener 
     dialog.show(); 
     // Notice that I`m not using DialogInterface.OnClicklistener but the 
     // View.OnClickListener 
     dialog.getButton(Dialog.BUTTON_POSITIVE).setOnClickListener( 
       new View.OnClickListener() { 

        @Override 
        public void onClick(View v) { 

         Toast toast = Toast.makeText(context, 
           "I`m a toast on top of a dialog.", 
           Toast.LENGTH_LONG); 
         toast.show(); 
         // Because you are overriding the OnClicklistener, the 
         // dialog will not auto dismiss after clicking 
        ////otherwise //dialog.dismiss(); 
         dialog.dismiss(); 
        } 
       }); 
    } 

 测试图片


http://stackoverflow.com/questions/2356149/how-to-raise-a-toast-on-top-of-a-alertdialog

你可能感兴趣的:(android,AlerDialog)