【问题标题】:how to click on drawableRight on every TextInputEditText如何在每个 TextInputEditText 上单击 drawableRight
【发布时间】:2020-06-29 06:27:35
【问题描述】:

如何点击每个TextInputEditText的drawableRight编辑图像?我想单击编辑图标,然后启用编辑每个字段。我该怎么做?

 <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <!--First name-->

            <com.google.android.material.textfield.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="@dimen/_64dp">

                <com.google.android.material.textfield.TextInputEditText
                   
                    android:id="@+id/etFirstName"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:focusableInTouchMode="true"
                    android:drawableRight="@drawable/icon_edit"
                    android:hint="@string/label_fname"
                    android:inputType="textCapWords" />
            </com.google.android.material.textfield.TextInputLayout>

            <!--Middle name-->
            <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/activity_registration_tiMiddleName"
                android:layout_width="match_parent"
                android:layout_height="@dimen/_64dp">

                <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/etMiddleName"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:focusableInTouchMode="true"
                    android:drawableRight="@drawable/icon_edit"
                    android:hint="@string/label_mname"
                    android:inputType="textCapWords"/>

            </com.google.android.material.textfield.TextInputLayout>


     </LinearLayout>

那么如何使用 drawbleRight 单独编辑每个字段。请帮忙。

【问题讨论】:

    标签: android onclicklistener android-textinputlayout material-components-android android-textinputedittext


    【解决方案1】:

    不要在TextInputEditText 中使用android:drawableRight。
    使用:

    <com.google.android.material.textfield.TextInputLayout
        ...
        app:endIconMode="custom"
        app:endIconDrawable="@drawable/..."
    

    和:

    textInputLayout.setEndIconOnClickListener {
      // Respond to end icon presses
    }
    

    【讨论】:

    • 这是完美的解决方案。非常感谢@Gabrele Mariotti 先生
    • @Gabriele Mariotti 你能帮我解决这个问题吗stackoverflow.com/questions/62789319/…,因为它与你帮助我的这个答案有关。
    【解决方案2】:

    您可以为此覆盖 onTouch 函数。

     @Override
    public boolean onTouch(View view, MotionEvent event) {
        final int DRAWABLE_LEFT = 0;
        final int DRAWABLE_TOP = 1;
        final int DRAWABLE_RIGHT = 2;
        final int DRAWABLE_BOTTOM = 3;
    
        if(event.getAction() == MotionEvent.ACTION_UP) {
            if(event.getRawX() >= (etFirstName.getRight() - etFirstName.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {
                // your action here
                
          }
        return false;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-28
      • 1970-01-01
      • 2017-10-14
      • 1970-01-01
      • 2018-06-17
      • 2017-05-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多