我正在尝试获取 editText 的值并将其存储在共享首选项中,我这样做了:
SharedPreferences participant;
Editor editor;
private EditText firstname = null;
private String name =null;
private LinearLayout formbis;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_participant);
participant = this.getSharedPreferences("participant", getBaseContext().MODE_PRIVATE);
editor = participant.edit();
formbis = (LinearLayout) findViewById(R.id.formbis);
firstname = new EditText(this);
firstname.setHint("first name");
firstname.setTextSize(12);
firstname.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
name = firstname.getText().toString();
editor.putString("key", name);
editor.commit();
formbis.addView(firstname);
但是当我打开 sharedPreferences XML 文件时,我只发现:editText 中没有值:
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
<string name="key"></string>
</map>