为什么`setPositiveButton`总是返回-1以及如何获得所选索引?

时间:2017-05-11 19:25:24

标签: android android-alertdialog android-dialog

我有一个对话框,显示在启动时切换App自动启动的设置。

enter image description here

用户点击EnableDisable,然后点击OK进行保存。

代码:

public void showShareDialog(final int itId, String title) {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle(title);

    String positiveText = getString(android.R.string.ok);
    String negativeText = getString(android.R.string.cancel);
    String[] items;
    int default_i = 0;
    final SharedPreferences sharedPref = getSharedPreferences("quickSettings", MODE_PRIVATE);
    if (itId == AUTOSTART_LI) {
        items = getResources().getStringArray(R.array.toggle);
        final boolean autostart = sharedPref.getBoolean("autostart", false);
        Log.d("hole", "#autostart get:" + autostart);
        if (!autostart) default_i = 1;

    } else return;

    builder.setSingleChoiceItems(items, default_i, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Log.d("hole", "single clicked" + which);

                }
            })
            .setPositiveButton(positiveText, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Log.d("hole", "positive clicked:" + which);

                    if (itId == AUTOSTART_LI) {

                        int flag;
                        boolean autostart;
                        if (which == 0) {
                            flag = PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
                            autostart = true;
                        } else {
                            flag = PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
                            autostart = false;
                        }
                        ComponentName component = new ComponentName("com.blogspot.diannaoxiaobai.skyshot"
                                , "com.blogspot.diannaoxiaobai.skyshot.MyReceiver");
                        getPackageManager().setComponentEnabledSetting(component, flag, PackageManager.DONT_KILL_APP);

                        //save only if above no crash
                        SharedPreferences.Editor editor = sharedPref.edit();
                        Log.d("hole", "#autostart saved:" + autostart);
                        editor.putBoolean("autostart", autostart);
                        editor.commit();

                    }
                }
            })
            .setNegativeButton(negativeText, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Log.d("hole", "negative clicked:" + which);
                }
            });

    AlertDialog dialog = builder.create();
    dialog.show();
}

但只有setSingleChoiceItems能够获取索引,而setPositiveButton的{​​{1}}变量始终打印which,logcat例如:

-1

0 个答案:

没有答案