EditTextPreference 下自定义弹出Dialog并获取
来源:互联网 发布:淘宝钻石展位要求 编辑:程序博客网 时间:2024/05/21 15:42
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() {@Overridepublic boolean onPreferenceClick(Preference preference) {// TODO Auto-generated method stubLog.d("config","clickListener");//点击Manually_Serverif(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
欢迎交流
- EditTextPreference 下自定义弹出Dialog并获取
- Android自定义Dialog实现弹出它的Activity背景变暗,并自定义Dialog的大小
- 自定义dialog弹出框
- 自定义dialog弹出框
- 底部弹出自定义Dialog
- 底部弹出自定义dialog
- 自定义dialog 弹出动画
- 自定义弹出框Dialog
- 【Android】快捷自定义Dialog对话框并获取属性
- 自定义dialog和弹出dialog的动画
- 自定义Dialog之底部弹出Dialog
- 自定义Dialog弹出软键盘
- android Dialog 自定义弹出层
- android 自定义弹出框Dialog
- Android自定义底部弹出Dialog
- 自定义Dialog 实现弹出窗口
- Android自定义dialog弹出布局
- Android自定义dialog弹出布局
- Dolphin Player编译
- C++ 虚函数表解析
- 内核日志:API 及实现
- 淘宝反作弊举报系统
- Path Sum 二叉树路径和 @LeetCode
- EditTextPreference 下自定义弹出Dialog并获取
- linux定时删除多余日志,以释放硬盘空间
- 三大框架整合ssh(一)-----持久层搭建
- leetcode Copy List with Random Pointer
- Bloom Filter原理及使用
- (2)Android开源项目介绍
- 图像主结构的提取方法
- 单点登录原理和java实现简单的单点登录
- j2ee ssh框架 页面url传参乱码问题