> 文章列表 > Android开发 文本输入 EditText 监听器

Android开发 文本输入 EditText 监听器

Android开发 文本输入 EditText 监听器

1.EditText

文本编辑框:用户输入文本信息

可以输入的文本类型如下: 

常用属性:

 系统默认的EditText:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".EdittextActivity"android:orientation="vertical"><EditTextandroid:layout_width="match_parent"android:layout_height="wrap_content"android:hint="please enter user name"android:inputType="text"android:maxLength="20"android:textColorHint="@color/purple_200"></EditText><EditTextandroid:layout_width="match_parent"android:layout_height="wrap_content"android:hint="please enter password"android:inputType="numberPassword"android:maxLength="16"android:textColorHint="@color/teal_200"android:paddingTop="20dp"></EditText></LinearLayout>

效果图

当然也可以自定义EditText的背景,比如用selector设定获取焦点时EditText背景的变化。

selector:

其中focus和nofocus都是自定义的shape,分别表示聚焦和没有聚焦时的背景,如下图

focus shape: 

nofocus shape: 

xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".EdittextActivity"android:orientation="vertical"><EditTextandroid:layout_width="match_parent"android:layout_height="wrap_content"android:hint="default edit text"android:inputType="text"android:maxLength="20"></EditText><EditTextandroid:layout_width="match_parent"android:layout_height="wrap_content"android:hint=" edit text with no background"android:inputType="text"android:maxLength="20"android:background="@null"></EditText><EditTextandroid:layout_width="match_parent"android:layout_height="wrap_content"android:hint="use selector"android:inputType="text"android:maxLength="20"android:background="@drawable/edittext"></EditText></LinearLayout>

效果图:

2.焦点变更监听器

使用EditText时,可以在focus变更时触发事件,常用于检查EditText的内容或者长度。

注意这里是焦点变更,而不是点击事件,因为EditText点击一次触发的是焦点变更,第二次点击才会触发点击事件。

 xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".FocusActivity"android:orientation="vertical"><EditTextandroid:id="@+id/etPhone"android:layout_width="match_parent"android:layout_height="wrap_content"android:hint="Please enter 11-digit phone number "android:background="@drawable/edittext"android:maxLength="11"android:inputType="number"android:layout_marginTop="50dp"android:layout_marginBottom="30dp"android:textSize="20dp"></EditText><EditTextandroid:id="@+id/etPw"android:layout_width="match_parent"android:layout_height="wrap_content"android:hint="Please enter 8-digit password "android:background="@drawable/edittext"android:maxLength="8"android:inputType="numberPassword"android:layout_marginBottom="80dp"android:textSize="20dp"></EditText><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Login"android:textSize="30dp"android:layout_gravity="center"></Button></LinearLayout>

java:

package com.example.ch3;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;public class FocusActivity extends AppCompatActivity implements View.OnFocusChangeListener {private EditText etPhone;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_focus);etPhone = findViewById(R.id.etPhone);findViewById(R.id.etPw).setOnFocusChangeListener(this);}@Overridepublic void onFocusChange(View view, boolean b) {if(b){String phoneNum = etPhone.getText().toString();if(phoneNum.length()<11){etPhone.requestFocus();Toast.makeText(this, "Please enter 11-digit phone number", Toast.LENGTH_LONG).show();}}}
}

 效果图:

当焦点移向密码栏时,检查手机号码的长度,若小于11位,将焦点返回手机号码栏,弹出提示。

3.文本变化监听器

监听EditText的内容变化,触发响应的动作

 举例:当EditText的内容长度满足要求后,隐藏输入法窗口。

java代码:

package com.example.ch3;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;import com.example.ch3.until.UtilFunc;public class TextChangeActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_text_change);EditText et_Phone = findViewById(R.id.etPhone);EditText et_Pw = findViewById(R.id.etPw);et_Phone.addTextChangedListener(new HideTextWatcher(et_Phone, 11));et_Pw.addTextChangedListener(new HideTextWatcher(et_Pw, 8));}private class HideTextWatcher implements TextWatcher {private EditText et;private int maxL;public HideTextWatcher(EditText et, int maxL) {this.et = et;this.maxL = maxL;}@Overridepublic void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}@Overridepublic void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {}@Overridepublic void afterTextChanged(Editable editable) {String s = editable.toString();if(s.length()==maxL){UtilFunc.hideInput(TextChangeActivity.this, et);}}}
}

隐藏输入法的工具函数实现:

package com.example.ch3.until;import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;public class UtilFunc {public static void hideInput(Activity act, View v){InputMethodManager imm = (InputMethodManager)         act.getSystemService(Context.INPUT_METHOD_SERVICE);imm.hideSoftInputFromWindow(v.getWindowToken(),0);}}