Android 自訂Preference

來源:互聯網
上載者:User
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>

  

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.