我在通过共享首选项从两个活动中访问字符串值时遇到问题。我想我犯了一些错误,任何人都可以帮助我解决这个问题吗?基本上我想从活动 1 和活动 2 中获取使用编辑文本的用户的整数值,将它们存储在共享首选项中(在应用程序重新启动时恢复)。现在使用活动 1 和 2 的值,我想在结果活动中执行计算。
我的问题:-
如何访问结果活动中的共享偏好?
值存储在字符串中我可以对其执行计算还是必须在结果活动中将它们转换为 int?如果是,如何将它们转换为 int(Integer.valueOff 会起作用吗?)
活动一
public class Abc extends Activity {
Button one2five, save1;
EditText edtA, edtB, edtC, edtD, edtE, edtF;
String tA, tsB, tsC, tsD, tsE, tsF;
int tB, tC, tD, tE, tF;
public static String FILE1 = "MyPrefsFile";
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);
edtB = (EditText) findViewById(R.id.etB);
edtC = (EditText) findViewById(R.id.etC);
edtD = (EditText) findViewById(R.id.etD);
edtE = (EditText) findViewById(R.id.etE);
edtF = (EditText) findViewById(R.id.etF);
abcPref = getSharedPreferences("FILE1", 0);
edtA.setText(abcPref.getString("tA", ""));
edtB.setText(abcPref.getString("tsB", ""));
edtC.setText(abcPref.getString("tsC", ""));
edtD.setText(abcPref.getString("tsD", ""));
edtE.setText(abcPref.getString("tsE", ""));
edtF.setText(abcPref.getString("tsF", ""));
one2five.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if ((!edtA.getText().toString().equals(""))
&& (!edtB.getText().toString().equals(""))
&& (!edtC.getText().toString().equals(""))
&& (!edtD.getText().toString().equals(""))
&& (!edtE.getText().toString().equals(""))
&& (!edtF.getText().toString().equals(""))) {
Intent openg2j = new Intent("com.sports.sport.G2J");
startActivity(openg2j);
}
}
});
save1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
abcPref = getSharedPreferences("FILE1", 0);
SharedPreferences.Editor editor = abcPref.edit();
editor.putString("tA", edtA.getText().toString());
editor.putString("tsB", edtB.getText().toString());
editor.putString("tsC", edtC.getText().toString());
editor.putString("tsD", edtD.getText().toString());
editor.putString("tsE", edtE.getText().toString());
editor.putString("tsF", edtF.getText().toString());
editor.commit();
}
});
}
}
活动二
public class G2J extends Activity {
Button two2five, save2;
EditText edtG, edtH, edtI, edtJ, edtK;
int tG, tH, tI, tJ, tK;
String tsG, tsH, tsI, tsJ, tsK;
public static String FileP2 = "MyPrefsFile";
SharedPreferences abcPref;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.g2j);
two2five = (Button) findViewById(R.id.btp2);
save2 = (Button) findViewById(R.id.btps2);
edtG = (EditText) findViewById(R.id.etG);
edtH = (EditText) findViewById(R.id.etH);
edtI = (EditText) findViewById(R.id.etI);
edtJ = (EditText) findViewById(R.id.etJ);
edtK = (EditText) findViewById(R.id.etK);
abcPref = getSharedPreferences("FileP2", 0);
edtG.setText(abcPref.getString("tsG", ""));
edtH.setText(abcPref.getString("tsH", ""));
edtI.setText(abcPref.getString("tsI", ""));
edtJ.setText(abcPref.getString("tsJ", ""));
edtK.setText(abcPref.getString("tsK", ""));
two2five.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if ((!edtG.getText().toString().equals(""))
&& (!edtH.getText().toString().equals(""))
&& (!edtI.getText().toString().equals(""))
&& (!edtJ.getText().toString().equals(""))
&& (!edtK.getText().toString().equals(""))) {
// TODO Auto-generated method stub
Intent openl2p = new Intent("com.sports.sport.Results");
startActivity(openl2p);
}
}
});
save2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
abcPref = G2J.this.getSharedPreferences(FileP2, 0);
SharedPreferences.Editor editor = abcPref.edit();
editor.putString("tsG", edtG.getText().toString());
editor.putString("tsH", edtH.getText().toString());
editor.putString("tsI", edtI.getText().toString());
editor.putString("tsJ", edtJ.getText().toString());
editor.putString("tsK", edtK.getText().toString());
editor.commit();
}
});
}
}
最终结果活动
public class Results extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.resultmain);
SharedPreferences abcPref= this.getSharedPreferences(FILE1,0);
}
}