简体   繁体   English

Android-如果已经单击按钮,则停止显示AlertDialog

[英]Android - stop showing AlertDialog if button already clicked

I need to stop showing the AlertDialog (that is in OnCreate) if the user has alredy pressed that button. 如果用户确实按下了该按钮,则需要停止显示AlertDialog(位于OnCreate中)。 I need to disable the AlertDialog if positive button has already been clicked. 如果已经单击肯定按钮,则需要禁用AlertDialog。

AlertDialog.Builder dlgAlert  = new AlertDialog.Builder(this);
    dlgAlert.setMessage(getString(R.string.dialogMSG));
    dlgAlert.setTitle("App Support Checker");
    dlgAlert.setPositiveButton(R.string.yesdev,
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {

                }
            });

dlgAlert.setCancelable(false);
    dlgAlert.create().show();

You need to point it to somewhere else, I mean some other activity. 您需要将其指向其他地方,我的意思是其他活动。 Otherwise, you can use setNegativeButton instead of setPositiveButton. 否则,可以使用setNegativeButton代替setPositiveButton。

dlgAlert.setNegativeButton(R.string.yesdev,
        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {

            }
        });

Cheers, 干杯,

Do you mean that on pressing positive button, yu need to remove dialog? 您是说在按下肯定按钮时需要删除对话框吗? By the way you can call dismiss() api for dialog to remove the dialog. 顺便说一下,您可以为对话框调用dismiss()api来删除对话框。

To obtain shared preferences, use the following method In your activity: 要获取共享的首选项,请在您的活动中使用以下方法:

SharedPreferences prefs = this.getSharedPreferences( "com.example.app", Context.MODE_PRIVATE); 

To read preferences: 要阅读首选项:

String dateTimeKey = "com.example.app.datetime"; 
long l = prefs.getLong(dateTimeKey, new Date().getTime()); 

To edit and save preferences 编辑和保存首选项

Date dt = getSomeDate(); 
prefs.edit().putLong(dateTimeKey, dt.getTime()).commit();

The android sdk's sample directory example of retrieving and stroing shared preferences. android sdk的示例目录示例,用于检索和存储共享首选项。 It's located in the: /samples/android-/ApiDemo‌​s directory 它位于/ samples / android- / ApiDemo‌目录中

To store values in shared preferences: 要将值存储在共享首选项中:

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); 
SharedPreferences.Editor editor = preferences.edit();
editor.putString("Name","Sushil"); 
editor.commit(); 

To retrieve values from shared preferences: 要从共享的首选项中检索值:

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); 
String name = preferences.getString("Name",""); 
if(!name.equalsIgnoreCase("")) { 
   name = name+" Jha"; /* Edit the value here*/ 
} 

This is the correct and best way to handle the scenario you are talking about. 这是处理您正在讨论的方案的正确和最佳方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM