我先做两个活动是timer.java活动,这是一个倒计时活动,第二个活动是SaveRestTime.java这个活动用户输入数字值在edittext中,用户将保存它我在这个活动中使用共享偏好现在我想要哪个如果用户没有保存任何值,那么SaveRestTime.java类中的值用户保存将自动生成倒计时值,默认值为30秒请帮助我,我很困惑如何从另一个活动中读取值 这是timer.java代码
package com.cinegoes.www.daily10exercise;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.CountDownTimer;
import com.cinegoes.www.daily10exercise.SaveRestTime;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import java.util.concurrent.TimeUnit;
import static com.cinegoes.www.daily10exercise.SaveRestTime.mypreference;
/**
* Created by ever on 7/25/2017.
*/
public class timer extends Activity implements View.OnClickListener {
private CountDownTimer countDownTimer;
private boolean timerStarted = false;
private Button buttonStart;
public TextView textView;
private ProgressBar progressBarCircle;
private final long startTime = 30 * 1000;
private final long interval = 1 * 1000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.timer);
buttonStart = (Button) this.findViewById(R.id.button);
progressBarCircle = (ProgressBar) findViewById(R.id.progressBarCircle);
buttonStart.setOnClickListener(this);
textView = (TextView) this.findViewById(R.id.textView);
countDownTimer = new CountDownTimerActivity(startTime, interval);
textView.setText(textView.getText() + String.valueOf(startTime/1000));
}
@Override
protected void onStart() {
super.onStart();
countDownTimer.start();
timerStarted = true;
setProgressBarValues();
}
@Override
public void onClick(View v) {
finish();
}
public class CountDownTimerActivity extends CountDownTimer {
public CountDownTimerActivity(long startTime, long interval) {
super(startTime, interval);
}
@Override
public void onFinish() {
textView.setText(msTimeFormatter(startTime));
textView.setText("Time's up!");
setProgressBarValues();
finish();
}
@Override
public void onTick(long millisUntilFinished) {
textView.setText(msTimeFormatter(millisUntilFinished));
progressBarCircle.setProgress((int) (millisUntilFinished / 1000));
}
}
private void setProgressBarValues() {
progressBarCircle.setMax((int) startTime / 1000);
progressBarCircle.setProgress((int) startTime / 1000);
}
private String msTimeFormatter(long milliSeconds) {
String ms = String.format("%02d:%02d",
TimeUnit.MILLISECONDS.toMinutes(milliSeconds) -
TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(milliSeconds)),
TimeUnit.MILLISECONDS.toSeconds(milliSeconds) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(milliSeconds)));
return ms;
}
}
这是我的SaveRestTime.java代码
package com.cinegoes.www.daily10exercise;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
public class SaveRestTime extends Activity {
SharedPreferences sharedpreferences;
TextView name;
public static final String mypreference = "mypref";
public static final String Name = "nameKey";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.saveresttime);
name = (TextView) findViewById(R.id.etName);
sharedpreferences = getSharedPreferences(mypreference,
Context.MODE_PRIVATE);
if (sharedpreferences.contains(Name)) {
name.setText(sharedpreferences.getString(Name, ""));
}
}
public void Save(View view) {
String n = name.getText().toString();
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Name, n);
editor.commit();
}
public void Get(View view) {
name = (TextView) findViewById(R.id.etName);
sharedpreferences = getSharedPreferences(mypreference,
Context.MODE_PRIVATE);
if (sharedpreferences.contains(Name))
{name.setText(sharedpreferences.getString(Name, ""));
}
}
}
答案 0 :(得分:1)
SharedPreferences prefs = this.getSharedPreferences("myPref", Context.MODE_PRIVATE);
String lanSettings = prefs.getString("nameKey", null);