我試圖保存用戶的日誌在下面Settings.java
屏幕的sharedPreference
,將由TestFragment2.java
讀取,從另一FragmentActivity稱爲輸入的名稱,SampleTabsDefault.java
閱讀SharedPreferences中的片段數據,內FragmentActivity
Settings.java
private void savePrefs(String key, boolean value) {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
Editor edit = sp.edit();
edit.putBoolean(key, value);
edit.commit();
}
private void savePrefs(String key, String value) {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
Editor edit = sp.edit();
edit.putString(key, value);
edit.commit();
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
savePrefs("CHECKBOX", cb.isChecked());
if (cb.isChecked())
savePrefs("NAME", et.getText().toString());
Intent myIntent = new Intent("com.test.SAMPLETABSDEFAULT");
startActivity(myIntent);
finish();
}
}
Intent myIntent = new Intent("com.test.SAMPLETABSDEFAULT");startActivity(myIntent);
負荷SampleTabsDefault.java
就好了,所以做所有的FRAG它呼籲。
class NavTabs extends FragmentPagerAdapter {
public NavTabs(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch(position)
{
case 0:
TestFragment fragment = new TestFragment();
return fragment;
case 1:
TestFragment2 fragment2 = new TestFragment2();
return fragment2;
}
TestFragment3 fragment3 = new TestFragment3();
return fragment3;
}
我可以滾動左,右,並得到以下TestFragment2.java看到片段....但其不讀書,或IM使應用sharedPreferences
內TestFragment2.java
TestFragment2.java很笨蛋舉動
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View myView = inflater.inflate(R.layout.fragment_2, container, false);
text = (TextView)myView.findViewById(R.id.textView1);
et = (TextView)myView.findViewById(R.id.textView3);
cb = (CheckBox) myView.findViewById(R.id.checkBox1);
loadPrefs();
return myView;
}
private void loadPrefs() {
SharedPreferences sp = getActivity().getSharedPreferences("CHECKBOX", 0);
boolean cbValue = sp.getBoolean("CHECKBOX", false);
String name = sp.getString("NAME", "YourName");
if(cbValue){
cb.setChecked(true);
}else{
cb.setChecked(false);
}
et.setText(name);
}
在TestFragment2.java
進出口試圖讀取並通過適用於textView
'的setText();' sharedPreferences和我在Settings.java
上做的一樣。在Settings.java
的代碼不工作,String name = sp.getString("NAME", "YourName");
它加載sharedPreference
並顯示最後一次申請是跑了輸入的用戶名,但在TestFragmnet2.java
相同的代碼塊從String name = sp.getString("NAME", "YourName");
任何幫助返回YOURNAME非常感謝,謝謝! !
正確的!就是這樣..謝謝! – user1446988 2013-02-22 22:50:12