我正在创建一个番茄工作计时器应用程序。它有2项活动:
我已经创建了一个共享首选项
对象,它保存了设置的时间,我希望这些时间显示在Recents活动中的按钮中。但是,似乎没有保存数据,而是显示了包名称。
package com.example.pomotimer;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
import java.util.Locale;
public class Activity2 extends AppCompatActivity {
public static final String EXTRA_TEXT = "com.example.pomotimer.EXTRA_TEXT";
private TextView TextViewCountdown;
private Button Button_Start_Pause;
private Button Reset;
SharedPreferences sp;
private Button buttonSet;
private Button select_Time;
private EditText edit_Text_Input;
private CountDownTimer countDownTimer;
public long mStartTimeinMillis;
private long timeLeft = mStartTimeinMillis;
private boolean timerRunning;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
TextViewCountdown = findViewById(R.id.textViewCount);
Button_Start_Pause = findViewById(R.id.bnStartPause);
Reset = findViewById(R.id.reset);
select_Time = findViewById(R.id.selectTime);
edit_Text_Input = findViewById(R.id.edit_Text_Input);
buttonSet = findViewById(R.id.buttonSet);
sp = getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);
select_Time.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
edit_Text_Input.setVisibility(View.VISIBLE);
buttonSet.setVisibility(View.VISIBLE);
buttonSet.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String input = edit_Text_Input.getText().toString();
if (input.length() == 0){
Toast.makeText(Activity2.this, "Field can't be empty", Toast.LENGTH_SHORT).show();
return;
}
long millisInput = Long.parseLong(input)*60000;
if (millisInput == 0){
Toast.makeText(Activity2.this, "Timer cannot be set to 0", Toast.LENGTH_SHORT).show();
return;
}
setTime(millisInput);
edit_Text_Input.setText("");
}
});
}
});
Button_Start_Pause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (timerRunning){
pauseTimer();
}
else{
startTimer();
}
}
});
Reset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
resetTimer();
}
});
int minutes = (int) (timeLeft/1000)/60;
int seconds = (int) (timeLeft/1000)%60;
String timeLeftFormatted = String.format(Locale.getDefault(),"%02d:%02d", minutes, seconds);
TextViewCountdown.setText(timeLeftFormatted);
}
@Override
public void finish() {
super.finish();
overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
}
public void setTime(long milliseconds){
mStartTimeinMillis = milliseconds;
resetTimer();
SP();
}
private void startTimer(){
countDownTimer = new CountDownTimer(timeLeft,1000) {
@Override
public void onTick(long millisUntilFinished) {
// The countdown timer has an automatic method of reducing time by 1s
timeLeft = millisUntilFinished;
int minutes = (int) (timeLeft/1000)/60;
int seconds = (int) (timeLeft/1000)%60;
String timeLeftFormatted = String.format(Locale.getDefault(),"%02d:%02d", minutes, seconds);
TextViewCountdown.setText(timeLeftFormatted);
}
@Override
public void onFinish() {
timerRunning = false;
Button_Start_Pause.setText("Start");
Button_Start_Pause.setVisibility(View.INVISIBLE);
}
}.start();
timerRunning = true;
Button_Start_Pause.setText("pause");
}
int data = (int) timeLeft;
private void pauseTimer(){
countDownTimer.cancel();
timerRunning = false;
Button_Start_Pause.setText("Start");
}
private void resetTimer(){
timeLeft = mStartTimeinMillis;
int minutes = (int) (timeLeft/1000)/60;
int seconds = (int) (timeLeft/1000)%60;
Button_Start_Pause.setVisibility(View.VISIBLE);
String timeLeftFormatted = String.format(Locale.getDefault(),"%02d:%02d", minutes, seconds);
TextViewCountdown.setText(timeLeftFormatted);
}
@Override
protected void onStop() {
super.onStop();
SharedPreferences prefs = getSharedPreferences("prefs", MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putLong("millisleft", timeLeft);
editor.putBoolean("timerRunning", timerRunning);
editor.apply();
}
@Override
protected void onStart() {
super.onStart();
SharedPreferences prefs = getSharedPreferences("prefs", MODE_PRIVATE);
timeLeft = prefs.getLong("millisleft",mStartTimeinMillis);
timerRunning = prefs.getBoolean("timeRunning", false);
int minutes = (int) (timeLeft/1000)/60;
int seconds = (int) (timeLeft/1000)%60;
String timeLeftFormatted = String.format(Locale.getDefault(),"%02d:%02d", minutes, seconds);
TextViewCountdown.setText(timeLeftFormatted);
}
public void SP() {
String data = TextViewCountdown.toString();
SharedPreferences.Editor editor = sp.edit();
editor.putString("time", data);
editor.commit();
Toast.makeText(Activity2.this, "this ", Toast.LENGTH_SHORT).show();
}
}
package com.example.pomotimer;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
public class recentActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recent);
Button button = findViewById(R.id.button_data);
// String number = getIntent().getStringExtra("keydata");
// button.setText(number);
SharedPreferences sp = getApplicationContext().getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);
String time = sp.getString("time", "");
button.setText(time);
}
}
而不是String data=TextViewCountdown。toString()
应该是
String data=TextViewCountdown。getText()。toString()
你好,我正在尝试保存一个共享首选项的主题。当用户点击某个主题的按钮时,我希望该主题被设置为默认并保存,所以当他们重新打开应用程序时,它仍然是那个新主题。
我正在向SharedPreference添加一个集合。这是来自另一个类的静态集,该类处理一些数据并将其存储在该集中。 然后我转到另一个活动(订单活动),在那里我显示了这组信息。第一次访问共享偏好时,我能够获得正确的详细信息。例如name1、name2、name3。 然后,我离开此活动,返回到另一个活动,添加另一个名称,然后再次返回到此 Order 活动。我希望这次得到名称1,名称2,名称3,名称4
我在共享首选项中保存列表并将其显示在列表视图中没有问题,但我的问题是当我重新启动应用程序并尝试将项目添加到列表视图时 旧的存储项目已删除.有我的代码: } public void getDates(){
以下是我的主要活动 这是我的作品 下面是我的activity_main.xml 和主要活动 现在调试器输出 很明显,即使我稍微调整一下,它也不能正常工作。接下来是:使用共享首选项保存android主题
对于下面的代码,我正在尝试检索共享的首选项,我认为它保存正确,但当我回到登录屏幕时,所有的数据都消失了。我需要它留在我回到这个屏幕上。所以我在个人资料页面上输入姓名、年龄和id到三个单独的行中。然后按下save按钮,然后按下action Bar上的back转到前面的页面。当我回到个人资料页面时,我的信息应该还在那里,但它没有任何帮助?
非常感谢您的光临,我正在为学校开发一个应用程序,我遇到了另一个问题。该应用程序的主要思想是跟踪您的卡路里,我希望它能节省卡路里,所以当应用程序关闭时,它仍然会记住他们。我已经忙了一段时间了,现在尝试使用SavedPreferences,但我总是出现错误。我希望有人能帮我。 } 我可能做了很多明显的愚蠢的事情,但我真的弄不明白。 多谢!