我有两个edittext我想在edittext中保存数据,直到我没有改变它如何制作它。我尝试这样的事情,但数据无法保存。
e1 = (EditText)findViewById(R.id.editText);
e2 = (EditText)findViewById(R.id.editText2);
b1 = (Button)findViewById(R.id.button3);
sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String homewifi = e1.getText().toString();
String officewifi = e2.getText().toString();
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(HOMEWIFI, homewifi);
editor.putString(OFFICEWIFI, officewifi);
editor.commit();
Toast.makeText(MainActivity.this,"Thanks",Toast.LENGTH_SHORT).show();
}
});
当活动再次打开时,数据不会显示在edittext中。
答案 0 :(得分:3)
您没有从 sharedpreferences 修改代码中检索数据,如下所示:
sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
e1 = (EditText)findViewById(R.id.editText);
e2 = (EditText)findViewById(R.id.editText2);
String homewifi = sharedpreferences.getString(HOMEWIFI, "");
String officeWifi = sharedpreferences.getString(OFFICEWIFI, "");
e1.setText(homewifi);
e2.setText(officeWifi);
b1 = (Button)findViewById(R.id.button3);
Context.MODE_PRIVATE);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String homewifi = e1.getText().toString();
String officewifi = e2.getText().toString();
editor.putString(HOMEWIFI, homewifi);
editor.putString(OFFICEWIFI, officewifi);
editor.commit();
Toast.makeText(MainActivity.this,"Thanks",Toast.LENGTH_SHORT).show();
}
});
答案 1 :(得分:1)
当活动再次打开时,EditText中没有显示数据。
需要从function get_total_order($id_vendor){
$this->db->like('vendor',$id_vendor);
$this->db->like('status',"due");
$this->db->from('sale');
return $this->db->count_all_results();
}
获取数据并调用SharedPreferences
的{{1}}方法,以便在启动setText
时显示它。再次。赞:
EditText's
答案 2 :(得分:0)
我猜你忘了将您的SharedPreferences中的字符串设置为edittext,
将其从编辑文本保存到共享偏好。
喜欢
e1 = (EditText)findViewById(R.id.editText);
e2 = (EditText)findViewById(R.id.editText2);
b1 = (Button)findViewById(R.id.button3);
sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
if(!TextUtils.isEmpty(sharedpreferences.getString(HOMEWIFI))){
e1.setText(sharedpreferences.getString(HOMEWIFI));
}
if(!TextUtils.isEmpty(sharedpreferences.getString(OFFICEWIFI))){
e1.setText(sharedpreferences.getString(OFFICEWIFI));
}
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String homewifi = e1.getText().toString();
String officewifi = e2.getText().toString();
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(HOMEWIFI, homewifi);
editor.putString(OFFICEWIFI, officewifi);
editor.commit();
Toast.makeText(MainActivity.this,"Thanks",Toast.LENGTH_SHORT).show();
}
});