1

我在 LinearLayout 布局中使用了 cardview 和 material textview,如下所示

<LinearLayout
                        android:layout_marginTop="10dp"
                        android:layout_width="match_parent"
                        android:layout_height="50dp"
                        android:weightSum="2">

                        <com.google.android.material.card.MaterialCardView
                            android:id="@+id/card_date"
                            android:layout_width="0dp"
                            android:clickable="true"
                            android:layout_weight="1"
                            android:layout_marginEnd="2dp"
                            android:layout_height="match_parent"
                            android:clipToPadding="true"
                            app:cardCornerRadius="5dp"
                            android:focusable="true">

                        <com.google.android.material.textview.MaterialTextView
                            android:layout_weight="1"
                            android:id="@+id/editText_schedule_date"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:layout_gravity="center"
                            android:gravity="center"
                            android:background="@color/transparent"
                            android:text="@string/date_post"
                            android:focusableInTouchMode="false"
                            android:singleLine="true"
                            android:textColor="@color/textView_hint_upload"
                            android:theme="@style/editText_style"
                            android:textSize="16sp" />
                        </com.google.android.material.card.MaterialCardView>



                        <com.google.android.material.card.MaterialCardView
                            android:layout_weight="1"
                            android:layout_width="0dp"
                            android:layout_marginStart="2dp"
                            android:layout_height="match_parent"
                            android:clipToPadding="true"
                            app:cardCornerRadius="5dp">

                        <com.google.android.material.textfield.TextInputEditText

                            android:id="@+id/editText_schedule_time"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:layout_gravity="center"
                            android:layout_marginLeft="10dp"
                            android:layout_marginRight="10dp"
                            android:background="@color/transparent"
                            android:hint="@string/time_post"
                            android:gravity="center"
                            android:inputType="time"
                            android:singleLine="true"
                            android:textColor="@color/textView_upload"
                            android:textColorHint="@color/textView_hint_upload"
                            android:theme="@style/editText_style"
                            android:textSize="16sp" />
                        </com.google.android.material.card.MaterialCardView>
                    </LinearLayout>

我正在尝试在我的活动中使用它,如下所示

private MaterialCardView card_date;

然后像下面这样创建

card_date = findViewById(R.id.card_date);
card_date.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e("called","this card");
            }
        });

但它不起作用。我也尝试过 textview onclick 监听器,但这也不起作用。让我知道是否有人可以帮助我解决难题。谢谢!此 LinearLayout 之外的代码同样适用。谢谢!

4

3 回答 3

2

请删除android:clickable="true"

 <com.google.android.material.card.MaterialCardView
  android:id="@+id/card_date"
  android:layout_width="0dp"
  android:layout_weight="1"
  android:layout_marginEnd="2dp"
  android:layout_height="match_parent"
  android:clipToPadding="true"
  app:cardCornerRadius="5dp"/>

                 
于 2020-10-22T13:04:09.937 回答
0

你必须设置android:clickable="false"

<com.google.android.material.card.MaterialCardView
                        android:id="@+id/card_date"
                        android:layout_width="0dp"
                        android:clickable="false"/>
于 2020-10-22T14:39:32.793 回答
0

我曾经使用过

android:layout_weight="1"

在导致问题的textview中。我已将其删除并已解决。谢谢!

于 2020-10-23T02:40:34.973 回答