gpt4 book ai didi

java - 带有单选按钮的对话框不适用于 onClick()

转载 作者:行者123 更新时间:2023-11-30 02:28:13 27 4
gpt4 key购买 nike

我的菜单里有一个项目

case R.id.theme:
ShowRadioDialog();
return true;

使用显示带有 3 个单选按钮的警报对话框的方法。当我单击对话框显示的项目时,但是当我在对话框中选择某个项目并点击正按钮时,没有任何反应。这是方法:

public void ShowRadioDialog() {
final CharSequence[] items={"Rosso","Verde","Blu"};
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setTitle("Seleziona un colore");
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

@SuppressLint("NewApi")
@Override
public void onClick(DialogInterface dialog, int which) {

if (wich== 1) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
getWindow().setStatusBarColor(MainActivity.this.getResources().getColor(R.color.red));
toolbar.setBackgroundColor(MainActivity.this.getResources().getColor(R.color.red));
Toast.makeText(MainActivity.this, "Rosso OK", Toast.LENGTH_SHORT).show();
Log.i("Colors", "Rosso Ok");
}
} else if (wich ==2) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
getWindow().setStatusBarColor(MainActivity.this.getResources().getColor(R.color.green_welcome));
}
} else if (wich == 3){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
getWindow().setStatusBarColor(MainActivity.this.getResources().getColor(R.color.primary_dark_blue));
}
}
}
});

builder.setSingleChoiceItems(items,-1, new DialogInterface.OnClickListener() {

@SuppressLint("NewApi")
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub

if ("Rosso".equals(items[which])) {
which = 1;
} else if ("Verde".equals(items[which])) {

which = 2;
} else if ("Blu".equals(items[which])) {

which = 3;
}

}
});
builder.show();
}

我不确定这是正确的方法。但是,两者都没有出现(登录 logcat 和应用程序中的 Toast)。似乎正面按钮不接受选择。有什么问题吗?

最佳答案

在顶层定义一个变量并使用它来访问所选项目。

int index = -1;

public void ShowRadioDialog() {
final CharSequence[] items={"Rosso","Verde","Blu"};
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setTitle("Seleziona un colore");
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

@SuppressLint("NewApi")
@Override
public void onClick(DialogInterface dialog, int which) {

if (index == 1) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
getWindow().setStatusBarColor(MainActivity.this.getResources().getColor(R.color.red));
toolbar.setBackgroundColor(MainActivity.this.getResources().getColor(R.color.red));
Toast.makeText(MainActivity.this, "Rosso OK", Toast.LENGTH_SHORT).show();
Log.i("Colors", "Rosso Ok");
}
} else if (index ==2) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
getWindow().setStatusBarColor(MainActivity.this.getResources().getColor(R.color.green_welcome));
}
} else if (index == 3){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
getWindow().setStatusBarColor(MainActivity.this.getResources().getColor(R.color.primary_dark_blue));
}
}
}
});

builder.setSingleChoiceItems(items,-1, new DialogInterface.OnClickListener() {

@SuppressLint("NewApi")
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub

if ("Rosso".equals(items[which])) {
index = 1;
} else if ("Verde".equals(items[which])) {

index = 2;
} else if ("Blu".equals(items[which])) {

index = 3;
}

}
});
builder.show();
}

如果你想保存 index 的值在点击 ok 按钮的共享首选项中,这样做

存储在 SharedPreferences 中

SharedPreferences preferences = getSharedPreferences("myPref", getApplicationContext().MODE_PRIVATE);
Editor editor = preferences.edit();
editor.putInt("choice", index);
editor.commit();

需要时从 SharedPreferences 获取

SharedPreferences preferences = getSharedPreferences("myPref", getApplicationContext().MODE_PRIVATE);
int index = preferences.getInt("choice",-1);

关于java - 带有单选按钮的对话框不适用于 onClick(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27639727/

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