简体   繁体   English

单击按钮后打开对话框

[英]Open Dialog after clicking on Button

I have implemented a WhatsNew Dialog to my Android app. 我已经在Android应用中实现了WhatsNew对话框。 But it only shows when a user installed and open a new version for the first time. 但是它仅在用户首次安装并打开新版本时显示。

I want to create a button that when its clicked opens the same WhatsNew Dialog again. 我想创建一个按钮,单击该按钮时将再次打开相同的WhatsNew对话框。 How can I do so? 我该怎么办? I already created the button, here is the code of the WhatNewScreen Activity: 我已经创建了按钮,这是WhatNewScreen活动的代码:

public class WhatsNewScreen {
    private static final String LOG_TAG                 = "WhatsNewScreen";

    private static final String LAST_VERSION_CODE_KEY   = "last_version_code";

    private Activity            mActivity;

    // Constructor memorize the calling Activity ("context")
    public WhatsNewScreen(Activity context) {
        mActivity = context;
    }

    // Show the dialog only if not already shown for this version of the application
    public void show() {
        try {
            // Get the versionCode of the Package, which must be different (incremented) in each release on the market in the AndroidManifest.xml
            final PackageInfo packageInfo = mActivity.getPackageManager().getPackageInfo(mActivity.getPackageName(), PackageManager.GET_ACTIVITIES);

            final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mActivity);
            final long lastVersionCode = prefs.getLong(LAST_VERSION_CODE_KEY, 0);

            if (packageInfo.versionCode != lastVersionCode) {
                Log.i(LOG_TAG, "versionCode " + packageInfo.versionCode + "is different from the last known version " + lastVersionCode);

                final String title = mActivity.getString(R.string.app_name) + " v" + packageInfo.versionName;

                final String message = mActivity.getString(R.string.whatsnew);

                // Show the News since last version
                AlertDialog.Builder builder = new AlertDialog.Builder(mActivity)
                        .setTitle(title)
                        .setMessage(message)
                        .setPositiveButton(android.R.string.ok, new Dialog.OnClickListener() {

                            public void onClick(DialogInterface dialogInterface, int i) {
                                // Mark this version as read
                                SharedPreferences.Editor editor = prefs.edit();
                                editor.putLong(LAST_VERSION_CODE_KEY, packageInfo.versionCode);
                                editor.commit();
                                dialogInterface.dismiss();
                            }
                        });
                builder.create().show();
            } else {
                Log.i(LOG_TAG, "versionCode " + packageInfo.versionCode + "is already known");
            }

        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }

}

I tried it with this in my MainAcitivty to open the Dialog. 我在MainAcitivty中对此进行了尝试,以打开对话框。 Please take a look on the Picture: 请看图片:

http://i.stack.imgur.com/8nav1.png http://i.stack.imgur.com/8nav1.png

But when I open the App and click the button it force closes. 但是,当我打开应用程序并单击按钮时,它会强制关闭。

Diolag can be shown if version code of your application changes(as I see from the code you've shared). 如果您的应用程序的版本代码发生更改(可以从共享代码中看到),则可以显示Diolag。 So it is going to shown just once. 因此,它将仅显示一次。 If you want to show it anytime with a button click, then you may change the show method a little bit. 如果您想随时通过单击按钮显示它,则可以稍微更改show方法。 See below: 见下文:

    public void show(boolean showWithVersionCheck) {
        try {
            // Get the versionCode of the Package, which must be different (incremented) in each release on the market in the AndroidManifest.xml
            final PackageInfo packageInfo = mActivity.getPackageManager().getPackageInfo(mActivity.getPackageName(), PackageManager.GET_ACTIVITIES);

            final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mActivity);

            final long lastVersionCode = prefs.getLong(LAST_VERSION_CODE_KEY, 0);

            boolean showDialog = true;
            if(showWithVersionCheck) {
                if(packageInfo.versionCode == lastVersionCode) {
                   showDialog = false; 
                }
            }

            if (showDialog) {
                Log.i(LOG_TAG, "versionCode " + packageInfo.versionCode + "is different from the last known version " + lastVersionCode);

                final String title = mActivity.getString(R.string.app_name) + " v" + packageInfo.versionName;

                final String message = mActivity.getString(R.string.whatsnew);

                // Show the News since last version
                AlertDialog.Builder builder = new AlertDialog.Builder(mActivity)
                        .setTitle(title)
                        .setMessage(message)
                        .setPositiveButton(android.R.string.ok, new Dialog.OnClickListener() {

                            public void onClick(DialogInterface dialogInterface, int i) {
                                // Mark this version as read
                                SharedPreferences.Editor editor = prefs.edit();
                                editor.putLong(LAST_VERSION_CODE_KEY, packageInfo.versionCode);
                                editor.commit();
                                dialogInterface.dismiss();
                            }
                        });
                builder.create().show();
            } else {
                Log.i(LOG_TAG, "versionCode " + packageInfo.versionCode + "is already known");
            }

        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }

}

And call it: 并称之为:

Button button = getYourButton();
button.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        new WhatsNewScreen(someActivityContext).show(false);
        // or show(true) if you want a version check 
        // If you are calling it from your MainActivity you can write MainActivity.this
        // instead of someActivityContext. 
    }
});

Edit: WhatsNewScreen does not extend from Activity, you can't start it with the code at the image you've shared. 编辑: WhatsNewScreen不会从Activity扩展,您不能使用共享图像上的代码来启动它。

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

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