Introduction to Android Studio App development: Explanation and use of text input EditText (source code attached includes edit box, focus change listener, and text change listener)

If you have any running problems or need the source code, please like, follow and leave a message in the comment area after collecting it~~~

1. Edit box EditText

The edit box EditText is used to receive text input from the soft keyboard, such as user name, password, evaluation content, etc. It is derived from the text view. The properties and methods are as shown below

 Next, watch the editing box interface effect through XML layout

When the specified number of digits has been entered, it cannot be entered, and when selected, it can be underlined and highlighted to look more beautiful.

 The EditSimpleActivity class code is as follows

package com.example.chapter05;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

public class EditSimpleActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_simple);
    }

}

activity_edit_shapeXML file code is as follows

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="5dp"
    android:orientation="vertical" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:text="下面是登录信息"
        android:textColor="@color/black"
        android:textSize="17sp" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="text"
        android:maxLength="10"
        android:hint="请输入用户名"
        android:textColor="@color/black"
        android:textSize="17sp" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:maxLength="8"
        android:hint="请输入密码"
        android:textColor="@color/black"
        android:textSize="17sp" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="下面是手机信息"
        android:textColor="@color/black"
        android:textSize="17sp" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:maxLength="11"
        android:hint="请输入11位手机号码"
        android:textColor="@color/black"
        android:textSize="17sp" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="numberPassword"
        android:maxLength="6"
        android:hint="请输入6位服务密码"
        android:textColor="@color/black"
        android:textSize="17sp" />

</LinearLayout>

Also for the sake of beauty, we can adjust the input box to a rounded rectangle. The effect is as follows

 Part of the code is as follows

package com.example.chapter05;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

public class EditBorderActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_border);
    }

}

The XML file is as follows

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="5dp"
    android:orientation="vertical" >

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="text"
        android:hint="这是默认边框"
        android:textColor="@color/black"
        android:textSize="17sp" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:inputType="text"
        android:hint="我的边框不见了"
        android:background="@null"
        android:textColor="@color/black"
        android:textSize="17sp" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:inputType="text"
        android:hint="我的边框是圆角"
        android:background="@drawable/editext_selector"
        android:textColor="@color/black"
        android:textSize="17sp" />

</LinearLayout>

2. Focus change listener

Although the edit box provides the maximum length of the input text, it does not provide the minimum length, so a focus change listener is needed to detect it at this time. The focus change event will be triggered when clicked, and the focus change event can be triggered when the cursor switches. The effect is as follows

When the entered username does not meet the specified number of digits and you click on the password box, you will be prompted below to enter your mobile phone number, which can activate the automatic detection of whether the input is legal.

 The EditFocusActivity class code is as follows

package com.example.chapter05;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class EditFocusActivity extends AppCompatActivity implements View.OnClickListener, View.OnFocusChangeListener {
    private EditText et_phone; // 声明一个编辑框对象

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_focus);
        // 从布局文件中获取名叫et_phone的手机号码编辑框
        et_phone = findViewById(R.id.et_phone);
        // 从布局文件中获取名叫et_password的密码编辑框
        EditText et_password = findViewById(R.id.et_password);
        // 给密码编辑框注册点击事件监听器
        et_password.setOnClickListener(this);
        // 给密码编辑框注册一个焦点变化监听器,一旦焦点发生变化,就触发监听器的onFocusChange方法
        et_password.setOnFocusChangeListener(this);
    }

    // 焦点变更事件的处理方法,hasFocus表示当前控件是否获得焦点。
    // 为什么光标进入事件不选onClick?因为要点两下才会触发onClick动作(第一下是切换焦点动作)
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        // 判断密码编辑框是否获得焦点。hasFocus为true表示获得焦点,为false表示失去焦点
        if (v.getId()==R.id.et_password && hasFocus) {
            String phone = et_phone.getText().toString();
            if (TextUtils.isEmpty(phone) || phone.length()<11) { // 手机号码不足11位
                // 手机号码编辑框请求焦点,也就是把光标移回手机号码编辑框
                et_phone.requestFocus();
                Toast.makeText(this, "请输入11位手机号码", Toast.LENGTH_SHORT).show();
            }
        }
    }

    @Override
    public void onClick(View v) {
        // 编辑框比较特殊,要点击两次后才会触发点击事件,因为第一次点击只触发焦点变更事件,第二次点击才触发点击事件
        if (v.getId() == R.id.et_password) {
            String phone = et_phone.getText().toString();
            if (TextUtils.isEmpty(phone) || phone.length()<11) { // 手机号码不足11位
                // 手机号码编辑框请求焦点,也就是把光标移回手机号码编辑框
                et_phone.requestFocus();
                Toast.makeText(this, "请输入11位手机号码", Toast.LENGTH_SHORT).show();
            }
        }
    }
}

