0
嗨,所有人都可以告訴我在保存後,我在編輯文本中獲得保存的數據,我的意思是當我保存一些文本,然後我希望該文本將始終出現在編輯文本謝謝。 這裏是我的代碼:我如何從共享首選項中檢索編輯文本中的數據?
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
private EditText inputName;
private TextInputLayout inputLayoutName;
private Button btnSave;
public static final String MyPREFERENCES = "MyPrefs" ;
public static final String Message = "nameKey";
SharedPreferences sharedpreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
inputLayoutName = (TextInputLayout) findViewById(R.id.input_layout_name);
inputName = (EditText) findViewById(R.id.input_message);
btnSave = (Button) findViewById(R.id.btn_save);
inputName.addTextChangedListener(new MyTextWatcher(inputName));
sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String m = inputName.getText().toString();
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Message, m);
editor.apply();
Toast.makeText(getApplicationContext(), "Thank You!", Toast.LENGTH_SHORT).show();
}
});
}
你能更清楚一點好嗎?文本保留在EditText中,除非您明確從鍵盤上刪除它。你的要求究竟是什麼?您是否在活動之間切換,並希望文字在EditText中自動填充一次以進入相關活動? –
'inputText.setText(sharedpreferences.getString(Message,「」));' – lionscribe