启动时为if语句

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了启动时为if语句相关的知识,希望对你有一定的参考价值。

我目前正在开发一个应用程序,在清除数据和/或安装时必须让用户输入数据。到目前为止,我没有实现任何目标,因为每次我在模拟器中运行它,它都会带有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-statement?我希望它在sharedPreferences为空时显示要输入的用户数据(如果用户安装应用程序和/或清除数据,他们将会这样做。

这个块:

    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

另一答案

GoalSave将永远不会是null所以只需删除空检查..并替换为.isEmpty()检查而不是

以上是关于启动时为if语句的主要内容,如果未能解决你的问题,请参考以下文章

片段的onPause()中的缓存数据在返回片段时为null

20个简洁的 JS 代码片段

20个简洁的 JS 代码片段

编写代码片段的更简洁的方法

shell if 语句

Linux里面if -s命令作用是啥?