0

我是 android 开发的新手,我想弄清楚我如何制作一个按钮来保存 Activity 中的更改。简介:我有一个 seekBar,我可以在其中更改铃声音量,但它会直接更改系统,我放了一个按钮来保存更改,我不知道该怎么做。这是我的代码:

主要的.xml

 <Button
        android:id="@+id/save"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_text"/>

    <SeekBar
        android:id="@+id/seekVolume"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

活动

public class SaveActivity extends Activity {

    private AudioManager aManager;
    private SeekBar seekVolume;

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

        aManager = (AudioManager) getSystemService(AUDIO_SERVICE);
        Button save = (Button) findViewById(R.id.save);

        setOnClickListener();      
}
    private void setOnClickListener(){
        SeekBar seekVolume = (SeekBar) findViewById(R.id.seekVolume);

        seekVolume.setMax(aManager.getStreamMaxVolume(AudioManager.STREAM_RING));
        seekVolume.setKeyProgressIncrement(1);
        seekVolume.setProgress(aManager.getStreamVolume(AudioManager.STREAM_RING));
        seekVolume.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
            public void onStopTrackingTouch(SeekBar ringvolume) {
            }
            public void onStartTrackingTouch(SeekBar ringvolume) {
            }
            public void onProgressChanged(SeekBar ringvolume, int progress,boolean fromUser) {
aManager.setStreamVolume(AudioManager.STREAM_RING, progress, AudioManager.FLAG_SHOW_UI + AudioManager.FLAG_PLAY_SOUND); 
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event){
        if(keyCode==KeyEvent.KEYCODE_VOLUME_DOWN || keyCode==KeyEvent.KEYCODE_VOLUME_UP){
            seekVolume.setProgress(aManager.getStreamVolume(AudioManager.STREAM_RING));
    }
        return (super.onKeyDown(keyCode, event));
    }

}

我真的不知道该怎么做。多谢你们!

4

1 回答 1

0

要阅读您的偏好(在 中onCreate()):

SharedPreferences preferences = getPreferences(MODE_PRIVATE);
seekVolume.setProgress(preferences.getInt("storedVol", 0));

要保存您的偏好(在 中onDestroy()):

SharedPreferences preferences = getPreference(MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putInt("storedVol", seekVolume.getProgress()); // value to store
editor.commit();

你取回Button saveonCreate()什么也不做。你想让它做什么?如何onClickListener通过添加连接它

  save.setOnClickListener(mySaveHandler);

onCreate()?然后:

View.OnClickListener mySaveHandler = new View.OnClickListener() {
  public void onClick(View v) {
   // Save preferences here instead/aswell
 }
}
于 2012-06-11T16:15:19.833 回答