android PreferenceScreen使用笔记


preference.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     >  
  4.     <Preference android:title="基本信息"  
  5.         android:layout="@layout/text_view"></Preference> <!--自定义layout-->  
  6.     <CheckBoxPreference android:key="checkbox"  
  7.         android:title="性别"  
  8.         android:summary="男 ,女"/>  
  9.     <RingtonePreference android:key="ringtone"  
  10.         android:title="Ringtone Preference"  
  11.         android:showDefault="true"  
  12.         android:showSilent="true"  
  13.         android:summary="Pick a tone, any tone"/>   
  14. <ListPreference android:summary="select a list"  
  15.     android:title="Type"  
  16.     android:entries="@array/my_array" <!--string-array-->  
  17.     android:entryValues="@array/my_array"  
  18.     android:key="list"/>  
  19. <EditTextPreference android:key="edit"  
  20.     android:dialogTitle="nihao"  
  21.     android:title="姓名"  
  22.     />  
  23.   
  24. </PreferenceScreen>  
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    >
    <Preference android:title="基本信息"
        android:layout="@layout/text_view"></Preference> <!--自定义layout-->
    <CheckBoxPreference android:key="checkbox"
        android:title="性别"
        android:summary="男 ,女"/>
    <RingtonePreference android:key="ringtone"
        android:title="Ringtone Preference"
        android:showDefault="true"
        android:showSilent="true"
        android:summary="Pick a tone, any tone"/> 
<ListPreference android:summary="select a list"
    android:title="Type"
    android:entries="@array/my_array" <!--string-array-->
    android:entryValues="@array/my_array"
    android:key="list"/>
<EditTextPreference android:key="edit"
    android:dialogTitle="nihao"
    android:title="姓名"
    />

</PreferenceScreen>

**Activity.java

  1. package com.lin.share;  
  2.   
  3. import android.content.SharedPreferences;  
  4. import android.os.Bundle;  
  5. import android.preference.EditTextPreference;  
  6. import android.preference.ListPreference;  
  7. import android.preference.Preference;  
  8. import android.preference.Preference.OnPreferenceChangeListener;  
  9. import android.preference.PreferenceActivity;  
  10. import android.preference.PreferenceManager;  
  11. import android.view.View;  
  12.   
  13. public class TestPreferenctScreenActivity extends PreferenceActivity {  
  14.     /** Called when the activity is first created. */  
  15.     ListPreference  list;  
  16.     SharedPreferences prefs;  
  17.     EditTextPreference editTextPreference;  
  18.       
  19.     @Override  
  20.     public void onCreate(Bundle savedInstanceState) {  
  21.         super.onCreate(savedInstanceState);  
  22.         addPreferencesFromResource(R.xml.preference);  
  23.        prefs=PreferenceManager.getDefaultSharedPreferences(this);  
  24.         list=(ListPreference)findPreference("list");  
  25.         editTextPreference=(EditTextPreference)findPreference("edit");  
  26.         editTextPreference.setSummary(prefs.getString("edit","default"));  
  27.           
  28.         editTextPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {  
  29.               
  30.             @Override  
  31.             public boolean onPreferenceChange(Preference preference, Object newValue) {  
  32.                 // TODO Auto-generated method stub   
  33.                 editTextPreference.setSummary(newValue.toString());  
  34.                 editTextPreference.setDefaultValue(newValue);  
  35.                 editTextPreference.setText(newValue.toString());  
  36.                 return false;  
  37.             }  
  38.         });  
  39.         list.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {  
  40.               
  41.             @Override  
  42.             public boolean onPreferenceChange(Preference preference, Object newValue) {  
  43.                 System.out.println("change"+newValue);  
  44.                 list.setSummary(newValue.toString());  
  45.                 list.setValue(newValue.toString());  
  46.                 return false;  
  47.             }  
  48.         });  
  49.     }  
  50.   
  51.       
  52. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值