Android的EditInput组件必知必会

2022-02-02  本文已影响0人  SeekLife0

1、如何修改光标颜色。

在res/drawable下创建cursor_style.xml文件

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <size android:width="1dp" />
    <solid android:color="@color/black" />
</shape>

在属性中引入

android:textCursorDrawable="@drawable/cursor_style"

2、限制输入为小数且只能输入小数点后两位。

在属性中添加输入类型

android:inputType="numberDecimal"

在activity中进行设置

//输入小数监听
etArea.addTextChangedListener(object : TextWatcher {
         //找到小数点下标,如果不是小数或者小数点后没有第三位则正常输入,存在小数点后三位那么,把第三位删除。
          override fun afterTextChanged(edt: Editable) {
                val temp = edt.toString()
                val posDot = temp.indexOf(".")
                if (posDot <= 0) return
                if (temp.length - posDot - 1 > 2) {   //更改精确位数
                    edt.delete(posDot + 3, posDot + 4)  //根据精确位数进行变化
                }
            }

            override fun beforeTextChanged(arg0: CharSequence, arg1: Int, arg2: Int, arg3: Int) {}
            override fun onTextChanged(arg0: CharSequence, arg1: Int, arg2: Int, arg3: Int) {}
})

3、输入文字居中显示。

android:gravity="center"

4、去除输入框的下划线。

android:background="@null"

5、常见的邮编,电话号码,身份证验证。

    //手机号码验证,验证是否为11位数字
    fun toCheckPhone(text : kotlin.String) : Boolean{ //
        val regx = Regex("^1[0-9]{10}\$")
        return text.matches(regx)
    }

    //邮政编码正则,验证是否为5位数字
    fun checkPostCode(text : String) : Boolean {
        val regx = Regex("^[1-9]\\d{5}\$")
        return text.matches(regx)
    } 

    //身份证验证
    fun personIdValidation(text : String) : Boolean{
        val regx = Regex("[0-9]{17}[Xx]")
        val reg1 = Regex("[0-9]{15}")
        val regex = Regex("[0-9]{18}")
        return text.matches(regx) or  text.matches(reg1) or  text.matches(regex)
    }

6、常见布局

修改光标,去除下划线,输入文字居中,限制输入字符类型

                        <EditText
                        android:id="@+id/et_area"
                        android:layout_width="70dp"
                        android:layout_height="20dp"
                        android:layout_toRightOf="@id/tv_area"
                        android:background="@null"
                        android:gravity="center"
                        android:inputType="numberDecimal"
                        android:paddingHorizontal="2dp"
                        android:textCursorDrawable="@drawable/cursor_style"
                        android:textSize="14dp" />

7、设置字符输入个数

xxx.setFilters(new InputFilter[]{new InputFilter.LengthFilter(20)}); //限制字符输入为20

8、设置是否可以换行

android:inputType="textMultiLine"

9、以省略号的方式省略字符

android:ellipsize="end"

10、移动光标位置

String text = "hello world"; 
et.setText(text); 
et.setSelection(text.length() );

11、修改软键盘回车键文字

只能修改为固定的文字,必须设置singleLine=”true”,否则没有效果。

android:imeOptions="actionNext"

12、监听回车键事件

        etMoney.setOnKeyListener(object : View.OnKeyListener {
            override fun onKey(view: View?, keyCode: Int, keyEvent: KeyEvent?): Boolean {
                if (keyCode == KeyEvent.KEYCODE_ENTER) {
                    //业务代码
                }
                return false
            }
        })

13、隐藏系统软键盘

参考:https://www.jianshu.com/p/cc1882dfb7b8

editText.showSoftInputOnFocus = false

//输入框获取焦点回调时调用
private fun hideSoftKeyboard(activity: Activity) {
    val view: View? = activity.currentFocus
    if (view != null) {
      val inputMethodManager = activity.getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
      inputMethodManager.hideSoftInputFromWindow(
          view.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
    }
  }
上一篇 下一篇

猜你喜欢

热点阅读