如何在Android内置的alertdialog中设置onclick

如何在Android内置的alertdialog中设置onclick,android,disabled-control,Android,Disabled Control,我正在开发一个消息应用程序。我的应用程序可以是默认的,也可以不是。我可以通过以下应用程序使我的应用程序成为默认应用程序 Intent intent = new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT); intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME,myPackageName); startActivity(intent); 执行此代码时,我的应用程序中会出现以下警

我正在开发一个消息应用程序。我的应用程序可以是默认的,也可以不是。我可以通过以下应用程序使我的应用程序成为默认应用程序

Intent intent = new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT);
intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME,myPackageName);
startActivity(intent);
执行此代码时,我的应用程序中会出现以下警报对话框。警报对话框是内置的。我不为它编写代码。对于上述代码,将显示警报对话框。


当我点击Yes按钮时,我需要调用enable_view()函数。当我单击NO按钮时,我需要调用disable_view()函数。我该怎么做

您必须实现onActivityResult方法才能从sms默认意图中获得结果。。比如这个例子

@Override
protected void onActivityResult(int reqcode, int rescode, Intent arg2) {
    // TODO Auto-generated method stub
    super.onActivityResult(reqcode, rescode, arg2);
    switch (reqcode) {
        case IS_APP_DEFAULT_ID:
            if (rescode == Activity.RESULT_OK) {
                 //call enable view here
            } else {
                  //call disabel view here
            }
    }

}

您必须使用startActivityForResult而不是startActivity。

根据我的理解,您需要一个带有yes和no选项的alertbox,其中包含自定义操作

 AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
      alertDialogBuilder.setMessage("Use super duper message app instead of default messaging app");

      alertDialogBuilder.setPositiveButton("yes", new DialogInterface.OnClickListener() {
         @Override
         public void onClick(DialogInterface arg0, int arg1) {
          //positive button action  goes here
         }
      });

      alertDialogBuilder.setNegativeButton("No",new DialogInterface.OnClickListener() {
         @Override
         public void onClick(DialogInterface dialog, int which) {
           //negative button action goes here
         }
      });

      AlertDialog alertDialog = alertDialogBuilder.create();
      alertDialog.show();
   }
干杯

请检查这个答案