android 实时输入框 Android写一个实时输入框功能

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

android 实时输入框 Android写一个实时输入框功能

l15120145814   2021-04-21 我要评论

我们在做安卓项目时通常都会对Android的 EditText输入框的内容实时监听,这里我们就做一个实时监听框,EditText实时输入,而TextView实现实时显示。话不多说,直接上效果图:

在这里插入图片描述在这里插入图片描述

以下是代码

配置文件activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".MainActivity">
  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <TextView
      android:layout_weight="1"
      android:layout_width="match_parent"
      android:layout_height="0dp"
      android:id="@+id/hello"
      android:text="你好"
      android:textSize="20dp"
      android:textColor="@android:color/holo_red_light"
      android:gravity="center"/>
    <EditText
      android:layout_weight="3"
      android:id="@+id/input"
      android:layout_width="match_parent"
      android:layout_height="0dp"
      android:textSize="20sp"
      android:hint="点击输入"
      android:textColorHint="@android:color/holo_blue_bright"
      android:background="@null"/>
    <TextView
      android:layout_weight="3"
      android:background="@android:color/holo_blue_light"
      android:id="@+id/output"
      android:layout_width="match_parent"
      android:layout_height="0dp"
      android:textSize="30sp"/>
  </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

java文件MainActivity.java:

package com.shiyan.realtimetext;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
  private TextView output;
  private EditText input;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    input=findViewById(R.id.input);
    output=findViewById(R.id.output);
    input.addTextChangedListener(new Watcher());
  }
  private class Watcher implements TextWatcher {
    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }
    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
      output.setText(charSequence);
    }
    @Override
    public void afterTextChanged(Editable editable) {

    }
  }
}

     小牢骚:

最开始我还没有百度过实时输入框这个东西,然后就自己闷头做。我的想法是通过开辟一个子线程来实现监听,然后将这个在EditTex找到id之后就开始运行,发现只要文本框一输入就开始报错或者已进入程序就来个白屏。最后再度娘的帮助下成功脱困。

下面看下android 输入框实时监听

editText.addTextChangedListener(new TextWatcher() {       
      @Override  
      public void onTextChanged(CharSequence s, int start, int before, int count) {  
        Log.e(TAG, "输入文字中的状态,count是输入字符数");  
        Log.e(TAG, editText.getText());  
      }  
        
      @Override  
      public void beforeTextChanged(CharSequence s, int start, int count,  
          int after) {   
        Log.e(TAG, "输入文本之前的状态");  
      }  
        
      @Override  
      public void afterTextChanged(Editable s) {   
        Log.e(TAG, "输入文字后的状态");  
      }  
    }); 

总结

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们