如何将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开发者文章
- 将
int progress
保持在OnSeekBarChangeListener
之外 - 将当前进度检索为
progress = seekBar.getProgress()
- 类似于
returnIntent.putExtra("Description",Description);
,将progress
作为额外的Intent