天天看点

autojs之密码专用输入框

使用场景

密码输入框

效果展示

autojs之密码专用输入框

autojs版本

autojs之密码专用输入框

TextInputLayout

TextInputLayout 主要是作为 EditText 的容器,从而为 EditText 生成一个浮动的 Label,当用户点击 EditText 的时候,EditText 中的 hint 字符串会自动移到 EditText 的左上角。

代码讲解

1. 布局, 两个输入框
ui.layout(
  <vertical margin="10">
    <com.google.android.material.textfield.TextInputLayout
      android:id="@+id/userInputLayout"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
    >
      <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="22sp" />
    </com.google.android.material.textfield.TextInputLayout>
    <input w="*"></input>
  </vertical>
);
           
2. 设置TextInputLayout属性
// 开启错误提示
ui.userInputLayout.setErrorEnabled(true);
// 开启计数
ui.userInputLayout.setCounterEnabled(true);
// 输入最大长度
ui.userInputLayout.setCounterMaxLength(10);
// 浮动标签文本
ui.userInputLayout.setHint("password");
// 启用或禁用密码可见性切换功能
ui.userInputLayout.setPasswordVisibilityToggleEnabled(true);
           
3. 设置输入框监听, 提醒用户错误原因
ui.userInputLayout.getEditText().addTextChangedListener(
  new android.text.TextWatcher({
    afterTextChanged: function (s) {
      if (ui.userInputLayout.getEditText().getText().toString().trim().length > 10) {
        ui.userInputLayout.setError("用户名长度超出限制");
      } else {
        ui.userInputLayout.setError(null);
      }
    },
  })
);
           

参考文章

1. Android Material Design 系列之 TextInputLayout 使用详解
2. 谷歌官网TextInputLayout

声明

部分内容来自网络

微信公众号 AutoJsPro教程

autojs之密码专用输入框

QQ群

747748653

autojs之密码专用输入框