的Android如何从编辑文本文本保存到共享preferences上按一下按钮 [英] Android How to save text from Edit Text to Shared Preferences on button click
问题描述
要么我可以输入上的编辑文本的文本,或选择从一个spinner.If pre定义的文本,我从微调选择,我只在编辑文本显示它。我想,如果我点击保存按钮,从编辑文本,文本将得到保存到共享preferences。现在,我节省了 onAfterTextChanged()
文本,但我希望它得到保存按钮的preSS。请Suggest.I甚至跟着教程,但并没有得到多少。
MainActivity.java
私人的EditText消息;
微调微调;
按钮来保存;
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
最后一个共享preferences preF = preferenceManager
.getDefaultShared preferences(本);
节省=(按钮)findViewById(R.id.save);
save.setOnClickListener(新OnClickListener(){ @覆盖
公共无效的onClick(视图v){
// TODO自动生成方法存根
共享preferences preF = preferenceManager.getDefaultShared preferences(MainActivity.this);
(自动保存,message.getText()的toString())。pref.edit()putString提交();
}
}); 消息=(EditText上)findViewById(R.id.et_message);
// message.setFocusable(假); 微调=(微调)findViewById(R.id.Spin_alert_message);
dbHelper =新MySQLiteHelper(MainActivity.this);
selectedContactList = dbHelper.getAllContacts();
ArrayAdapter<串GT; ADP =新ArrayAdapter<串GT;(
getApplicationContext(),android.R.layout.simple_spinner_item,
警报);
spinner.setAdapter(ADP); spinner.setOnItemSelectedListener(新OnItemSelectedListener(){
@覆盖
公共无效onItemSelected(适配器视图<>为arg0,ARG1查看,
INT位置,长的id){
// message.setSelection(pref.getString(自动保存,));
字符串将selectedItem =警告[位置]
message.setText(将selectedItem); //加载已经存储到SP并将其设置为文本
//的EditText //把光标移动到前方
message.setSelection(message.getText()长()); } @覆盖
公共无效onNothingSelected(适配器视图<>为arg0){
}
}); / ************保存自定义消息******************** ********* / ListView控件contactlist =(ListView控件)findViewById(R.id.contactListitems);
资源解析度= getResources();
适配器=新CustomAdapter(MainActivity.this,selectedContactList,
资源,MainActivity.this);
contactlist.setAdapter(适配器);
按钮
单击这样写: 私人OnClickListener onsaveButtonClick =新OnClickListener(){ @覆盖
公共无效的onClick(视图v){
// TODO自动生成方法存根
ReturningClass.setMY_NAME_ preF(getApplicationContext(),Amresh);
ReturningClass.setMY_USERID_ preF(getApplicationContext(),8);
}
};
ReturningClass.java
:
公共类ReturningClass { 私人静态字符串MY_NAME_ preF =了mystring preF; 私人静态字符串MY_USERID_ preF =shareduserid; 公共静态共享preferences获得preFS(上下文的背景下){
返回context.getShared preferences(UserNameAcrossApplication
context.MODE_PRIVATE); } 公共静态字符串getMY_NAME_ preF(上下文的背景下){
得到的回报preFS(上下文).getString(MY_NAME_ preF的,);
} 公共静态无效setMY_NAME_ preF(上下文的背景下,字符串值){
获得preFS(上下文).edit()putString(MY_NAME_ preF,值).commit()。
} 公共静态字符串getMY_USERID_ preF(上下文的背景下){
得到的回报preFS(上下文).getString(MY_USERID_ preF的,);
} 公共静态无效setMY_USERID_ preF(上下文的背景下,字符串值){
获得preFS(上下文).edit()putString(MY_USERID_ preF,值).commit()。
}}
你可以得到的值是这样的:
字符串名称= ReturningClass.getMY_NAME_ preF(MainActivity.this);
字符串ID = ReturningClass.getMY_USERID_ preF(MainActivity.this);
有了这个code,可你能得到或值存储在您的任何活动的
。
Either I can enter a text on Edit Text or choose a pre-defined text from the spinner.If I choose from spinner, I am displaying it on Edit Text only. I want if I click on save button, the text from Edit Text will get saved to Shared Preferences. Right now, I am saving the text on onAfterTextChanged()
but I want it to get saved on press of button. Please Suggest.I even followed the tutorials but didn't get much.
MainActivity.java
private EditText message;
Spinner spinner;
Button save;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final SharedPreferences pref = PreferenceManager
.getDefaultSharedPreferences(this);
save = (Button) findViewById(R.id.save);
save.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
pref.edit().putString("autoSave", message.getText().toString()).commit();
}
}) ;
message = (EditText) findViewById(R.id.et_message);
// message.setFocusable(false);
spinner = (Spinner) findViewById(R.id.Spin_alert_message);
dbHelper = new MySQLiteHelper(MainActivity.this);
selectedContactList = dbHelper.getAllContacts();
ArrayAdapter<String> adp = new ArrayAdapter<String>(
getApplicationContext(), android.R.layout.simple_spinner_item,
alert);
spinner.setAdapter(adp);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int position, long id) {
// message.setSelection(pref.getString("AutoSave",""));
String selectedItem = alert[position];
message.setText(selectedItem);
// loads the text that has been stored to SP and set it to
// EditText
// to bring cursor to front
message.setSelection(message.getText().length());
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
/************************** saving custom message *****************************/
ListView contactlist = (ListView) findViewById(R.id.contactListitems);
Resources res = getResources();
adapter = new CustomAdapter(MainActivity.this, selectedContactList,
res, MainActivity.this);
contactlist.setAdapter(adapter);
on Button
click write this :
private OnClickListener onsaveButtonClick = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ReturningClass.setMY_NAME_PREF(getApplicationContext(), "Amresh");
ReturningClass.setMY_USERID_PREF(getApplicationContext(), "8");
}
};
ReturningClass.java
:
public class ReturningClass {
private static String MY_NAME_PREF = "mystringpref";
private static String MY_USERID_PREF = "shareduserid";
public static SharedPreferences getPrefs(Context context) {
return context.getSharedPreferences("UserNameAcrossApplication",
context.MODE_PRIVATE);
}
public static String getMY_NAME_PREF(Context context) {
return getPrefs(context).getString(MY_NAME_PREF, "");
}
public static void setMY_NAME_PREF(Context context, String value) {
getPrefs(context).edit().putString(MY_NAME_PREF, value).commit();
}
public static String getMY_USERID_PREF(Context context) {
return getPrefs(context).getString(MY_USERID_PREF, "");
}
public static void setMY_USERID_PREF(Context context, String value) {
getPrefs(context).edit().putString(MY_USERID_PREF, value).commit();
}
}
you can get the values like this :
String Name = ReturningClass.getMY_NAME_PREF(MainActivity.this);
String Id = ReturningClass.getMY_USERID_PREF(MainActivity.this);
With this code you can you can get or store the values in any of your Activity
.
这篇关于的Android如何从编辑文本文本保存到共享preferences上按一下按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!