Android 单击任何子视图时,父视图的选择器不起作用

Android 单击任何子视图时,父视图的选择器不起作用,android,android-layout,Android,Android Layout,选择器的XML为: <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@color/primary_light_transparent" android:state_enabled="true" android:state_pressed="true"/> <item android:drawable="@color/pr

选择器的XML为:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/primary_light_transparent" android:state_enabled="true" android:state_pressed="true"/>
    <item android:drawable="@color/primary_light_transparent" android:state_enabled="true" android:state_focused="true"/>
    <item android:drawable="@color/primary_light_transparent" android:state_enabled="true" android:state_selected="true"/>
    <item android:drawable="@color/primary_light_transparent" android:state_activated="true" android:state_enabled="true"/>
    <item android:drawable="@drawable/normal"/>
</selector>

布局XML为:

<FrameLayout
android:id="@+id/keypad_6"
     android:layout_width="0dp"
     android:layout_height="match_parent"
     android:layout_weight="1"
     android:background="@drawable/button_selector"
     android:descendantFocusability="beforeDescendants"
     android:onClick="onClick" >

     <Button
     android:id="@+id/keypad_6_bt"
     style="@style/Button.Keypad.Numeric"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:onClick="onClick"
     android:text="6" />

    <TextView
     android:id="@+id/keypad_6_tv"
     style="@style/Keypad_Letters"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:layout_gravity="bottom"
     android:gravity="center_horizontal"
     android:onClick="onClick"
     android:text="MNO"
     android:textColor="@color/keypad_digits_color" />
</FrameLayout>

我想要的是,当点击
FrameLayout
的子视图(按钮或文本视图)时,FrameLayout的选择器应该工作,并且应该突出显示整个布局

我无法做到这一点,希望知道如何才能做到这一点
android:addStatesFromChildren=“true”

到您的框架布局

当按下子对象时,焦点将转移到父对象,而不是父对象。因此,请忘记使用选择器执行此操作

我给你的建议是:你必须在你的
活动
片段
中以编程方式进行,方法是为父布局提供参考,并根据你从孩子那里听到的状态以编程方式更改其背景。比如说

FrameLayout parentLayout = findViewById(R.id.keypad_6);
Button button6 = (Button) findViewById(R.id.keypad_6_bt);
button.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
         if (MotionEvent.ACTION_DOWN == event.getAction()){
                parentLayout.setBackgroundColor(Color.BLACK);
            }else if (MotionEvent.ACTION_UP == event.getAction()){
                parentLayout.setBackgroundColor(Color.white);
            }
            return false;
        }
    });

酷。这正是我想要的。我们怎样才能对点击处理做同样的事情呢?你想要按钮/textview点击触发家长的onClick吗?是的。当前,当我单击子视图时,它不会触发父视图的单击侦听器。因此,我处理子视图也是出于同样的目的,我认为这是不好的。只需在子视图的
onClick
中调用
onClick(parenView)
;谢谢,但是@Archie.bpgc的答案是有效的,这正是我想要的。@ShajeelAfzal。。不客气。询问StackOverFlow的想法是获得不同的观点,并与所有用户分享您的问题:)对,但我这么说是因为您说:
您将无法做您想做的事。
:)@ShajeelAfzal。。实际上,Archie.bpgc答案对我来说也是一个有用的答案。这就是为什么我也投票支持他,并修改了我的答案。