Android custom preference

Source: Internet
Author: 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>

  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.