-2

我创建了一个带有单选按钮的警报对话框,当我选择单选按钮曲目列表时,它不会记住下一次选择期间的曲目,因此我无法知道当前正在播放哪个曲目。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;
}

}
4

2 回答 2

1

您需要将其存储到数据库、存储或使用静态变量并在用户更改单选框时更改变量的值。

干杯,

于 2013-08-06T05:00:21.720 回答
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();
于 2013-08-06T04:44:50.677 回答