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住址和端口號,現在我需要閱讀SharedPreferencesin主要活動如何從另一個活動讀取SharedPreferences?
您可以訪問SharedPreference從你的應用程序內部的任 –