activity_edit_focusXML file code is as follows

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="5dp" >

    <EditText
        android:id="@+id/et_phone"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:hint="请输入11位手机号码"
        android:inputType="number"
        android:maxLength="11"
        android:background="@drawable/editext_selector"
        android:textColor="@color/black"
        android:textSize="17sp" />

    <EditText
        android:id="@+id/et_password"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_marginTop="5dp"
        android:hint="请输入6位密码"
        android:inputType="numberPassword"
        android:maxLength="6"
        android:background="@drawable/editext_selector"
        android:textColor="@color/black"
        android:textSize="17sp" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="登录"
        android:textColor="@color/black"
        android:textSize="17sp" />

</LinearLayout>

3. Text change listener

The soft keyboard of the input method often covers the lower part of the page, making the login, confirmation, next and other buttons invisible. If the user wants to click these buttons, they have to click the return key again to close the soft keyboard. In order to facilitate user operations , it is best to automatically close the soft keyboard when certain conditions are met, such as automatically closing the soft keyboard after entering 11 digits of a mobile phone number.

The input method soft keyboard is managed by the system service INPUT_METHOD_SERVICE, so closing the soft keyboard must also be handled by this service.

This function point requires real-time monitoring of the currently entered text length. This monitoring operation uses the text listener interface TextWatcher. This interface provides three monitoring methods. The details are as follows:

1: beforeTextChanged triggers before the text changes

2: onTextChanged is triggered when the text changes.

3: afterTextChanged triggers after the text changes

Let’s use an example to automatically close and hide the keyboard after entering an 11-digit mobile phone number and a 6-digit password.

 

 

 

 The EditHideActivity class code is as follows

package com.example.chapter05;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;

import com.example.chapter05.util.ViewUtil;

public class EditHideActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_hide);
        // 从布局文件中获取名叫et_phone的手机号码编辑框
        EditText et_phone = findViewById(R.id.et_phone);
        // 从布局文件中获取名叫et_password的密码编辑框
        EditText et_password = findViewById(R.id.et_password);
        // 给手机号码编辑框添加文本变化监听器
        et_phone.addTextChangedListener(new HideTextWatcher(et_phone, 11));
        // 给密码编辑框添加文本变化监听器
        et_password.addTextChangedListener(new HideTextWatcher(et_password, 6));
    }

    // 定义一个编辑框监听器,在输入文本达到指定长度时自动隐藏输入法
    private class HideTextWatcher implements TextWatcher {
        private EditText mView; // 声明一个编辑框对象
        private int mMaxLength; // 声明一个最大长度变量

        public HideTextWatcher(EditText v, int maxLength) {
            super();
            mView = v;
            mMaxLength = maxLength;
        }

        // 在编辑框的输入文本变化前触发
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

        // 在编辑框的输入文本变化时触发
        public void onTextChanged(CharSequence s, int start, int before, int count) {}

        // 在编辑框的输入文本变化后触发
        public void afterTextChanged(Editable s) {
            String str = s.toString(); // 获得已输入的文本字符串
            // 输入文本达到11位(如手机号码),或者达到6位(如登录密码)时关闭输入法
            if ((str.length() == 11 && mMaxLength == 11)
                    || (str.length() == 6 && mMaxLength == 6)) {
                ViewUtil.hideOneInputMethod(EditHideActivity.this, mView); // 隐藏输入法软键盘
            }
        }
    }
}

activity_edit_hideXML file code is as follows

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="5dp" >

    <EditText
        android:id="@+id/et_phone"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:hint="输入11位时自动隐藏输入法"
        android:inputType="number"
        android:maxLength="11"
        android:background="@drawable/editext_selector"
        android:textColor="@color/black"
        android:textSize="17sp" />

    <EditText
        android:id="@+id/et_password"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_marginTop="10dp"
        android:hint="输入6位时自动隐藏输入法"
        android:inputType="numberPassword"
        android:maxLength="6"
        android:background="@drawable/editext_selector"
        android:textColor="@color/black"
        android:textSize="17sp" />

</LinearLayout>

Creation is not easy. If you find it helpful, please like, follow and collect.

Guess you like

Origin blog.csdn.net/jiebaoshayebuhui/article/details/127716484