如何从另一个活动中读取 SharedPreferences? [英] how can i read a SharedPreferences from another activity?

查看:52
本文介绍了如何从另一个活动中读取 SharedPreferences?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    sharedPreferences = getSharedPreferences("HTTP_HELPER_PREFS", Context.MODE_PRIVATE);
    editor = sharedPreferences.edit();

    // get the IP address and port number from the last time the user used the app,
    // put an empty string "" is this is the first time.
    editTextIPAddress.setText(sharedPreferences.getString(PREF_IP, ""));
    editTextPortNumber.setText(sharedPreferences.getString(PREF_PORT, ""));
    confbutton.setOnClickListener(this);

}


@Override
public void onClick(View v) {
    // get the ip address
    String ipAddress = editTextIPAddress.getText().toString().trim();
    // get the port number
    String portNumber = editTextPortNumber.getText().toString().trim();

    editor.putString(PREF_IP, ipAddress); // 
    editor.putString(PREF_PORT, portNumber); 
    editor.commit(); 
    onBackPressed();

这是我的代码,我创建了第二个活动来获取 IP 地址和端口号,现在我需要在主活动中读取 SharedPreferences

that's my code i created a second Activity to get the ip adresse and the port number now i need to read that SharedPreferencesin the main activity

推荐答案

类似这样的事情

SharedPreferences prefs = getSharedPreferences("HTTP_HELPER_PREFS", Context.MODE_PRIVATE);
String ipAddress = prefs.getString(PREF_IP, ipAddress); 
String portNumber = prefs.getString(PREF_PORT, portNumber);

这篇关于如何从另一个活动中读取 SharedPreferences?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