我创建了一个带有单选按钮的警报对话框,当我选择单选按钮轨道列表时,它在下次选择时不记得音轨,因此我无法知道当前正在播放的音轨。 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;
}
}
答案 0 :(得分:1)
您需要将其存储到数据库,存储或使用静态变量,并在用户更改radiobox时随时更改变量的值。
干杯,
答案 1 :(得分:0)
Radio button does not preserve value. So you loose them after exiting. You can handle radio button scenario in combination with sharedpreferences.
After setting radio button add it to shared preferences like:
private RadioButton mWifiDataButton = null;
private RadioButton mWifiButton = null;
in onCreate() ..
mWifiDataButton = (RadioButton) findViewById(R.id.radio_wifi_data);
mWifiDataButton.setOnClickListener(this);
mWifiButton = (RadioButton) findViewById(R.id.radio_wifi_only);
mWifiButton.setOnClickListener(this);
Editor ed = pref.edit();
ed.putBoolean("pref_radio_wifidata",mWifiDataButton.isChecked());
ed.putBoolean("pref_radio_wifi",mWifiButton.isChecked());
and next time retrieve the values like this:
mWifiDataButton.setChecked(pref.getBoolean("pref_radio_wifidata", true));
mWifiButton.setChecked(pref.getBoolean("pref_radio_wifi", false));
Then in onClick() handle it properly:
@Override
public void onClick(View v) {
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
switch(v.getId()){
case R.id.radio_wifi_data:
pref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
boolean wifidataState = pref.getBoolean("pref_radio_wifidata", true);
mWifiDataButton.setChecked(!wifidataState);
mWifiButton.setChecked(wifidataState);
Editor ed = pref.edit();
ed.putBoolean("pref_radio_wifidata", !wifidataState);
ed.putBoolean("pref_radio_wifi", wifidataState);
ed.commit();
break;
case R.id.radio_wifi_only:
pref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
boolean wifiState = pref.getBoolean("pref_radio_wifi", false);
mWifiButton.setChecked(!wifiState);
mWifiDataButton.setChecked(wifiState);
Editor ed1 = pref.edit();
ed1.putBoolean("pref_radio_wifi", !wifiState);
ed1.putBoolean("pref_radio_wifidata", wifiState);
ed1.commit();
break;
ed.commit();