Android的切换preferences改变一起在preferenceActivity [英] Android SwitchPreferences change together in PreferenceActivity

查看:265
本文介绍了Android的切换preferences改变一起在preferenceActivity的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是开关preference在我的Andr​​oid应用程序,并发现了一些太奇怪了。有一个以上的开关preference在preference

当我使用preferenceActivity默认布局,一切都运行得那么好。但经过我设置自定义布局为preference活动,这些交换机开始变化起来,当你点击其中任何一个。我在一个基于ARM的平板电脑测试。此外,我测试它在我的Andr​​oid手机,它的工作原理只是让相同的。

它是如何发生的!

下面是我的自定义布局(<$ C C $> Setting.xml的)为preference:

 &LT; XML版本=1.0编码=UTF-8&GT?;
&LT; LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:方向=垂直&GT;

    &LT;的LinearLayout
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT&GT;

        &LT;按钮
            机器人:ID =@ + ID /按钮1
            机器人:layout_width =0dp
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_weight =1
            机器人:文本=按钮/&GT;

        &LT;按钮
            机器人:ID =@ + ID /按钮2
            机器人:layout_width =0dp
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_weight =1
            机器人:文本=按钮/&GT;
    &LT; / LinearLayout中&GT;

    &LT;的ListView
        机器人:ID =@机器人:ID /列表
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT&GT;
    &LT; / ListView控件&GT;

&LT; / LinearLayout中&GT;
 

和preferenceScreen

 &LT; preferenceScreen的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android&GT;
    &LT;开关preference
        机器人:关键=交换机1
        机器人:汇总=这是开关1
        机器人:标题=开关1/&GT;
    &LT;开关preference
        机器人:关键=开关2
        机器人:汇总=这是开关2
        机器人:标题=开关2/&GT;
&LT; / preferenceScreen&GT;
 

在code,我刚才设置的自定义布局

 公共类SettingsActivity扩展preferenceActivity {
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.setting);
    }

    @覆盖
    保护无效onPostCreate(包savedInstanceState){
        super.onPostCreate(savedInstanceState);

        setupSimple preferencesScreen();
    }

    私人无效setupSimple preferencesScreen(){
        加preferencesFromResource(R.xml pref_general。);
    }
}
 

这里是preference的截图,和两个开关总是什么都你点击他们每个人在同一时间发生变化。

解决方案

看来,交换机preference的 onBindView 将使用其他开关视图作为参数。<​​/ P>

下面是Android项目一错误问题在谷歌code。它的不可以这个问题,但相似,二者介绍切换视图将根据其他交换机的国家采取行动。

于是我找到解决问题的双向:

  1. 使用复选框preference 如果有多于一个布尔值preference在你的屏幕上。
  2. 使用此解决办法

     公共类CustomSwitch preference扩展交换机preference {
    
        / **
         *构造一个新的交换机preference与给定样式选项。
         *
         * @参数方面,将这种风格preference上下文
         *从默认的不同@param ATTRS样式属性
         *参数defStyle主题属性定义的默认样式选项
         * /
        公共CustomSwitch preference(上下文的背景下,ATTRS的AttributeSet,诠释defStyle){
            超(背景下,ATTRS,defStyle);
        }
    
        / **
         *构造一个新的交换机preference与给定样式选项。
         *
         * @参数方面,将这种风格preference上下文
         *从默认的不同@param ATTRS样式属性
         * /
        公共CustomSwitch preference(上下文的背景下,ATTRS的AttributeSet){
            超(背景下,ATTRS);
        }
    
        / **
         *构造一个新的交换机preference使用默认样式选项。
         *
         * @参数方面,将这种风格preference上下文
         * /
        公共CustomSwitch preference(上下文的背景下){
            超(背景下,NULL);
        }
    
        @覆盖
        保护无效onBindView(查看视图){
            之前调用切换preference.onBindView //清洁监听器
            ViewGroup中的ViewGroup =(ViewGroup中)查看;
            clearListenerInViewGroup(的ViewGroup);
            super.onBindView(视图);
        }
    
        / **
         *在切换为指定的ViewGroup清除监听器。
         *
         *参数的ViewGroup的ViewGroup中,将需要清除的听众。
         * /
        私人无效clearListenerInViewGroup(ViewGroup中的ViewGroup){
            如果(空==的ViewGroup){
                返回;
            }
    
            诠释计数= viewGroup.getChildCount();
            对于(INT N = 0; N&LT;计数; ++ N){
                查看childView = viewGroup.getChildAt(N);
                如果(childView的instanceof开关){
                    最后的开关SWITCHVIEW =(开关)childView;
                    switchView.setOnCheckedChangeListener(空);
                    返回;
                }否则,如果(childView的instanceof的ViewGroup){
                    ViewGroup中childGroup =(ViewGroup中)childView;
                    clearListenerInViewGroup(childGroup);
                }
            }
        }
    
    }
     

