简体   繁体   English

Android:onClick 使用 TextView 和 Button 的 LinearLayout

[英]Android: onClick on LinearLayout with TextView and Button

I have a Fragment that uses the following XML layout:我有一个使用以下 XML 布局的片段:

<?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:background="@drawable/card"
    android:clickable="true"
    android:onClick="editActions"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/title"
        style="@style/CardTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:duplicateParentState="true"
        android:text="@string/title_workstation" />

    <Button
        android:id="@+id/factory_button_edit"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:duplicateParentState="true"
        android:text="@string/label_edit" />

</LinearLayout>

As you can see, I have an onClick parameter set on LinearLayout .如您所见,我在LinearLayout设置了一个onClick参数。 Now on the TextView this one is triggered correctly, and on all empty area.现在在TextView上,这个被正确触发,并且在所有空白区域。 Just on the Button it doesn't invoke the onClick method that I set.仅在Button上,它不会调用我设置的 onClick 方法。

Is this normal?这是正常的吗? What do I have to do so that the onClick method is invoked everywhere on the LinearLayout ?我必须做什么才能在LinearLayout任何地方调用 onClick 方法?

This is what you need in your LinearLayout :这是您在 LinearLayout 中需要的:

android:onClick="editActions" 
android:clickable="true"

and also in your class which call that xml some method:并且还在您的类中调用该 xml 一些方法:

public void editActions(View view){

           //something TODO

    }

add onClick Action for your button dirrectly in code like this:直接在代码中为您的按钮添加 onClick 操作,如下所示:

Button btn = findViewById(R.id.factory_button_edit);
btn.setOnClickListener(button_click);

      OnClickListener button_click = new OnClickListener() {

        @Override
        public void onClick(View v) {
            editActions;
        }
    };

or xml add或 xml 添加

android:onClick="editActions"

to your button到你的按钮

I think you should use a TouchDelegate :我认为你应该使用TouchDelegate

Android provides the TouchDelegate class to make it possible for a parent to extend the touchable area of a child view beyond the child's bounds. Android 提供了 TouchDelegate 类,使父视图可以将子视图的可触摸区域扩展到子视图的边界之外。 This is useful when the child has to be small, but should have a larger touch region.当孩子必须很小但应该有更大的触摸区域时,这很有用。 You can also use this approach to shrink the child's touch region if need be.如果需要,您也可以使用这种方法缩小孩子的触摸区域。

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 .然后单击处理将上升parent

try to do that尝试这样做

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/card"
    android:clickable="true"
    android:onClick="editActions"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/title"
        style="@style/CardTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/title_workstation" />

    <Button
        android:id="@+id/factory_button_edit"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="clickMe"
        android:text="@string/label_edit" />

</LinearLayout>

and on your Activity or fragement以及您的 Activity 或 Fragment

    public void editActions(View v) {
        Toast.makeText(this, "Layout", Toast.LENGTH_SHORT).show();
    }

    public void clickMe(View v) {
        Toast.makeText(this, "clickMe", Toast.LENGTH_SHORT).show();
    }

Well, if this a bar with some children and you want to have only this bar clickable with children area - try add this option for every child in parent:好吧,如果这是一个有一些孩子的栏,而你只想让这个栏可以点击孩子区域 - 尝试为父级中的每个孩子添加这个选项:

android:clickable="false"

and of course add setOnClickListener for main layout, example on my blog当然,为主布局添加setOnClickListener ,例如我的博客

Example:示例:

<LinearLayout  
      android:id="@+id/parent"  
      android:layout_width="match_parent"  
      android:layout_height="wrap_content"  
      android:orientation="horizontal">  
      <ImageButton  
           android:clickable="false"  
           android:layout_width="40dp"  
           android:layout_height="40dp"  
           android:text="Cinema"  
           android:textSize="13sp"  
           android:layout_marginRight="10dp" />  
      <Button  
           android:clickable="false"  
           android:layout_width="match_parent"  
           android:layout_height="40dp"  
           android:text="Check in"  
           android:textSize="13sp"/>  
 </LinearLayout> 

我想你应该为你的 Button 删除这个参数:

android:duplicateParentState="true"
View.OnClickListener myViewHandler = new View.OnClickListener() {
        public void onClick(View v) {
                RelativeLayout pressedRelViewx = (RelativeLayout) v;
                TextView pressedTextView = (TextView) pressedRelViewx.getChildAt(0);
                String text = pressedTextView.getText().toString();
                Toast.makeText(getApplicationContext() , "My Text "+ text , Toast.LENGTH_LONG).show() ;
        }
    };


Relative_Layout = (RelativeLayout)findViewById(R.id.my_container);
Relative_Layout.setOnClickListener(myViewHandler);

ok this may be late , but it will help have others, who are looking for好的,这可能会迟到,但它会帮助正在寻找的其他人

how to set onClick Listener for layouts [android].如何为布局设置 onClick 侦听器 [android]。

LinearLayout mLinearLayout = findViewById(R.id.your_linear_layout_name);

mLinearLayout.setonClickListener(new View.onClickListener(){
   @Overide
   public void onClick(View v){
     //do want to do here

    }

});

Note:注意:

you can not perform onClick Listener on button when button is inside the linear layout which is already listening to onClick , and if do that first parent(Linear Layout) is get clicked.当按钮位于已经在侦听 onClick 的线性布局内时,您不能对按钮执行 onClick 侦听器,如果这样做,则单击第一个父级(线性布局)。 It is best practice to do not preform nested onClick Listeners.最好不要执行嵌套的 onClick 侦听器。

To eliminate copy-paste code, you can set a OnClickListener to all of the chidlren View 's with code:要消除复制粘贴代码,您可以将OnClickListener设置为所有带有代码的 chidlren View

private void setClickListenerRecursively(View view, View.OnClickListener listener){
    view.setOnClickListener(listener);

    if(view instanceof ViewGroup){
        ViewGroup viewGroup = (ViewGroup) view;
        for(int i = 0; i < viewGroup.getChildCount(); i++){
            View child = viewGroup.getChildAt(i);
            setClickListenerRecursively(child, listener);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM