1 /* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.inputmethod.latin.settings; 18 19 import android.content.Context; 20 import android.preference.Preference; 21 import android.util.AttributeSet; 22 import android.view.View; 23 import android.widget.RadioButton; 24 25 import com.android.inputmethod.latin.R; 26 27 /** 28 * Radio Button preference 29 */ 30 public class RadioButtonPreference extends Preference { 31 interface OnRadioButtonClickedListener { 32 /** 33 * Called when this preference needs to be saved its state. 34 * 35 * @param preference This preference. 36 */ onRadioButtonClicked(RadioButtonPreference preference)37 public void onRadioButtonClicked(RadioButtonPreference preference); 38 } 39 40 private boolean mIsSelected; 41 private RadioButton mRadioButton; 42 private OnRadioButtonClickedListener mListener; 43 private final View.OnClickListener mClickListener = new View.OnClickListener() { 44 @Override 45 public void onClick(final View v) { 46 callListenerOnRadioButtonClicked(); 47 } 48 }; 49 RadioButtonPreference(final Context context)50 public RadioButtonPreference(final Context context) { 51 this(context, null); 52 } 53 RadioButtonPreference(final Context context, final AttributeSet attrs)54 public RadioButtonPreference(final Context context, final AttributeSet attrs) { 55 this(context, attrs, android.R.attr.preferenceStyle); 56 } 57 RadioButtonPreference(final Context context, final AttributeSet attrs, final int defStyleAttr)58 public RadioButtonPreference(final Context context, final AttributeSet attrs, 59 final int defStyleAttr) { 60 super(context, attrs, defStyleAttr); 61 setWidgetLayoutResource(R.layout.radio_button_preference_widget); 62 } 63 setOnRadioButtonClickedListener(final OnRadioButtonClickedListener listener)64 public void setOnRadioButtonClickedListener(final OnRadioButtonClickedListener listener) { 65 mListener = listener; 66 } 67 callListenerOnRadioButtonClicked()68 void callListenerOnRadioButtonClicked() { 69 if (mListener != null) { 70 mListener.onRadioButtonClicked(this); 71 } 72 } 73 74 @Override onBindView(final View view)75 protected void onBindView(final View view) { 76 super.onBindView(view); 77 mRadioButton = (RadioButton)view.findViewById(R.id.radio_button); 78 mRadioButton.setChecked(mIsSelected); 79 mRadioButton.setOnClickListener(mClickListener); 80 view.setOnClickListener(mClickListener); 81 } 82 isSelected()83 public boolean isSelected() { 84 return mIsSelected; 85 } 86 setSelected(final boolean selected)87 public void setSelected(final boolean selected) { 88 if (selected == mIsSelected) { 89 return; 90 } 91 mIsSelected = selected; 92 if (mRadioButton != null) { 93 mRadioButton.setChecked(selected); 94 } 95 notifyChanged(); 96 } 97 } 98