求助:XML布局转为Card View后,金额与付款方式控件无法编辑
Hey there! Let's troubleshoot why your editable fields (amount and payment method) stopped working after moving your layout into a CardView. I’ve got a few common fixes for you:
1. You’re Using TextView Instead of EditText
TextView isn’t designed for user input—even if you set android:editable="true", that property’s been deprecated and won’t work reliably. Switch to EditText instead, which is built specifically for editable text.
Here’s how to update your layout with CardView:
<androidx.cardview.widget.CardView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="16dp" app:cardCornerRadius="8dp" app:cardElevation="4dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="16dp"> <!-- Existing customer details --> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="客户名称" android:textStyle="bold"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="张三"/> <!-- Editable Amount Field --> <EditText android:id="@+id/et_amount" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="输入金额" android:inputType="numberDecimal" android:layout_marginTop="8dp"/> <!-- Editable Payment Method Field --> <EditText android:id="@+id/et_payment_method" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="输入付款方式" android:layout_marginTop="8dp"/> </LinearLayout> </androidx.cardview.widget.CardView>
2. CardView Is Blocking Focus/Touch Events
If your CardView has android:clickable="true" or android:focusable="true" set, it might be intercepting touch events meant for your EditTexts. To fix this, explicitly enable focus for your EditTexts:
<EditText android:id="@+id/et_amount" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="输入金额" android:inputType="numberDecimal" android:focusable="true" android:focusableInTouchMode="true" android:layout_marginTop="8dp"/>
3. Outdated CardView Library
Make sure you’re using the latest version of the CardView dependency in your module-level build.gradle file to avoid compatibility bugs:
implementation 'androidx.cardview:cardview:1.0.0'
- Replace TextView with EditText for editable fields—TextView isn’t meant for user input.
- Check CardView’s focus/click properties to ensure it doesn’t block child views.
- Update your CardView dependency to rule out library issues.
内容的提问来源于stack exchange,提问作者user9532704

