• android PreferenceScreen使用笔记



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


  • 相关阅读:
    I2C协议
    SPI
    学习高速PCB设计,这些走线方式你要知道! 高速射频百花潭 20220121 08:53
    ORCAD中,怎么一次性去掉所有元器件下面的下划线呢
    PCB中波长和真空中波长的关系
    I2S
    解决 Vue 重复点击相同路由,出现 Uncaught (in promise) NavigationDuplicated: Avoided redundant navigation 问题
    vue2搭建h5页面
    git提交代码
    vue报错解决方案
  • 原文地址:https://www.cnblogs.com/lcchuguo/p/4038574.html
Copyright © 2020-2023  润新知