我目前正在开发一个应用程序,在清除数据和/或安装时必须让用户输入数据。到目前为止,我没有实现它,因为每次我在模拟器中运行它时,它都会显示process.env
隐藏和休息。我试过EditTexts
,但我不确定它是否正确。
代码如下:
if-statement
任何人都可以帮助我找到正确的@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
checkFirstRun();
savedData();
}
public void savedData() {
showGoalTextView = (TextView) findViewById(R.id.textView3);
showBeamsTextView = (TextView) findViewById(R.id.textView2);
showGoalEditText = (EditText) findViewById(R.id.editText2);
checkBtnPressed = (ImageButton) findViewById(R.id.imageButton5);
showBeamsEditText = (EditText) findViewById(R.id.editText4);
beamsGoal = (EditText) findViewById(R.id.editText4);
//Fetch the stored data and display it upon starting.
String goalSave = showGoalTextView.getText().toString();
String beamsSave = showBeamsTextView.getText().toString();
preferences = getSharedPreferences("userData", MODE_PRIVATE);
goalSave = preferences.getString("goals", goalSave);
if (goalSave != null) {
showGoalTextView.setText(goalSave);
hideAll();
} else {
resetAll();
}
beamsSave = preferences.getString("beam", beamsSave);
if (beamsSave != "0") {
showBeamsTextView.setText(beamsSave);
hideAll();
} else {
resetAll();
}
}
public void checkFirstRun() {
final String PREFS_NAME = "MyPrefsFile";
final String PREF_VERSION_CODE_KEY = "version_code";
final int DOESNT_EXIST = -1;
//Get current version code
int currentVersionCode = BuildConfig.VERSION_CODE;
//Get saved version
SharedPreferences preferences = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
int savedVersionCode = preferences.getInt(PREF_VERSION_CODE_KEY, DOESNT_EXIST);
//Check for first run or upgrade
if (currentVersionCode == savedVersionCode) {
//No big deal
return;
} else if (savedVersionCode == DOESNT_EXIST) {
//TODO This is a new install
} else if (currentVersionCode > savedVersionCode) {
//TODO This is an upgrade
return;
}
//Update sharedPreferences with the current version code
preferences.edit().putInt(PREF_VERSION_CODE_KEY, currentVersionCode).apply();
}
public void hideAll() {
TextView showGoalTextView =
(TextView) findViewById(R.id.textView3);
EditText showGoalEditText =
(EditText) findViewById(R.id.editText2);
ImageButton checkBtnPressed =
(ImageButton) findViewById(R.id.imageButton5);
EditText showBeamsEditText =
(EditText) findViewById(R.id.editText4);
EditText beamsGoal =
(EditText) findViewById(R.id.editText4);
//Hide rest
showGoalEditText.setVisibility(View.GONE);
checkBtnPressed.setVisibility(View.GONE);
showBeamsEditText.setVisibility(View.GONE);
beamsGoal.setVisibility(View.GONE);
}
public void resetAll() {
//For when resetting
TextView showGoalTextView =
(TextView) findViewById(R.id.textView3);
EditText showGoalEditText =
(EditText) findViewById(R.id.editText2);
ImageButton checkBtnPressed =
(ImageButton) findViewById(R.id.imageButton5);
EditText showBeamsEditText =
(EditText) findViewById(R.id.editText4);
EditText beamsGoal =
(EditText) findViewById(R.id.editText4);
//Clear editTexts
showGoalEditText.getText().clear();
showBeamsEditText.getText().clear();
//Show all editTexts
showGoalEditText.setVisibility(View.VISIBLE);
checkBtnPressed.setVisibility(View.VISIBLE);
showBeamsEditText.setVisibility(View.VISIBLE);
beamsGoal.setVisibility(View.VISIBLE);
//Get the text view
TextView showResetTextView =
(TextView) findViewById(R.id.textView2);
//Get the value of the text view
String resetString = showResetTextView.getText().toString();
//Convert value to a number and reset it
Integer reset = Integer.parseInt(resetString);
reset = 0;
//Display the new value in the text view.
showResetTextView.setText(reset.toString());
showGoalTextView.setText("BEAMS");
}
吗?我想让它显示要输入的用户数据,当sharedPreferences为空时(如果用户安装应用程序和/或清除数据,他们将会这样做。
这个区块:
if-statement
如果您想知道我的意思,请下载我的测试版应用:https://play.google.com/store/apps/details?id=jhpcoenen.connectlife.beams
提前谢谢!
答案 0 :(得分:2)
问题是您对两个编辑文本使用相同的edittext ID,因此请使用适当的ID
EditText showBeamsEditText =
(EditText) findViewById(R.id.editText4);
EditText beamsGoal =
(EditText) findViewById(R.id.editText4);
showBeamsEditText
和beamsGoal
应该有不同的ID而不是editText4
并将beamsSave != "0"
更改为beamsSave.equals("0")
,How to compare strings in java
答案 1 :(得分:1)
GoalSave
永远不会是null
所以只需删除空检查...并替换为.isEmpty()
检查