о сохранении статуса радиогруппы в общих настройках - PullRequest
0 голосов
/ 19 января 2012

У меня есть 2 действия с точно такой же разметкой XML, но с разными идентификаторами для каждого элемента (каждый идентификатор должен быть уникальным, верно?), Проблема в том, что я не смог загрузить статус радиогруппы во втором действии, если Я сохраняю его в первом и наоборот (поскольку я использую целое число или число из 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;
        }
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...