android – 如何使用膨脹的佈局在自定義DialogPreference中訪問視窗小部件?
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" />
好的,我發現自己.那麼,我還是不知道是什麼導致的錯誤,但我做了很多的佈局和程式碼的變化,突然它只是工作.我試圖回到我在這裡釋出的程式碼,但是我無法重現錯誤.我釋出我的工作程式碼,所以任何人誰遇到這個問題,可能會使用它.
管理員也可以選擇刪除此帖子,因為可能無法重現錯誤.
這裡是佈局:
<?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); } }
程式碼日誌版權宣告:
翻譯自:http://stackoverflow.com/questions/5722531/how-to-access-widgets-in-a-custom-dialogpreference-with-a-inflated-layout