码迷,mamicode.com
首页 > 移动开发 > 详细

android PreferenceScreen使用笔记

时间:2014-10-20 21:09:48      阅读:249      评论:0      收藏:0      [点我收藏+]

标签:android   http   io   os   ar   使用   java   div   on   

bubuko.com,布布扣


preference.xml

<?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

package com.lin.share;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.view.View;

public class TestPreferenctScreenActivity extends PreferenceActivity {
    /** Called when the activity is first created. */
    ListPreference  list;
    SharedPreferences prefs;
    EditTextPreference editTextPreference;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preference);
       prefs=PreferenceManager.getDefaultSharedPreferences(this);
        list=(ListPreference)findPreference("list");
        editTextPreference=(EditTextPreference)findPreference("edit");
        editTextPreference.setSummary(prefs.getString("edit","default"));
        
        editTextPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
			
			@Override
			public boolean onPreferenceChange(Preference preference, Object newValue) {
				// TODO Auto-generated method stub
				editTextPreference.setSummary(newValue.toString());
				editTextPreference.setDefaultValue(newValue);
				editTextPreference.setText(newValue.toString());
				return false;
			}
		});
        list.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
			
			@Override
			public boolean onPreferenceChange(Preference preference, Object newValue) {
				System.out.println("change"+newValue);
				list.setSummary(newValue.toString());
				list.setValue(newValue.toString());
				return false;
			}
		});
    }

	
}


android PreferenceScreen使用笔记

标签:android   http   io   os   ar   使用   java   div   on   

原文地址:http://www.cnblogs.com/lcchuguo/p/4038574.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!