如何将活动B 的seekBar
值返回活动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();
}
});
}
答案 0 :(得分:1)
除了@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 Developers Article
答案 1 :(得分:0)
int progress
置于OnSeekBarChangeListener
。progress = seekBar.getProgress()
。returnIntent.putExtra("Description",Description);
类似,将progress
作为额外内容添加到Intent
。