以下代码的输出是一个图像按钮,当我们点击图像按钮时,会显示一个带有两个单选按钮的警告对话框,以便在两个不同的轨道之间进行选择。一切正常但是当我再次点击图像按钮,即第二次时,单选按钮不显示当前正在播放的曲目,即先前选择的曲目。 以前选择的项目不会被记住。 ?
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();
}
});
}
}
答案 0 :(得分:1)
用户更改时保存选择的索引。将其传递给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();
}
});
}
}
答案 1 :(得分:0)
你只需要一个名为
的新变量 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();
}
});