制作一个包含3项活动的受人尊敬的应用程序。 第三个是信使,当我按下后退按钮并返回上一个活动(第一个)时,我刚刚在edittextes中写的所有内容都被清理(文本,数字),我想使用共享首选项,任何帮助? ``
package com.example.AzaaadCompany.mymessenger;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class thirdActivity extends AppCompatActivity {
EditText number;
EditText msg;
Button btnsend;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
number=(EditText)this.findViewById(R.id.noteText);
msg=(EditText)this.findViewById(R.id.editTextmsg);
btnsend=(Button)this.findViewById(R.id.button);
btnsend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String mynumber = number.getText().toString();
String mymsg = msg.getText().toString();
if (mynumber.length() >10 ) {
sendMessage(mynumber, mymsg);
} else {
}
}
});
}
private void sendMessage(String mynumber, String mymsg){
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(mynumber,null,mymsg,null,null);
Toast.makeText(getApplicationContext(), "sent", Toast.LENGTH_LONG).show();
} catch (Exception e){
Toast.makeText(getApplicationContext(),"something is wrong",Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
}
答案 0 :(得分:0)
获取共享偏好
SharedPreferences prefs = this.getSharedPreferences(
"com.example.app", Context.MODE_PRIVATE);
将数据写入SharedPreferences
// Writing data to SharedPreferences
Editor editor = prefs.edit();
editor.putString("key", "some value"); // replace 'some value' with your edit text value
editor.putInt("your_int_key", yourIntValue);
editor.commit();
从SharedPreferences中读取数据
// Reading from SharedPreferences
String value = prefs.getString("key", ""); // replace empty string with some default string (useful if key not found)
int intValue = prefs.getInt("your_int_key", -1);
希望这有助于:)
答案 1 :(得分:0)
写
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt(getString(R.string.saved_high_score), newHighScore);
editor.commit();
读
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
int defaultValue = getResources().getInteger(R.string.saved_high_score_default);
long highScore = sharedPref.getInt(getString(R.string.saved_high_score), defaultValue);
自: https://developer.android.com/training/basics/data-storage/shared-preferences.html
答案 2 :(得分:0)
非常感谢每3个答案, 但是你的答案有不完美之处。我用我的知识结合你的答案,这就是结果。
@覆盖 protected void onDestroy(){
SharedPreferences preferences = getPreferences(Context.MODE_PRIVATE);
String name = msg.getText().toString();
String name2 = number.getText().toString();
SharedPreferences.Editor editor = preferences.edit();
editor.putString("MATN",name);
editor.putString("MATN2",name2);
editor.commit();
super.onDestroy();
}
@Override
protected void onResume() {
SharedPreferences preferences = getPreferences(Context.MODE_PRIVATE);
String value = preferences.getString("MATN2", "");
String matn = preferences.getString("MATN","Hello"):
msg=(EditText)this.findViewById(R.id.editTextmsg);
number=(EditText)this.findViewById(R.id.numberText);
msg.setText(matn);
number.setText(value);
super.onResume();
}
}