如何将SeekBar值返回到之前的活动?

时间:2015-11-27 17:23:17

标签: java android seekbar startactivityforresult

如何将活动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();
        }
    });

}

2 个答案:

答案 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的其他信息:JavaDocAndroid Developers Article

答案 1 :(得分:0)

  1. int progress置于OnSeekBarChangeListener
  2. 之外
  3. 将当前进度检索为progress = seekBar.getProgress()
  4. returnIntent.putExtra("Description",Description);类似,将progress作为额外内容添加到Intent