关于在共享偏好中保存radiogroup的状态

时间:2012-01-19 05:30:51

标签: android radio-button sharedpreferences

我在这里有两个完全相同的xml布局的活动,但每个项目的ID不同(每个id应该是唯一的吗?)问题是我无法在第二个活动中加载radiogroup的状态,如果我将它保存在第一个中,反之亦然(因为我使用整数或R.java中的数字)

(顺便说一句,我没有在这里使用单选按钮,我只是声明它,或者我应该使用它?)

以下是代码:

public class SharedPreferencesActivity extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
    String s;
    float number;
    EditText edittext;
    EditText editnum;
    RadioGroup rGroup; 
    RadioButton radioa;
    RadioButton radiob;
    RadioButton radioc;
    SharedPreferences shared;
    Intent intent;
    double x = 0;
    boolean bool = true;
    int radiochecked;
    int radiocheckeddef;
    DecimalFormat df = new DecimalFormat("#.##");
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.page1);
    //final Context cont = SharedPreferencesActivity.this;
    edittext = (EditText) findViewById(R.id.text1);
    editnum = (EditText) findViewById(R.id.decimal1);
    Button save = (Button) findViewById(R.id.save1);
    Button load = (Button) findViewById(R.id.load1);
    Button page = (Button) findViewById(R.id.page1);
    save.setOnClickListener(this);
    load.setOnClickListener(this);
    page.setOnClickListener(this);
    rGroup = (RadioGroup)findViewById(R.id.radioGroup);
    radioa =(RadioButton) findViewById(R.id.radio1a);
    radiob =(RadioButton) findViewById(R.id.radio1b);
    radioc =(RadioButton) findViewById(R.id.radio1c);

    radiocheckeddef = rGroup.getCheckedRadioButtonId();
    shared = getSharedPreferences("string", 0);

}



public void onClick(View v) {
    switch (v.getId()) {
    case R.id.save1:
        //radiochecked = rGroup.getCheckedRadioButtonId();
        s = edittext.getText().toString();
        number = Float.parseFloat(editnum.getText().toString());
        radiochecked = rGroup.getCheckedRadioButtonId();
        SharedPreferences.Editor editor = shared.edit();
        editor.putString("sharedtext", s);
        editor.putFloat("sharednum", number);
        editor.putInt("sharedradio",radiochecked);
        editor.commit();
        Toast.makeText(this, "save1", Toast.LENGTH_SHORT).show();
        break;
    case R.id.load1:
        String returntext = shared.getString("sharedtext", "Returner fail");
        float returnnum = shared.getFloat("sharednum", 0);
        int returnradio = shared.getInt("sharedradio", radiocheckeddef);
        edittext.setText(returntext);
        editnum.setText(String.valueOf(returnnum));
        rGroup.check(returnradio);
        Toast.makeText(this, "load1", Toast.LENGTH_SHORT).show();
        break;
    case R.id.page1:
        intent= new Intent(this,Shared2.class);
        startActivity(intent);
        break;
    }
}

}

public class Shared2 extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
    String string;
    float number;
    EditText edittext;
    EditText editnum;
    RadioGroup rGroup; 
    RadioButton radioa;
    RadioButton radiob;
    RadioButton radioc;
    int radiochecked;
    int radiocheckeddef;
    SharedPreferences shared;
    Intent intent;
    double x = 0;
    DecimalFormat df = new DecimalFormat("#.##");
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.page2);

        edittext = (EditText)findViewById(R.id.text2);
        editnum = (EditText)findViewById(R.id.decimal2);
        Button save = (Button)findViewById(R.id.save2);
        Button load = (Button)findViewById(R.id.load2);
        Button page= (Button)findViewById(R.id.page2);
        save.setOnClickListener(this);
        load.setOnClickListener(this);
        page.setOnClickListener(this);
        shared = getSharedPreferences("string",0);

        rGroup = (RadioGroup)findViewById(R.id.radioGroup2);
        radioa =(RadioButton) findViewById(R.id.radio2a);
        radiob =(RadioButton) findViewById(R.id.radio2b);
        radioc =(RadioButton) findViewById(R.id.radio2c);
        radiocheckeddef = rGroup.getCheckedRadioButtonId();

    }

    public void onClick(View v) {
        switch(v.getId()){
        case R.id.save2:
            //get from edittext
            string = edittext.getText().toString();
            number = Float.parseFloat(editnum.getText().toString());
            radiochecked = rGroup.getCheckedRadioButtonId();
            //put data into sharedpreferences
            SharedPreferences.Editor editor = shared.edit();
            editor.putString("sharedtext",string);
            editor.putFloat("sharednum",number);
            editor.putInt("sharedradio",radiochecked);
            editor.commit();
            Toast.makeText(this,"save2",Toast.LENGTH_SHORT).show();
            break;
        case R.id.load2:
            //get data from sharedpreferences
            String returner = shared.getString("sharedtext","Returner fail");
            float returnnum = shared.getFloat("sharednum",0);
            int returnradio = shared.getInt("sharedradio", radiocheckeddef);
            //put data into edittext fields
            edittext.setText(returner);
            editnum.setText(String.valueOf(returnnum));
            rGroup.check(returnradio);
            Toast.makeText(this,"load2",Toast.LENGTH_SHORT).show();
            break;
        case R.id.page2:
            intent= new Intent(this,SharedPreferencesActivity.class);
            startActivity(intent);
            break;
        }
    }
}

0 个答案:

没有答案