【发布时间】:2017-09-27 05:55:36
【问题描述】:
我正在开发我的第一个应用程序,即秒表
所以使用下面的代码,我可以创建一个秒表,它工作正常。但是此时,当我完全关闭应用程序并重新启动它时,它的统计信息从 0:0:0 我知道我应该在这里使用共享偏好,但我对此没有足够的了解,所以我没有得到什么我要。
public class StopwatchFragment extends Fragment implements View.OnClickListener {
public static StopwatchFragment newInstance() {
StopwatchFragment fragment = new StopwatchFragment();
return fragment;
}
FloatingActionButton stopwatchButton;
ImageView stopwatchReplayButton, stopwatchShareButton;
TextView timerValue;
private long startTime=0L;
long timeInMilliSec= 0L;
long timeSwapBuff=0L;
long updatedTime=0L;
int secs, mins, millisec;
private Handler customHandler= new Handler();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.stopwatch, container, false);
stopwatchButton=(FloatingActionButton) view.findViewById(R.id.stopwatchButton);
stopwatchButton.setOnClickListener(this);
timerValue=(TextView) view.findViewById(R.id.timerValue);
stopwatchReplayButton=(ImageView)view.findViewById(R.id.stopwatchReplayButton);
stopwatchReplayButton.setOnClickListener(this);
stopwatchShareButton=(ImageView)view.findViewById(R.id.stopwatchShareButton);
stopwatchShareButton.setOnClickListener(this);
return view;
}
@Override
public void onClick(View view) {
if(view==stopwatchButton)
{
if(stopwatchButton.getTag().toString().equals("off"))
{
stopwatchButton.setTag("on");
stopwatchButton.setImageResource(R.drawable.pause);
startTime= SystemClock.uptimeMillis();
customHandler.postDelayed(updateTimerThread,0);
stopwatchReplayButton.setVisibility(View.VISIBLE);
}
else
{
stopwatchButton.setTag("off");
stopwatchShareButton.setVisibility(View.VISIBLE);
stopwatchButton.setImageResource(R.drawable.play);
timeSwapBuff+=timeInMilliSec;
customHandler.removeCallbacks(updateTimerThread);
}
}
if(view==stopwatchReplayButton)
{
customHandler.removeCallbacks(updateTimerThread);
timerValue.setText("0 : 00 : 00");
stopwatchButton.setTag("off");
stopwatchButton.setImageResource(R.drawable.play);
timeInMilliSec= 0L;
timeSwapBuff=0L;
updatedTime=0L;
}
if(view==stopwatchShareButton)
{
String text="My Time is "+mins+" : "+secs+" : "+millisec;
Intent i=new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_TEXT,text);
startActivity(Intent.createChooser(i,"Share Using"));
}
}
private Runnable updateTimerThread=new Runnable() {
@Override
public void run() {
timeInMilliSec = SystemClock.uptimeMillis() - startTime;
updatedTime = timeSwapBuff + timeInMilliSec;
secs = (int) (updatedTime / 1000);
mins = secs / 60;
secs = secs % 60;
millisec = (int) (updatedTime % 1000);
timerValue.setText("" + mins + " : " + String.format("%02d", secs) + " : " + String.format("%03d", millisec));
customHandler.postDelayed(this, 0);
}
};
}
我只想实现:如果用户通过按停止按钮停止秒表,并假设计时器值为 0:3:50,那么即使在完全关闭应用程序后,用户应该在打开时获得最后一个计时器值再次应用,
【问题讨论】:
-
请说明您是如何尝试使用 SharedPreferences 的。