How to make every content of a Relative Layout clickable in android

女生的网名这么多〃 提交于 2019-12-14 03:33:41

问题


I have a layout:

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="227dp"
        android:addStatesFromChildren="true"
        android:clickable="true"
        android:duplicateParentState="false"
        android:focusable="true"
        android:id ="@+id/relLay">

        <Gallery
            android:id = "@+id/gvImage"
            android:layout_height="200dp"
            android:layout_width="match_parent"
            android:paddingRight="5dp"
            android:paddingLeft="5dp"
            android:fadingEdgeLength="2dp"
            android:clickable="true"/>


    <TextView
        android:clickable="true"
        android:id="@+id/myImageViewText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/gvImage"
        android:layout_alignTop="@+id/gvImage"
        android:layout_alignRight="@+id/gvImage"
        android:gravity="center"
        android:text=SOMETHING"
        android:textAppearance="@android:style/TextAppearance.Large"
        android:textStyle="bold"
        android:textColor="#FF0000"
        android:layout_marginLeft="1dp"
        android:layout_marginRight="1dp"
        android:layout_marginTop="1dp"
        android:layout_alignParentBottom="true" />

        <ToggleButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/toggleButton"
            android:layout_marginTop="20dp"
            android:layout_marginRight="20dp"
            android:layout_alignParentTop="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:textOn=""
            android:textOff=""
            android:drawableTop="@drawable/favorite_check"
            android:gravity="center"
            android:background="@null"/>

        <TextView
            android:background="#CCCCCC"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Rate: ?"
            android:textColor="#FF0000"
            android:textStyle="bold"
            android:textSize="20dp"
            android:id="@+id/textView2"
            android:layout_alignBottom="@+id/gvImage"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />


    </RelativeLayout>

And it is called by a fragment class,

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.search_result, container, false);
        Gallery gallery = (Gallery)view.findViewById(R.id.gvImage);
        gallery.setAdapter(new ImageAdapter(getActivity().getApplicationContext()));

        
        return view;
    }

I want to do something like, if I click any item inside of RelativeLayout of that xml file it will do something like, will go to another class or Toast text. How will I do this?


回答1:


Implement onClickListener to each and every View.

Simplest way will be

<TextView
            android:background="#CCCCCC"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Rate: ?"
            android:textColor="#FF0000"
            android:textStyle="bold"
            android:textSize="20dp"
            android:id="@+id/textView2"
            android:layout_alignBottom="@+id/gvImage"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            onclick="executeTextViewClick" />

And in java class declare a function as follows.

public void executeTextViewClick(View view){

//Toast here or navigate to other activity 

}


来源:https://stackoverflow.com/questions/27836019/how-to-make-every-content-of-a-relative-layout-clickable-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!