您的位置:首页 > 移动开发 > Android开发

Android写一个实时输入框功能

2020-04-02 12:08 2061 查看

我们在做安卓项目时通常都会对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, "输入文字后的状态");
}
});

总结

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

您可能感兴趣的文章:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android 实时 输入框
相关文章推荐