我创建了一个带有单选按钮的警报对话框,当我选择单选按钮曲目列表时,它不会记住下一次选择期间的曲目,因此我无法知道当前正在播放哪个曲目。int currentselection = -1;
请检查代码并告诉我我的代码是否有问题。提前致谢..
public class MainActivity extends Activity {
Button b1;
int currentselection = -1;
@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","Track #3" };
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select Track");
builder.setIcon(R.drawable.ic_launcher);
builder.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.setSingleChoiceItems(items, currentselection,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface mdialog, int item) {
// TODO Auto-generated method stub
switch (item) {
case 0:
currentselection = 0;
Toast.makeText(getApplicationContext(),
items[item], Toast.LENGTH_SHORT).show();
break;
case 1:
currentselection = 1;
Toast.makeText(getApplicationContext(),
items[item], Toast.LENGTH_SHORT).show();
break;
case 2:
currentselection = 2;
Toast.makeText(getApplicationContext(),
items[item], Toast.LENGTH_SHORT).show();
break;
}
mdialog.cancel();
}
});
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
AlertDialog alert = builder.create();
alert.show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}