如何添加AlertDialog并将其放置在按钮上?

问题描述 投票:-3回答:1

我依赖要做的是在我的android应用程序中添加一个AlertDialog,我希望在单击它时弹出消息。

我尝试过的:

  AlertDialog alertDialog = new AlertDialog(this); //Also this place shows error
        alertDialog.setMessage("Message Sample");
        alertDialog.setTitle("Info");
        alertDialog.setView(null); // I want to believe this is where I should add my view ?
android android-activity
1个回答
0
投票

这里是如何在您的应用程序中添加警报对话框

SharedPreferences mPrefs;
final String welcomeScreenShownPref = "welcomeScreenShown";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mPrefs = PreferenceManager.getDefaultSharedPreferences(this);

    // second argument is the default to use if the preference can't be found
    Boolean welcomeScreenShown = mPrefs.getBoolean(welcomeScreenShownPref, false);

    if (!welcomeScreenShown) {
        // here you can launch another activity if you like
        // the code below will display a popup

        String whatsNewTitle = getResources().getString(R.string.whatsNewTitle);
        String whatsNewText = getResources().getString(R.string.whatsNewText);
        new AlertDialog.Builder(this).setIcon(android.R.drawable.ic_dialog_alert).setTitle(whatsNewTitle).setMessage(whatsNewText).setPositiveButton(
                R.string.ok, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).show();
        SharedPreferences.Editor editor = mPrefs.edit();
        editor.putBoolean(welcomeScreenShownPref, true);
        editor.commit(); // Very important to save the preference
    }

}
© www.soinside.com 2019 - 2023. All rights reserved.