Android – get / set the sharedpreference global value

Hi, I have a class (mycustomform. XML) that I use as the loginform of the user. Now I want to use SharedPreferences to save and load the value in the user name (EditText) from loginform, but I don't know how to set the username value saved by SharedPreferences to EditText in loginform (mycustomform. XML)

I want to save the value in onpause in my main.xml and load the value through oncreate in the class mycustomform.xml

Usually I want to use global SharedPreferences

How does this look? Can someone help me get on track?

It is thinking of the following main.xml:

public class AndroidLogin extends Activity implements OnClickListener {

@Override     
protected void onPause() { 
   super.onPause();  
     Editor e = mPrefs.edit();
     e.putString(USERNM, username);
     e.commit();
}
}
@H_502_18@

代码MyCustomForm(LoginForm):

public class MyCustomForm extends Dialog {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(com.sencide.R.layout.inlogdialog);

    EditText userTest = (EditText)findViewById(R.id.txtUserName);
    userTest.setText(USERNM);
}
}
@H_502_18@

解决方法:

你可以这样做:

SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(YouActivity.this);

String servername = settings.getString("sharedPreferencesKey", "defaultValue");
server.setText(servername);  // EditText
@H_502_18@

你存储这样的数据:

SharedPreferences.Editor editor = settings.edit();
editor.putString("server", "serverName");
@H_502_18@

编辑:

这段代码应该为您解决问题:

SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
String servername = settings.getString("sharedPreferencesKey", "defaultValue");
@H_502_18@

          

总结

以上是编程之家为你收集整理的android – 获取/设置值SharedPreference全局全部内容,希望文章能够帮你解决android – 获取/设置值SharedPreference全局所遇到的程序开发问题。

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
喜欢与人分享编程技术与工作经验,欢迎加入编程之家官方交流群!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>