天天看點

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之密碼專用輸入框