为什么在我重新启动应用程序时编辑文本不可见?

why the edit text is invisible when i restart the application?

我正在尝试制作一个简单的应用程序,在按下按钮后将添加编辑文本,我希望在编辑文本中输入任何内容后将其保存。重新启动应用程序时,我希望看到包含某些内容的编辑文本。我不知道为什么当我重新启动应用程序时,编辑文本中的数据已保存但编辑文本再次不可见。这是我的代码:

public class MainActivity extends AppCompatActivity {

private static final String PREFERENCES_NAME = "myPreferences";
private static final String PLIK_GLOWNEJ_KWOTY = "text_plik_glownej_kwoty";
private static final String PLIK_E1 = "text_pliku_e1";


private EditText WpiszKwote;
private TextView WpisanaKwota;
private Button Przycisk;
private Button Zapisz;
private EditText e1;
private EditText e2;
int x = 0 ;

private SharedPreferences preferences;





@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    preferences = getSharedPreferences(PREFERENCES_NAME, Activity.MODE_PRIVATE);

    //inicjalizacja pola

    WpiszKwote = (EditText) findViewById(R.id.WpiszKwote);
    WpisanaKwota = (TextView) findViewById(R.id.WpisanaKwota);
    Przycisk = (Button) findViewById(R.id.Przycisk);
    Zapisz = (Button) findViewById(R.id.Zapisz);
    e1 = (EditText) findViewById(R.id.editText1);
    e2 = (EditText) findViewById(R.id.editText2);
    if (TextUtils.isEmpty(e1.getText().toString())) {
        e1.setVisibility(View.GONE);
    }else {
        e1.setVisibility(View.VISIBLE);
    }
    if (TextUtils.isEmpty(e2.getText().toString())) {
        e2.setVisibility(View.GONE);
    }
    restoreData();

    //nasłuchiwanie pola edytowania textu

        WpiszKwote.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String wpisana = WpiszKwote.getText().toString();
                WpisanaKwota.setText(wpisana);
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        // nasłuchiwanie przycisku


    Przycisk.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            x++;
            Runnable run = new Runnable() {
                @Override
                public void run() {
                    x = 0;
                }
            };

            if (x == 1) {
                e1.setVisibility(View.VISIBLE);
            } else if (x == 2) {
                e2.setVisibility(View.VISIBLE);
            }
        }
    });


        // nasłuchiwanie przycisku zapisywania


        Zapisz.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                saveData();
                showToast("Dane zostały zapisane");

            }
        });


}
private void saveData() {
    SharedPreferences.Editor preferencesEditor = preferences.edit();
    String editTextData = WpiszKwote.getText().toString();
    String editTextData1 = e1.getText().toString();
    preferencesEditor.putString(PLIK_GLOWNEJ_KWOTY, editTextData);
    preferencesEditor.putString(PLIK_E1, editTextData1);
    preferencesEditor.commit();
}
private void restoreData() {
    String text_pliku_glownej_kwoty = preferences.getString(PLIK_GLOWNEJ_KWOTY, "");
    WpisanaKwota.setText(text_pliku_glownej_kwoty);
    String text_pola_e1 = preferences.getString(PLIK_E1, "");
    e1.setText(text_pola_e1);

}
private void showToast(String msg) {
    Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
}

}

我认为那是因为您首先检查 e1.text 是否为空,然后 restoreData()。首先要恢复数据,然后检查它是否为空。

简单改变这个

if (TextUtils.isEmpty(e1.getText().toString())) {
    e1.setVisibility(View.GONE);
}else {
    e1.setVisibility(View.VISIBLE);
}
if (TextUtils.isEmpty(e2.getText().toString())) {
    e2.setVisibility(View.GONE);
}
restoreData();

restoreData();
if (TextUtils.isEmpty(e1.getText().toString())) {
    e1.setVisibility(View.GONE);
}else {
    e1.setVisibility(View.VISIBLE);
}
if (TextUtils.isEmpty(e2.getText().toString())) {
    e2.setVisibility(View.GONE);
}

希望对您有所帮助