如何将SeekBar值返回到上一个活动



如何将seekBar值从活动B返回到活动A?

seekBar=(SeekBar)findViewById(R.id.seekBarPercentage);
save.setOnClickListener(new View.OnClickListener()  //return to previous activity {
    @Override
    public void onClick(View v) {
        Intent returnIntent=new Intent();
        Project=project.getSelectedItem().toString(); //spinner value
        Description=description.getText().toString(); //editText value
        // seekBar value ?
        returnIntent.putExtra("Project",Project);
        returnIntent.putExtra("Description",Description);
        setResult(Activity.RESULT_OK,returnIntent);
    }
});
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
    int progress = 0;
    @Override
        public void onProgressChanged(SeekBar seekBar, int progresValue, boolean fromUser) {
            progress = progresValue;
            // Toast.makeText(getApplicationContext(), "Changing seekbar's progress", Toast.LENGTH_SHORT).show();
        }
        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
            //  Toast.makeText(getApplicationContext(), "Started tracking seekbar", Toast.LENGTH_SHORT).show();
        }
        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            progressText.setText("Covered: " + progress + "/" + seekBar.getMax());
            // Toast.makeText(getApplicationContext(), "Stopped tracking seekbar", Toast.LENGTH_SHORT).show();
        }
    });
}

除了@Anindya Dutta的答案之外,如果您想持久化数据,请使用SharedPreferences

获取SharedPreferences

SharedPreferences prefs = getDefaultSharedPreferences(context);

读取偏好:

String key = "test1_string_pref";
String default = "returned_if_not_defined";
String test1 = prefs.getString(key, default);

编辑和保存首选项

SharedPreferences.Edtior editor = prefs.edit(); //Get SharedPref Editor
editor.putString(key, "My String");
editor.commit();

更短的写入方式

prefs.edit().putString(key, "Value").commit();

SharedPreferences的其他信息:JavaDoc和Android开发者文章

  1. int progress保持在OnSeekBarChangeListener之外
  2. 将当前进度检索为progress = seekBar.getProgress()
  3. 类似于returnIntent.putExtra("Description",Description);,将progress作为额外的Intent

相关内容

  • 没有找到相关文章

最新更新