Radiobutton记不住以前选择的项目Android
下面代码的输出是一个图像按钮,当我们点击图像按钮时,会显示一个带有两个单选按钮的alertdialog框,以便在2个不同的轨道之间进行选择。一切工作正常,但当我再次点击图像按钮,即第二次,单选按钮不显示当前正在播放的曲目,即以前选择的曲目。 以前选择的项目不被记住。 ?Radiobutton记不住以前选择的项目Android
public class MainActivity extends Activity { Button b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button) findViewById(R.id.button1);
final CharSequence[] items = {"Track #1", "Track #2"};
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Pick a number");
builder.setIcon(R.drawable.ic_launcher);
builder.setNeutralButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to execute after dialog
// closed
Toast.makeText(getApplicationContext(), "You clicked on OK",
Toast.LENGTH_SHORT).show();
dialog.cancel();
}
});
builder.setSingleChoiceItems(items, -1,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
// TODO Auto-generated method stub
switch (item) {
case 0:
Toast.makeText(getApplicationContext(),
items[item], Toast.LENGTH_SHORT).show();
// Your code when first option seletced
break;
case 1:
// Your code when 2nd option seletced
Toast.makeText(getApplicationContext(),
items[item], Toast.LENGTH_SHORT).show();
break;
}
dialog.cancel();
}
});
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
AlertDialog alert = builder.create();
alert.show();
}
});
}
}
回答:
你只需要当用户改变它采取了一个名为
public int mSelectedValue = -1;
新的变量,并把它传递给
builder.setSingleChoiceItems(items, mSelectedValue, new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface dialmSelectedValue og, int item) {
switch (item) {
case 0:
mSelectedValue = 0;
Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
break;
case 1:
mSelectedValue = 1;
Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
break;
}
dialog.cancel();
}
});
回答:
保存的首选指标。通过这个上builder.setSingleChoiceItems()
:
public class MainActivity extends Activity { int currentChoice = -1;
Button b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button) findViewById(R.id.button1);
final CharSequence[] items = {"Track #1", "Track #2"};
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Pick a number");
builder.setIcon(R.drawable.ic_launcher);
builder.setNeutralButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to execute after dialog
// closed
Toast.makeText(getApplicationContext(), "You clicked on OK",
Toast.LENGTH_SHORT).show();
dialog.cancel();
}
});
builder.setSingleChoiceItems(items, currentChoice,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
// TODO Auto-generated method stub
switch (item) {
case 0:
Toast.makeText(getApplicationContext(),
items[item], Toast.LENGTH_SHORT).show();
// Your code when first option seletced
currentChoice = 0;
break;
case 1:
// Your code when 2nd option seletced
Toast.makeText(getApplicationContext(),
items[item], Toast.LENGTH_SHORT).show();
currentChoice = 1;
break;
}
dialog.cancel();
}
});
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
AlertDialog alert = builder.create();
alert.show();
}
});
}
}
以上是 Radiobutton记不住以前选择的项目Android 的全部内容, 来源链接: utcz.com/qa/257854.html