Android写一个实时输入框功能

2020-09-25 0 296

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

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

以下是代码

配置文件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, \"输入文字后的状态\");  
      }  
    }); 

总结

到此这篇关于Android写一个实时输入框的文章就介绍到这了,更多相关android 实时输入框内容请搜索自学编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持自学编程网!

遇见资源网 Android Android写一个实时输入框功能 http://www.ox520.com/23484.html

常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务