EditTextPreference 下自定义弹出Dialog并获取

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>

将自定义的Dialog加入EditTextPrefernce



之后效果如下:



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));

本来以为可以对其中的控件进行获取,可是报错editText 是 Null,说明获取的并不是Dialog的布局ID

后来又发现了这样一个函数:
EditTextPreference.getDialog();

获取的值是Dialog类,Dialog类里面有个findviewbyid()函数,这不就是获取控件的方法吗

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 
欢迎交流
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值