gpt4 book ai didi

android - 为什么 `setPositiveButton` 总是返回 -1 以及如何获取选定的索引?

转载 作者:行者123 更新时间:2023-12-04 14:26:00 25 4
gpt4 key购买 nike

我有一个对话框来显示在启动时切换应用程序自动启动的设置。

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 能够获取索引,而 setPositiveButtonwhich 变量总是打印 -1, logcat 例如:

05-12 03:14:41.983  5014  5014 D hole    : single clicked0
05-12 03:14:43.066 5014 5014 D hole : single clicked1
05-12 03:14:43.615 5014 5014 D hole : positive clicked:-1

最佳答案

将单选按钮的点击位置保存在 Int 变量中。

然后在您的 setPositiveButton 中单击检查字符串数组的值。

int index;   //declare Globally

builder.setSingleChoiceItems(items, default_i ,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
index = item; //save radio button click position
}
});
builder.setPositiveButton(positiveText,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
if (items[index] == "Enable") {
Toast.makeText(YourActivity.this, "Enable clicked.",Toast.LENGTH_SHORT).show();
}
if (items[index] == "Disable") {
Toast.makeText(YourActivity.this, "Disable clicked.",Toast.LENGTH_SHORT).show();
}
}
});

关于android - 为什么 `setPositiveButton` 总是返回 -1 以及如何获取选定的索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43924021/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com