java - 启动时的if语句

标签 java android if-statement

我目前正在开发一个应用程序,在清除数据和/或安装时必须让用户输入数据。到目前为止,我还没有实现任何目标,因为每次我在模拟器中运行它时,它都会隐藏 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");
}

任何人都可以帮我找到正确的 if 语句吗?我希望它在sharedPreferences为空时显示要输入的用户数据(如果用户安装应用程序和/或清除数据,它们就会显示)。

该 block :

    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();
    }

如果您想知道我的确切意思,请下载我的测试版应用程序:https://play.google.com/store/apps/details?id=jhpcoenen.connectlife.beams

提前谢谢您!

最佳答案

问题是您对两个编辑文本使用相同的 edittext ID,因此请使用适当的 ID

EditText showBeamsEditText =
        (EditText) findViewById(R.id.editText4);
EditText beamsGoal =
        (EditText) findViewById(R.id.editText4);

showBeamsEditTextbeamsGoal 应该有不同的 id,而不是 editText4

并将 beamsSave != "0" 更改为 beamsSave.equals("0"), How to compare strings in java

关于java - 启动时的if语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49055084/

相关文章:

java - 无法打印整个数组

java - Spring 可分页 : Nav Bar Pages by TotalPages

java - 如何使用对话框更改 listView 中 TextView 的文本

javascript - 循环遍历 Javascript 数组

java - 如何在 Android 10 (Android Q) 中访问外部存储?

java - 将 java.util.Date 转换为请求的不同时区

android - 键盘隐藏了 Android WebView 上的输入字段

java - 谷歌地图 - Android 应用程序未加载 - 空对象引用

iphone - 如果带有文本框的语句不起作用

javascript - jquery onclick if ...else if ...else 无法正常工作