如何在preference.xml 窗口中创建RadioButton 组?

How to create RadioButton group in preference.xml window?(如何在preference.xml 窗口中创建RadioButton 组?)

本文介绍了如何在preference.xml 窗口中创建RadioButton 组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Java Android 开发的初学者.我正在使用 Eclipse SDK 3.6.1 版本.我有一个带有两个复选框和一个后退按钮的首选项窗口.

I'm beginner in Java Android developing. I'm using Eclipse SDK 3.6.1 version. I have a preferences window with two checkbox and one back button.

    <?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
             <PreferenceCategory
             android:title="PIN requirement">                
            <CheckBoxPreference                        
                    android:title="Use PIN" 
                    android:defaultValue="true"                                              
                    android:key="checkboxPref" /> 
           <CheckBoxPreference
                    android:title="Don't use PIN"
                    android:defaultValue="false"                        
                    android:key="checkboxPref2" />

                    </PreferenceCategory>

<PreferenceCategory>
    <Preference
            android:title="Back"                
            android:key="customPref" />
    </PreferenceCategory>
</PreferenceScreen>

如何将两个 CheckBox 更改为 RadioButton 组?

How to change two CheckBox in to the RadioButton group?

推荐答案

如果您只需要使用 PIN 启用或禁用,在这种情况下,只需一个 CheckBoxPreference 就足够了(参见下面的示例代码,第一个类别).当您需要从设置列表 (ListPreference) 中选择某些内容时,通常会使用 RadioButtons - 例如(参见示例代码,第二类)来选择颜色.

If you need just to enable or disable using PIN, only one CheckBoxPreference will be enough in this case (see example code below, First Category). RadioButtons are usually used, when you need to choose something from a list of settings (ListPreference) - for example (see example code, Second Category), to pick a color.

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <PreferenceCategory
        android:title="First Category">
        <CheckBoxPreference
            android:title="Using PIN"
            android:defaultValue="false"
            android:key="checkboxPref" 
            android:summaryOn="Disable PIN" 
            android:summaryOff="Enable PIN"/>
    </PreferenceCategory>

    <PreferenceCategory
        android:title="Second Category">
        <ListPreference
            android:title="Pick your favourite color"
            android:key="listPref"
            android:defaultValue="4"
            android:entries="@array/listArray"
            android:entryValues="@array/listValues" />
    </PreferenceCategory>
</PreferenceScreen>

这个例子的源代码是:

public class PreferencesHelpExample extends PreferenceActivity implements OnSharedPreferenceChangeListener {

    public static final String KEY_LIST_PREFERENCE = "listPref";

    private ListPreference mListPreference;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        addPreferencesFromResource(R.xml.preferences);

        // Get a reference to the preferences
        mListPreference = (ListPreference)getPreferenceScreen().findPreference(KEY_LIST_PREFERENCE);

    }

    @Override
    protected void onResume() {
        super.onResume();

        // Setup the initial values
        mListPreference.setSummary("Current value is " + mListPreference.getEntry().toString());

        // Set up a listener whenever a key changes            
        getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
    }

    @Override
    protected void onPause() {
        super.onPause();

        // Unregister the listener whenever a key changes            
        getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);    
    }

    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
        // Set new summary, when a preference value changes
        if (key.equals(KEY_LIST_PREFERENCE)) {
            mListPreference.setSummary("Current value is " + mListPreference.getEntry().toString()); 
        }
    }
}

对于 ListPreference,您还需要一个 arrays.xml 文件,该文件位于values"文件夹中:

For ListPreference you will also need an arrays.xml file, which is located in the "values" folder:

<?xml version="1.0" encoding="utf-8"?>

<resources>

    <string-array name="listArray">
        <item>red</item>
        <item>orange</item>
        <item>yellow</item>
        <item>green</item>
        <item>blue</item>
        <item>violet</item>
    </string-array>

    <string-array name="listValues">
        <item>1</item>
        <item>2</item>
        <item>3</item>
        <item>4</item>
        <item>5</item>
        <item>6</item>
    </string-array>

</resources>

还可以查看一些很棒的示例,使用 PreferenceActivity - 它们对我帮助很大:

See also some great examples, working with PreferenceActivity - they helped me a lot:

Android 偏好设置;
如何创建一组 RadioButtons 而不是一个列表;
如何在首选项摘要中显示 Android 首选项的当前值?

这篇关于如何在preference.xml 窗口中创建RadioButton 组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何在preference.xml 窗口中创建RadioButton 组?

基础教程推荐