我在通过共享首选项从两个活动访问String值时遇到问题。我想我会犯一些错误可以帮助我吗? 基本上我想从活动1和活动2得到的是使用编辑文本从用户获取整数值,将它们存储在共享首选项中(在应用程序重新启动时恢复)。 现在使用活动1和2的值我想在结果活动中执行计算。
我的问题: -
如何在结果活动中访问共享首选项?
值存储在字符串中我可以对其执行计算,还是必须在结果活动中将它们转换为int?如果是,如何将它们转换为int(Integer.valueOff将起作用?)
活动1
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();
}
});
}
}
活动2
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);
}
}
答案 0 :(得分:1)
在Abc
活动回复中
abcPref = getSharedPreferences("FILE1", 0);
与
abcPref = getSharedPreferences(FILE1, 0);
在G2J
活动回复中
abcPref = getSharedPreferences("FileP2", 0);
与
abcPref = getSharedPreferences(FileP2, 0);
在Results
活动定义
public static String FILE1= "MyPrefsFile";
要对字符串值执行caluclation,您可以解析字符串
int value = Integer.parseInt(abcPref.getString("tsG"));
等等
如果你只想接受editText中的整数值,那么在你的xcm add:
中 android:inputType="number"
<EditText
android:id="@+id/edtA"
android:inputType="number"
/>
答案 1 :(得分:0)
try this way
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);
int a = Integer.parseInt(abcPref.getString("tA", ""));
int b = Integer.parseInt(abcPref.getString("tsB", ""));
int c = Integer.parseInt(abcPref.getString("tsC", ""));
int d = Integer.parseInt(abcPref.getString("tsD", ""));
int e = Integer.parseInt(abcPref.getString("tsE", ""));
int f = Integer.parseInt(abcPref.getString("tsF", ""));
// Your operations
}
}
答案 2 :(得分:0)
我建议你在两个活动中使用getDefaultSharedPreferences()而不是getSharedPreferences()。这样就不存在使用首选项文件名称输入拼写错误的风险。
答案 3 :(得分:0)
abcPref.getString
,然后它会抛出NullPointerException
。您还可以使用Interger.parseInt(abcPref.getString("tA","" + 0));
执行将String解析为Integer等等...