下面代码的输出是一个图像按钮,当我们单击图像按钮时,会显示一个带有两个单选按钮的警报对话框,用于在 2 个不同的轨道之间进行选择。一切正常,但是当我再次单击图像按钮(即第二次)时,单选按钮不会显示当前播放的曲目,即先前选择的曲目。以前选择的项目不会被记住。?
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();
}
});
}
}