【发布时间】:2016-08-25 20:18:48
【问题描述】:
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
【问题讨论】:
-
您可以从应用内的任何位置访问您的 SharedPreference。
标签: android-studio sharedpreferences