0

如果我有两个活动。1.Main_Activity 其中包含一个单选按钮和一个图像按钮。2.Second_activity从Main_activity扩展而来。

这是 Main_activity 中的意图,并包含三个单选按钮,其保存状态为共享首选项。振动、正常和静音模式适用于这些单选按钮。Drvset 活动已经通过图像按钮意图。

我想检索单选按钮的保存状态并通过主活动中的单选按钮加载它。

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ImageButton buttonOne = findViewById(R.id.drvedit);    
        RadioButton buttondr=findViewById(R.id.drvbtn);

        buttonOne.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                System.out.println("Button Clicked");
                Intent activity2Intent = new Intent(getApplicationContext(), drvset.class);
                startActivity(activity2Intent);
            }
        });
    }
}

public class drvset extends MainActivity {

    private RadioButton normal;
    private RadioButton silent;
    private RadioButton vibrate;
    private AudioManager myAudioManager;

    final String KEY_SAVED_RADIO_BUTTON_INDEX = "SAVED_RADIO_BUTTON_INDEX";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_drvset);

        LoadPreferences();

        normal = (RadioButton) findViewById(R.id.radioNormal);
        silent = (RadioButton) findViewById(R.id.radioSilent);
        vibrate=(RadioButton)findViewById(R.id.radiovibrate);

       normal.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
              normalEnable(v);
            }
        });
        silent.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                silentEnable(v);
            }
        });
        vibrate.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                vibrateEnable(v);
            }
        });
        myAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);

    }
    public void vibrateEnable(View view){
        myAudioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
        status.setText("Current Status: Vibrate Mode");
    }
    public void normalEnable(View view){
        myAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
        status.setText("Current Status: Ring Mode");
    }
    public void silentEnable(View view){
        myAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
        status.setText("Current Status: Silent Mode");
    }
                }};
    public void SavePreferences(String key, int value){
        SharedPreferences sharedPreferences = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putInt(key, value);
        editor.apply();
    }

    public void LoadPreferences(){
        SharedPreferences sharedPreferences = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE);
        int savedRadioIndex = sharedPreferences.getInt(KEY_SAVED_RADIO_BUTTON_INDEX, 0);
        RadioButton savedCheckedRadioButton = (RadioButton)radioGroup.getChildAt(savedRadioIndex);
        savedCheckedRadioButton.setChecked(true);
    }   
}
4

0 回答 0