我创建了一个页面,其中我在 EditText 中接收 String 和 int,然后通过单击保存按钮将其存储在 sharedpreferences 中,但是我无法使其正常工作。当我重新打开页面数据丢失时,具体数据不会存储。请帮忙
public class Abc extends Activity{
Button one2five, save1;
EditText edtA, edtB, edtC, edtD, edtE, edtF;
String tA;
int tB, tC, tD, tE, tF;
public static String filename = "MySharedString";
SharedPreferences abcPref;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.abc);
one2five = (Button) findViewById(R.id.btp1);
save1 = (Button) findViewById(R.id.btps1);
edtA = (EditText) findViewById(R.id.etA);
tA = edtA.getText().toString();
edtB = (EditText) findViewById(R.id.etB);
tB = edtB.getInputType();
edtC = (EditText) findViewById(R.id.etC);
tC = edtC.getInputType();
edtD = (EditText) findViewById(R.id.etD);
tD = edtD.getInputType();
edtE = (EditText) findViewById(R.id.etE);
tE = edtE.getInputType();
edtF = (EditText) findViewById(R.id.etF);
tF = edtF.getInputType();
one2five.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent openg2j = new Intent("com.sport.sport.G2J");
abcPref = PreferenceManager.getDefaultSharedPreferences(Abc.this);
abcPref.getInt("filename", 0);
startActivity(openg2j);
}
});
save1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
abcPref= getSharedPreferences(filename,0);
SharedPreferences.Editor editor = abcPref.edit();
editor.putString("field1Data", tA);
editor.putInt("field2Data", tB);
editor.putInt("field3Data", tC);
editor.putInt("field4Data", tD);
editor.putInt("field5Data", tE);
editor.commit();
}
});
}
}