这是导致我出现问题的代码:
public class MainActivity extends Activity {
private TextView lblName;
private View.OnClickListener droidTapListener;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences prefs = getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);
boolean prefsShown = prefs.getBoolean("haveShownPrefs", false);
setContentView(R.layout.activity_main);
if (!prefsShown){
//launch the preferences activity
Intent i = new Intent(getApplicationContext(), Preferences.class);
startActivity(i);
}
init(prefs);
}
private void init(SharedPreferences prefs) {
lblName = (TextView) findViewById(R.id.lblName);
lblName.setText(prefs.getString("userName",""));
}
}
public class Preferences extends Activity {
private EditText txtName;
private Button btnDone;
private View.OnClickListener droidTapListener;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.preferences);
SharedPreferences prefs = getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);
SharedPreferences.Editor ed = prefs.edit();
ed.putBoolean("haveShownPrefs", true);
ed.commit();
init(prefs, ed);
}
private void init(SharedPreferences prefs, SharedPreferences.Editor ed) {
btnDone = (Button) findViewById(R.id.btnDone);
txtName = (EditText) findViewById(R.id.txtName);
ed.putString("userName", txtName.getText().toString());
ed.commit();
droidTapListener = new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), MainActivity.class);
startActivity(i);
}
};
btnDone.setOnClickListener(droidTapListener);
}
}
这是 XML,以防我遗漏一些东西:
首选项.xml:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Preferences">
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/name"
android:id="@+id/lblName"
android:layout_column="3"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="@+id/txtName"
android:layout_column="9"/>
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/done"
android:id="@+id/btnDone"
android:layout_column="10"/>
</TableRow>
</TableLayout>
活动主.xml:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Preferences">
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/lblName"
android:layout_column="5"/>
</TableRow>
</TableLayout>
问题是,在 MainActivity 上,当我在 lblName 上打印名称时它是空白的,我已经在 Prefereces 上进行了测试:
public void onClick(View v) {
btnDone.setText(txtName.getText().toString());
}
它工作正常,所以问题出在 SharedPreferences 上。有任何想法吗?提前致谢!