我们在做安卓项目时通常都会对Android的 EditText
输入框的内容实时监听,这里我们就做一个实时监听框,EditText实时输入,而TextView实现实时显示。话不多说,直接上效果图:
以下是代码
配置文件activity_main.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
<?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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
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 输入框实时监听
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
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 实时输入框内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/l15120145814/article/details/105104465