我是非常新的android,我试图从我的自定义DialogPreference加载/持久化值。目前,这会失败,因为findViewById返回null。我(尝试)这样做的方式是否正确?如何在代码中访问我的EditText小部件?
public class AddressDialogPreference extends DialogPreference {
public AddressDialogPreference(Context context, AttributeSet attrs) {
super(context, attrs);
setDialogLayoutResource(R.layout.address_dialog);
}
@Override
protected void onBindDialogView(View view) {
EditText idField = (EditText) view.findViewById(R.id.hostID);
EditText ipField = (EditText) view.findViewById(R.id.hostIP);
SharedPreferences pref = getSharedPreferences();
idField.setText(pref.getString(getKey() + "_id","ExampleHostname"));
ipField.setText(pref.getString(getKey() + "_ip","192.168.1.1"));
super.onBindDialogView(view);
}
@Override
protected void onDialogClosed(boolean positiveResult) {
if(!positiveResult)
return;
Dialog myDial = getDialog();
EditText idField = (EditText) myDial.findViewById(R.id.hostID);
EditText ipField = (EditText) myDial.findViewById(R.id.hostIP);
SharedPreferences.Editor editor = getEditor();
editor.putString(getKey() + "_id",idField.getText().toString());
editor.putString(getKey() + "_ip",ipField.getText().toString());
}
address_dialog.xml:
<TextView
android:text="Insert IP address"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/hostIP" />
<TextView
android:text="Insert identifier"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/hostID" />
答案 0 :(得分:22)
好的,我自己发现了。好吧,我仍然不知道导致错误的原因,但是我对布局和代码做了很多更改,突然间它才起作用。我试图恢复到我在这里发布的代码,但我无法重现错误。我发布了我的工作代码,所以遇到这个问题的任何人都可以使用它。
管理员也可以选择删除此帖子,因为可能无法重现错误。
这是布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:text="Insert IP address"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/AddressBox" />
<TextView
android:text="Insert identifier"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/HostnameBox" />
</LinearLayout>
和AddressDialogPreference.java:
public class AddressDialogPreference extends DialogPreference {
private EditText ipBox;
private EditText hostBox;
public AddressDialogPreference(Context context, AttributeSet attrs) {
super(context, attrs);
setDialogLayoutResource(R.layout.address_dialog);
}
@Override
protected void onBindDialogView(View view) {
ipBox = (EditText) view.findViewById(R.id.AddressBox);
hostBox = (EditText) view.findViewById(R.id.HostnameBox);
SharedPreferences pref = getSharedPreferences();
hostBox.setText(pref.getString(getKey() + "_host","ExampleHostname"));
ipBox.setText(pref.getString(getKey() + "_ip","192.168.1.1"));
super.onBindDialogView(view);
}
@Override
protected void onDialogClosed(boolean positiveResult) {
if(!positiveResult)
return;
SharedPreferences.Editor editor = getEditor();
editor.putString(getKey() + "_host",hostBox.getText().toString());
editor.putString(getKey() + "_ip",ipBox.getText().toString());
editor.commit();
super.onDialogClosed(positiveResult);
}
}