【问题标题】:TextInputLayout password toggle while keep pressed按住 TextInputLayout 密码切换
【发布时间】:2018-04-11 11:10:36
【问题描述】:

我在密码字段的布局中使用了TextInputLayout。当我按下密码切换按钮时,一切正常,文件和密码正常切换。每当我单击密码切换按钮时,内容就会在普通模式和密码模式之间切换。

但我想知道是否可以将显示密码模式更改为按住切换密码按钮而不是单击。这意味着只要我按住密码切换按钮,我就会看到纯内容,当我释放按钮时,内容会变回密码模式。你知道这是否可能吗?

更新

如果你把它添加到你的布局中:

<android.support.design.widget.TextInputLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:hint="@string/pin_hint"
    app:passwordToggleEnabled="true"
    app:passwordToggleDrawable="@drawable/password_visibility_eye"
    app:hintEnabled="true"
    app:hintAnimationEnabled="true"
    app:errorEnabled="true">
    <android.support.design.widget.TextInputEditText
        android:id="@+id/password"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:imeOptions="actionDone"
        android:maxLength="16"/>
</android.support.design.widget.TextInputLayout>

并将此可绘制对象用作password_visibility_eye.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/ic_visibility_on" android:state_pressed="true" android:state_checked="true" />
    <item android:drawable="@drawable/ic_visibility_on" android:state_pressed="true" />
    <item android:drawable="@drawable/ic_visibility_on" android:state_checked="true" />
    <item android:drawable="@drawable/ic_visibility_off"  />
</selector>

您可以单击眼睛按钮并在布局编辑中在普通模式和密码模式之间切换(无需代码)。我喜欢将此行为更改为显示密码同时保持关闭密码

【问题讨论】:

    标签: android android-textinputlayout


    【解决方案1】:

    当然。代码一切皆有可能。

    只需执行以下操作:

    button.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, android.view.MotionEvent event) {
            if (event.equals(MotionEvent.ACTION_DOWN)) {
                // enable it
            }
    
            if (event.equals(MotionEvent.ACTION_UP)) {
                // disable it
            }
            return false;
        }
    });
    

    【讨论】:

      【解决方案2】:

      您可以在切换按钮上使用 OnTouchListener。

        // this goes wherever you setup your button listener:
        button.setOnTouchListener(new OnTouchListener() {
           @Override
           public boolean onTouch(View v, MotionEvent event) {
              if(event.getAction() == MotionEvent.ACTION_DOWN) {
                 //here your onPressed state
              } else if (event.getAction() == MotionEvent.ACTION_UP) {
                 //here your onReleased state
              }
      
              return true;
           }
        });
      

      【讨论】:

      • TextInputLayout 自动切换切换状态。假设此代码将阻止此行为,我需要如何更改其切换按钮?
      • 更新并添加了布局。
      • 对我来说,它返回 ActionEvent Down 和 After。如何避免?
      猜你喜欢
      • 2017-07-26
      • 1970-01-01
      • 1970-01-01
      • 2019-04-16
      • 1970-01-01
      • 2020-05-19
      • 1970-01-01
      • 2017-04-27
      • 2018-09-03
      相关资源
      最近更新 更多