我已经测试了两种解决方案。他们的作品。

I'm using SwitchPreference in my Android app and found something so weird. There is more than one SwitchPreference in the preference.

When I use the default layout of PreferenceActivity, everything works so well. But after I set custom layout to the preference activity, those Switches begin change together when you click any of them. I'm testing it on a arm-based tablet. Also I test it on my Android phone, It works just so the same.

How does it happens!

Here is my custom layout (setting.xml) for preference:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/button1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Button" />

        <Button
            android:id="@+id/button2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Button" />
    </LinearLayout>

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>

and the PreferenceScreen

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
    <SwitchPreference
        android:key="switch1"
        android:summary="This is switch 1"
        android:title="Switch 1" />
    <SwitchPreference
        android:key="switch2"
        android:summary="This is switch 2"
        android:title="Switch 2" />
</PreferenceScreen>

In the code, I just set the custom layout

public class SettingsActivity extends PreferenceActivity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.setting);
    }

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

        setupSimplePreferencesScreen();
    }

    private void setupSimplePreferencesScreen() {
        addPreferencesFromResource(R.xml.pref_general);
    }
}

Here is the screenshot of the preference, and the two switch always change at the same time what ever you click each of them.

解决方案

It seems that SwitchPreference's onBindView will use other Switch view as the parameters.

Here is a bug issue in Android project in Google Code. It's NOT this problem, but similar, both describes switch views will act according to other switches' states.

So I found two way to solve the problem:

  1. Use CheckBoxPreference if there are more than one boolean preference in your screen.
  2. Use this workaround

    public class CustomSwitchPreference extends SwitchPreference {
    
        /**
         * Construct a new SwitchPreference with the given style options.
         *
         * @param context The Context that will style this preference
         * @param attrs Style attributes that differ from the default
         * @param defStyle Theme attribute defining the default style options
         */
        public CustomSwitchPreference(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
    
        /**
         * Construct a new SwitchPreference with the given style options.
         *
         * @param context The Context that will style this preference
         * @param attrs Style attributes that differ from the default
         */
        public CustomSwitchPreference(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        /**
         * Construct a new SwitchPreference with default style options.
         *
         * @param context The Context that will style this preference
         */
        public CustomSwitchPreference(Context context) {
            super(context, null);
        }
    
        @Override
        protected void onBindView(View view) {
            // Clean listener before invoke SwitchPreference.onBindView
            ViewGroup viewGroup= (ViewGroup)view;
            clearListenerInViewGroup(viewGroup);
            super.onBindView(view);
        }
    
        /**
         * Clear listener in Switch for specify ViewGroup.
         *
         * @param viewGroup The ViewGroup that will need to clear the listener.
         */
        private void clearListenerInViewGroup(ViewGroup viewGroup) {
            if (null == viewGroup) {
                return;
            }
    
            int count = viewGroup.getChildCount();
            for(int n = 0; n < count; ++n) {
                View childView = viewGroup.getChildAt(n);
                if(childView instanceof Switch) {
                    final Switch switchView = (Switch) childView;
                    switchView.setOnCheckedChangeListener(null);
                    return;
                } else if (childView instanceof ViewGroup){
                    ViewGroup childGroup = (ViewGroup)childView;
                    clearListenerInViewGroup(childGroup);
                }
            }
        }
    
    }
    

I've tested both of the two solutions. They works.

这篇关于Android的切换preferences改变一起在preferenceActivity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