onClick not triggered on LinearLayout with child

后端 未结 15 1065
生来不讨喜
生来不讨喜 2020-11-29 03:33

I\'ve got a custom LinearLayout with a smaller TextView child. I\'d like to be able to click the area not covered by the TextView, so I set clickable=true and an onclicklist

相关标签:
15条回答
  • 2020-11-29 03:46

    Create a variable for LinearLayout

    LinearLayout layout;
    layout.setOnClicknersetOnClickListener(new View.OnClickListener()
            {
                public void onClick(View v)
                {                   
                    Toast t = Toast.makeText(context, "hey!", Toast.LENGTH_SHORT);
                    t.show();
                }
            });
    
    0 讨论(0)
  • 2020-11-29 03:48

    android:duplicateParentState="true" did not help me.

    To make your layout clickable with its children you need add this option for every child:

     android:clickable="false"
    

    Then click handling will go up to parent.

    0 讨论(0)
  • 2020-11-29 03:50

    Your TextView height covers the whole parent (whole layout) so you might clicking on empty space but not on the layout. Try using wrap_content for android:layout_height for your TextView. Set click listener for the layout as well.

    0 讨论(0)
  • 2020-11-29 03:50

    None of the solutions above forked for me. Than i noticed that a LinkMovementMethod set to TextView inside my LinearLayout. This class intercepts touch events, click events and properties of TextView such as clickable, focusable vs..

    0 讨论(0)
  • 2020-11-29 03:54

    If the views in question are TextViews, you may need to set them as focusable="false" so that the first click isn't used focusing on the text view.

    0 讨论(0)
  • 2020-11-29 03:57

    Make Your parent LinearLayout's android:clickable="true"

    Make all of the the childview's android:clickable="false"

    Under Linearlayout - Remove android:inputType="" from TextView

    0 讨论(0)
提交回复
热议问题