با سلام.
کد زیر رو از جایی گرفتم.درست هم کار میکنه ولی یه ابهامی در خصوص نحوه کار خطوطی که قرمز رنگشون کردم دارم
سوال اینکه:
این خطوط میگه مقدار متغیر Name رو از شی sharedpreferences بگیر و بریز توی shar1. درسته؟
سوال اینکه متغیر Name که یه متغیر ساده تعریف شده است چجوری وصل شد به sharedpreferences ؟
اصلا این دوتا کجا به هم ربط داده شدن؟
ممنون که جواب میدید.
EditText ed1,ed2,ed3;
Button b1;
public static final String MyREFERENCES = "MyPrefs" ;
public static final String Name = "nameKey";
public static final String Phone = "phoneKey";
public static final String Email = "emailKey";
SharedPreferences sharedpreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed1=(EditText)findViewById(R.id.editText);
ed2=(EditText)findViewById(R.id.editText2);
ed3=(EditText)findViewById(R.id.editText3);
b1=(Button)findViewById(R.id.button);
sharedpreferences = getSharedPreferences(MyREFERENCES, Context.MODE_PRIVATE);
//نمایش اطلاعات ذخیره شده
String shar1 = sharedpreferences.getString(Name,"");
String shar2 = sharedpreferences.getString(Phone,"");
String shar3 = sharedpreferences.getString(Email,"");
ed1.setText(shar1);
ed2.setText(shar2);
ed3.setText(shar3);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String n = ed1.getText().toString();
String ph = ed2.getText().toString();
String e = ed3.getText().toString();
//ذخیره اطلاعات
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Name, n);
editor.putString(Phone, ph);
editor.putString(Email, e);
editor.commit();
Toast.makeText(MainActivity.this,"Thanks",Toast.LENGTH_LONG).show();
}
});
}
}
برچسب:
نویسنده: خنج
تاريخ: دوشنبه
6 آذر
1396 ساعت: 20:41