1自定义Dialog布局并加入EditTextPrefernce
Preference的布局文件
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
android:title=" ">
<PreferenceCategory
xmlns:android="http://schemas.android.com/apk/res/android"
android:title="Exit"
android:summary="settings about exit">
<CheckBoxPreference
android:title="Completely exit?"
android:summaryOn="Yes,Activity Completely exit."
android:summaryOff="No,Service run."
android:defaultValue="true"
android:key="@string/Config_Exit">
</CheckBoxPreference>
</PreferenceCategory>
<PreferenceCategory
xmlns:android="http://schemas.android.com/apk/res/android"
android:title="Server"
android:summary="settings about Server">
<EditTextPreference
android:positiveButtonText="Positive"
android:negativeButtonText="Negative"
android:dialogLayout="@layout/config_dialog_server"
android:dialogTitle="@string/Manually_Server"
android:key="@string/Config_Manually_Server"
android:title="@string/Manually_Push_Server"/>
<EditTextPreference
android:dialogTitle="@string/Auto_Server"
android:dialogLayout="@layout/config_loading"
android:negativeButtonText="Negative"
android:positiveButtonText="Positive"
android:key="@string/Config_Auto_Server"
android:title="@string/Auto_Server"/>
</PreferenceCategory>
<PreferenceCategory
xmlns:android="http://schemas.android.com/apk/res/android"
android:title="Push Server"
android:summary="settings about Push Server">
<EditTextPreference
android:positiveButtonText="Positive"
android:negativeButtonText="Negative"
android:dialogLayout="@layout/config_dialog_server"
android:dialogTitle="@string/Manually_Push_Server"
android:key="@string/Config_Manually_Push_Server" android:title="@string/Manually_Push_Server"/>
<EditTextPreference
android:dialogTitle="@string/Auto_Push_Server"
android:dialogLayout="@layout/config_loading"
android:negativeButtonText="Negative"
android:positiveButtonText="Positive"
android:key="@string/Config_Auto_Push_Server"
android:title="@string/Auto_Push_Server"/>
</PreferenceCategory>
</PreferenceScreen>
自定义Dialog的布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#ffffff">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center"
android:layout_marginTop="20dp">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Ip"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:layout_marginLeft="22dp"
android:inputType="number|text" >
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:layout_marginBottom="20dp">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Port"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" >
</EditText>
</LinearLayout>
</LinearLayout>
之后效果如下:
2在Activity中初始化并设定
addPreferencesFromResource(R.xml.config);
EditTextPreference manually_server;
ListPreference auto_server;
auto_server=(ListPreference)findPreference(getResources().getString(R.string.Config_Auto_Server));
manually_server=(EditTextPreference)findPreference(getResources().getString(R.string.Config_Manually_Server));
在其中,获取控件的方式不是通过findviewbyid的形式,而是findPreference的形式,在xml中定义了Key,就是寻找这个Key
config_exit.setOnPreferenceChangeListener(changeListener);
manually_server.setOnPreferenceClickListener(clickListener);
设置监听事件,OnPreferenceChangeListener是状态改变事件,如果你在对话框点击了确定,就会产生这个事件
另外一个是点击事件
3定义响应事件
因为我们的Dialog是自定义的,所以我们要找到自定义Dialog的Layout,这样才能对其中的控件进行操作
在EditTextPreference中找到了一个获取Dialog布局ID的函数,返回值是Int型的ID号
manually_server.getDialogLayoutResource();
LinearLayout layout=(LinearLayout)findViewById(manually_server.getDialogLayoutResource());
EditText editText1=(EditText)layout.findViewById(R.id.editText1);
editText1.setText(ConfigDao.GetIP(context));
后来又发现了这样一个函数:
EditTextPreference.getDialog();
OnPreferenceClickListener clickListener=new OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
// TODO Auto-generated method stub
Log.d("config","clickListener");
//点击Manually_Server
if(preference.getKey().equals(getResources().getString(R.string.Config_Manually_Server)))
{
Dialog dialog=((EditTextPreference)preference).getDialog();
EditText editText1=(EditText)dialog.findViewById(R.id.editText1);
editText1.setText(ConfigDao.GetIP(context));
EditText editText2=(EditText)dialog.findViewById(R.id.editText2);
editText2.setText(""+ConfigDao.GetPort(context));
}
return false;
}
};
QQ:305268748
欢迎交流