Android开发 文本输入 EditText 监听器

这篇具有很好参考价值的文章主要介绍了Android开发 文本输入 EditText 监听器。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

1.EditText

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

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

android edittext 输入监听,APP开发,android,android studio

常用属性:

android edittext 输入监听,APP开发,android,android studio

 系统默认的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"

    >


    <EditText
        android: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>

    <EditText
        android: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>

效果图:

android edittext 输入监听,APP开发,android,android studio

android edittext 输入监听,APP开发,android,android studio

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

selector:

android edittext 输入监听,APP开发,android,android studio

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

focus shape: 

android edittext 输入监听,APP开发,android,android studio

nofocus shape: 

android edittext 输入监听,APP开发,android,android studio

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"

    >
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="default edit text"
        android:inputType="text"
        android:maxLength="20"


        ></EditText>

    <EditText
        android: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>

    <EditText
        android: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>

效果图:

android edittext 输入监听,APP开发,android,android studio

android edittext 输入监听,APP开发,android,android studio

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"
    >

    <EditText
        android: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>

    <EditText
        android: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>


    <Button
        android: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;

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

        etPhone = findViewById(R.id.etPhone);

        findViewById(R.id.etPw).setOnFocusChangeListener(this);

    }

    @Override
    public 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位,将焦点返回手机号码栏,弹出提示。

android edittext 输入监听,APP开发,android,android studio

3.文本变化监听器

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

android edittext 输入监听,APP开发,android,android studio

 举例:当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 {

    @Override
    protected 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;
        }

        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void afterTextChanged(Editable editable) {
            String s = editable.toString();
            if(s.length()==maxL){
                UtilFunc.hideInput(TextChangeActivity.this, et);
            }
        }
    }
}

隐藏输入法的工具函数实现:文章来源地址https://www.toymoban.com/news/detail-531884.html

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);
    }

}

到了这里,关于Android开发 文本输入 EditText 监听器的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • HttpSessionListener监听器和HttpSessionAttributeListener监听器

    1.作用:监听Session创建或销毁,即生命周期监听 2.相关方法: 3.使用场景: 和前面的ServletContextListener等一样,可以用于监控用户上线和离线 4.代码 HttpSessionListener监听器 实现类 HttpSessionAttributeListener监听器 1.作用:监听Session属性的变化,使用少 2.相关方法: 3.代码 监听器 实

    2024年02月04日
    浏览(36)
  • Listener监听器----HttpServletRequest对象的生命周期监听器

    一、HttpServletRequest对象的生命周期监听器         ServletRequestListener接口定义了ServletRequest(是HttpServletRequest接口的父接口类型)对象生命周期的监听行为。 void requestInitialized(ServletRequestEvent sre)         HttpServletRequest对象创建后会触发该监听器方法,并将已创建HttpServletR

    2024年01月23日
    浏览(46)
  • camunda执行监听器和任务监听器有什么区别

    Camunda的执行监听器和任务监听器是用于添加自定义逻辑的监听器,它们的区别在于作用对象和触发事件的不同。 执行监听器是与BPMN流程中的各种流程元素(例如开始事件、用户任务、服务任务、网关等)相关联的。执行监听器可以在流程元素执行前、执行后或抛出异常时添

    2024年02月04日
    浏览(36)
  • 消息监听器和消息监听容器

    前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站。 消息监听器顾名思义用来接收消息,它是使用消息监听容器的必须条件。目前有8个消息监听器: 使用自动提交或容器管理的提交方法之一,处理从 Kafka 消费者 p

    2024年02月07日
    浏览(37)
  • watch监听器三种监听方式

    1、普通监听( 无法监听到第一次绑定的变化 ) 这样使用watch时有一个特点,就是当值第一次绑定的时候,不会执行监听函数,只有值发生改变才会执行。 2、普通监听( 可以监听到第一次绑定的变化) 给 text 绑定了一个handler方法,之前我们写的 watch 方法其实默认写的就是

    2024年02月15日
    浏览(30)
  • Servlet的监听器

    Servlet常用的监听器 ServletContextAttributeListener 用来感知 ServlerContext 对象属性变化,比如添加或删除属性变化 ServletContextListener 用来感知 ServlerContext 对象的创建和销毁的 ServletRequestListener 可以用来监听感知 ServletRequest 对象的创建和销毁的 ServletRequestAttributeListener 用来感知 Serv

    2024年02月17日
    浏览(30)
  • SpringBoot 监听器

    Spring的监听器也可以说是一种观察者模式,它能实现事件与事件监听者直接的解耦,在Spring中监听器的实现主要有一下重要组件: ApplicationListener:事件监听者,观察者; ApplicationEvent:Spring 事件,记录事件源、事件内容、时间等数据; 有些场景事件主体主要是String或基本类

    2024年02月09日
    浏览(28)
  • Flowable监听器

    本人最近在找工作,有推荐的小伙伴私我,不胜感激。 开始、结束节点 连线节点 节点的开始和结束 网关的开始和结束 中间事件的开始和结束 开始时间结束或结束事件开始 Event事件 start 开始 take 启用 end 结束 类型 类:Class:com.sgp.StartListeners 类名全限定 监听器类上无论是否

    2024年02月16日
    浏览(26)
  • 计算属性和监听器

    计算属性是Vue中非常常用的一个概念,它能够根据现有的数据直接生成一个新的数据进行展示和操作。在Vue的实例中,可以使用 computed 来定义计算属性。 例如,我们有一个Vue实例 vm ,其中有一个数据 message ,我们可以通过计算属性 reversedMessage 来生成消息的反转字符串: 在

    2024年02月15日
    浏览(26)
  • Kafka 监听器详解

    Kafka Assistant 是一款 Kafka GUI 管理工具——管理Broker,Topic,Group、查看消费详情、监控服务器状态、支持多种消息格式。 你需要将 advertised.listeners (如果你使用Docker镜像,则为 KAFKA_ADVERTISED_LISTENERS )设置为外部地址(host/IP),以便客户端可以正确地连接到它。否则,他们会尝试

    2024年02月06日
    浏览(29)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包