Java 如何从Android菜单中捕获选定值无线电值
我已经创建了一个单选按钮菜单列表Java 如何从Android菜单中捕获选定值无线电值,java,android,Java,Android,我已经创建了一个单选按钮菜单列表 final CharSequence[] items = { "3 sec", "5 sec", "7 sec" }; AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("Change Wallpaper Every.."); builder.setSingleChoiceItems(items, -1,
final CharSequence[] items = { "3 sec", "5 sec", "7 sec" };
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Change Wallpaper Every..");
builder.setSingleChoiceItems(items, -1,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(), items[item],
Toast.LENGTH_SHORT).show();
}
});
AlertDialog alert = builder.create();
builder.show();
我想做的是从上面的字符序列中捕获所选的值,并将其放入一个变量 所选项目是索引为
项目的项目,对应的字符串是项目[项目]
捕捉选择的典型模式如下所示:
public class SomeDialog {
final CharSequence[] items = { "3 sec", "5 sec", "7 sec" };
int choice = -1;
//...
public void someMethod() {
//...
builder.setSingleChoiceItems(items, -1,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(), items[item],
Toast.LENGTH_SHORT).show();
choice = item; // <-- now we store the selection to a class member
}
});
// ...
}
public String getSelection() {
return items[choice]; // <-- this allows another class to read the current selection
// often called after the dialog has been closed
}
}
公共类对话框{
最终字符序列[]项={“3秒”、“5秒”、“7秒”};
int-choice=-1;
//...
公共方法(){
//...
建造商设置单选择项(项目-1,
新建DialogInterface.OnClickListener(){
公共void onClick(对话框接口对话框,int项){
Toast.makeText(getApplicationContext(),items[item],
吐司。长度(短)。show();
choice=item;//所选项目是索引为item
的项目,对应的字符串是items[item]
捕捉选择的典型模式如下所示:
public class SomeDialog {
final CharSequence[] items = { "3 sec", "5 sec", "7 sec" };
int choice = -1;
//...
public void someMethod() {
//...
builder.setSingleChoiceItems(items, -1,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(), items[item],
Toast.LENGTH_SHORT).show();
choice = item; // <-- now we store the selection to a class member
}
});
// ...
}
public String getSelection() {
return items[choice]; // <-- this allows another class to read the current selection
// often called after the dialog has been closed
}
}
公共类对话框{
最终字符序列[]项={“3秒”、“5秒”、“7秒”};
int-choice=-1;
//...
公共方法(){
//...
建造商设置单选择项(项目-1,
新建DialogInterface.OnClickListener(){
公共void onClick(对话框接口对话框,int项){
Toast.makeText(getApplicationContext(),items[item],
吐司。长度(短)。show();
choice=item;//我使用工厂来处理这类事情:
public interface ChoiceListener {
void onPositiveResult(String choice);
}
public static AlertDialog getWallpaperDialog(Context ctx,
final String[] options, int choice, final ChoiceListener listener) {
AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
// set title, buttons, etc.
builder.setSingleChoiceItems(options, choice, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
listener.onPositiveResult(options[which]);
dialog.dismiss();
}
});
return builder.create();
}
建议:将字符串数组存储在资源中(例如strings.xml
)&坚持使用索引而不是字符串作为首选项。我使用工厂来处理这类事情:
public interface ChoiceListener {
void onPositiveResult(String choice);
}
public static AlertDialog getWallpaperDialog(Context ctx,
final String[] options, int choice, final ChoiceListener listener) {
AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
// set title, buttons, etc.
builder.setSingleChoiceItems(options, choice, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
listener.onPositiveResult(options[which]);
dialog.dismiss();
}
});
return builder.create();
}
建议:将字符串数组存储在资源中(例如,strings.xml
)&坚持使用索引而不是字符串作为首选项。我尝试了If…else
首先,我用一个默认值声明一个私有变量
private static int myChoice = 3000; //3000 is Millisecond = 3 sec
然后,我在Toast.makeText()之后添加了一个条件
我是这样得到值的。我试过使用If…else
首先,我用一个默认值声明一个私有变量
private static int myChoice = 3000; //3000 is Millisecond = 3 sec
然后,我在Toast.makeText()之后添加了一个条件
我是这样得到这个值的。getSelection()
对于异步的AlertDialog
s将是一个额外的混乱。getSelection()
对于异步的AlertDialog
s将是一个额外的混乱。