Android中两个LinearLayout的相同onClick方法

时间:2018-07-11 12:18:36

标签: java android android-linearlayout

我有两个LinearLayout,如下:

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:background="@drawable/border"
        android:orientation="horizontal"
        android:padding="10dp"
        android:weightSum="2"
        android:onClick="viewTipsDetail">

        <ImageView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            android:src="@mipmap/img_breakfast" />

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1.5"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Đừng bỏ bữa sáng"
                android:textSize="18sp"
                android:textStyle="bold" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:singleLine="false"
                android:text="Các nghiên cứu cho thấy bữa sáng là một những điê"
                android:textSize="18sp" />
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:background="@drawable/border"
        android:orientation="horizontal"
        android:padding="10dp"
        android:weightSum="2"
        android:onClick="viewTipsDetail">

        <ImageView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            android:src="@mipmap/img_waterglass" />

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1.5"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Uống đủ nước"
                android:textSize="18sp"
                android:textStyle="bold" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity=""
                android:singleLine="false"
                android:text="Bổ sung đủ nước cho cơ thể, ngay cả các bạn làm việc trong môi trường máy lạnh hoặc ngoài trời. Việc mất nước có thể khiến bạn căng thẳng, khó tập trung vào công việc"
                android:textSize="18sp" />
        </LinearLayout>
    </LinearLayout>

我打算针对两者使用相同的方法来实现onClickEvent。在这种情况下,事件将调用viewTipsDetail()方法。那么,如何仅使用一种方法来获取图像src(特定于LinearLayout中的文本)呢?有没有可能以给定的详细信息自动生成LinearLayout这样的方法?

这是示例,我将使用更多的LinearLayout,因此,我一般需要一种可能的方法来获取特定LinearLayout中的详细信息。

1 个答案:

答案 0 :(得分:1)

<LinearLayout
        id="@+id/layout1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1.5"
        onClick="onLayoutClick"
        android:orientation="vertical">

<LinearLayout
        id="@+id/layout2"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1.5"
        onClick="onLayoutClick"
        android:orientation="vertical">

在代码中:

@Override
public void onLayoutClick(View view) {
    // click click
    // view.getTag()
}

或者只是使用列表视图来实现...