zoukankan      html  css  js  c++  java
  • Android 自定义Preference

    CustomPreference
    package com.rasa.game;

    import android.content.Context;
    import android.content.SharedPreferences;
    import android.content.SharedPreferences.Editor;
    import android.content.res.TypedArray;
    import android.preference.Preference;
    import android.util.AttributeSet;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.TextView;

    public class CustomPreference extends Preference {
    private int mClickCounter = 100;
    private Button ok_button = null;

    // This is the constructor called by the inflater
    public CustomPreference(Context context, AttributeSet attrs) {
    super(context, attrs);
    setWidgetLayoutResource(R.layout.custom_preference);
    }

    @Override
    protected void onBindView(View view) {
    super.onBindView(view);

    // Set our custom views inside the layout
    final TextView myTextView = (TextView) view
    .findViewById(R.id.mypreference_widget);

    ok_button
    = (Button) view.findViewById(R.id.ok);
    ok_button.setOnClickListener(
    new OnClickListener() {

    @Override
    public void onClick(View v) {
    mClickCounter
    += 1;
    notifyChanged();
    // notify UI to refresh
    SharedPreferences sp = getSharedPreferences();
    Editor et
    = sp.edit();
    et.putInt(
    "mypreference_widget", mClickCounter);
    et.commit();
    }
    });
    if (myTextView != null) {
    SharedPreferences sp
    = getSharedPreferences();
    mClickCounter
    = sp.getInt("mypreference_widget", 200);
    myTextView.setText(String.valueOf(mClickCounter));
    }
    }

    @Override
    protected Object onGetDefaultValue(TypedArray a, int index) {
    return a.getInteger(index, 0);
    }

    @Override
    protected void onClick() {
    // TODO Auto-generated method stub
    int nKeep = mClickCounter + 1;
    callChangeListener(nKeep);
    // add lock
    mClickCounter = nKeep;
    persistInt(mClickCounter);
    notifyChanged();
    // notify UI to refresh
    super.onClick();
    }

    @Override
    protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
    if (restoreValue) {
    // Restore state
    mClickCounter = getPersistedInt(mClickCounter);
    }
    else {
    // Set state
    int value = (Integer) defaultValue;
    mClickCounter
    = value;
    persistInt(value);
    }

    }


    }

      

    custom_preference.xml
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id
    ="@+id/custompref" android:orientation="vertical"
    android:layout_width
    ="wrap_content" android:layout_height="wrap_content">
    <TextView android:id="@+id/mypreference_widget"
    android:layout_width
    ="wrap_content" android:layout_height="wrap_content"
    android:layout_gravity
    ="center_vertical" android:layout_marginRight="6sp"
    android:text
    ="Hello Kitty" android:focusable="false"
    android:clickable
    ="false" />
    <LinearLayout android:orientation="horizontal"
    android:layout_width
    ="wrap_content" android:layout_height="wrap_content">
    <Button android:id="@+id/ok" android:text="OK"
    android:layout_weight
    ="1.0" android:layout_width="wrap_content"
    android:layout_height
    ="wrap_content" />
    <Button android:id="@+id/cancel" android:text="cancel"
    android:layout_weight
    ="1.0" android:layout_width="wrap_content"
    android:layout_height
    ="wrap_content" />
    </LinearLayout>
    </LinearLayout>

      

    setting.xml
    <?xml version="1.0" encoding="utf-8"?>
    <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <CheckBoxPreference android:key="music"
    android:title
    ="@string/music_title" android:summary="@string/music_summary"
    android:defaultValue
    ="true" />
    <CheckBoxPreference android:key="hints"
    android:title
    ="@string/hints_title" android:summary="@string/hints_summary"
    android:defaultValue
    ="true" />
    <PreferenceScreen android:key="detailSetting"
    android:title
    ="@string/detail_setting_title" android:summary="@string/detail_setting_summary">
    <CheckBoxPreference android:key="music1"
    android:title
    ="@string/music_title" android:summary="@string/music_summary"
    android:defaultValue
    ="true" />
    <CheckBoxPreference android:key="music2"
    android:title
    ="@string/music_title" android:summary="@string/music_summary"
    android:defaultValue
    ="true" />
    </PreferenceScreen>
    <PreferenceScreen android:key="customSetting"
    android:title
    ="@string/custom_setting_title" android:summary="@string/custom_setting_summary">
    <intent android:action="android.intent.action.MAIN"
    android:targetPackage
    ="com.rasa.game" android:targetClass="com.rasa.game.CustomSetting" />
    </PreferenceScreen>
    <com.rasa.game.CustomPreference
    android:key="my_preference" android:title="Advance Preference"
    android:summary
    ="You can custom design your prefrence UI"
    android:defaultValue
    ="100" />
    </PreferenceScreen>

      

  • 相关阅读:
    Eclipse插件
    Android res文件夹下新建layout文件夹出错:invalid resource directory name
    Java笔记一:斐波那契数列
    Android应用的启动界面
    android短信系列之实现发送短信,并获得发送报告与接收报告
    转:android 使用html5作布局文件
    ubuntu10.10 全自动安装微软雅黑字体
    gcswf32.dll已停用
    Android连接真机之中兴
    在Servlet中连接Access
  • 原文地址:https://www.cnblogs.com/myparamita/p/2174425.html
Copyright © 2011-2022 走看看