Android寫一個(gè)實(shí)時(shí)輸入框功能
我們?cè)谧霭沧宽?xiàng)目時(shí)通常都會(huì)對(duì)Android的 EditText輸入框的內(nèi)容實(shí)時(shí)監(jiān)聽,這里我們就做一個(gè)實(shí)時(shí)監(jiān)聽框,EditText實(shí)時(shí)輸入,而TextView實(shí)現(xiàn)實(shí)時(shí)顯示。話不多說,直接上效果圖:


以下是代碼
配置文件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="點(diǎn)擊輸入"
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) {
}
}
}
小牢騷:
最開始我還沒有百度過實(shí)時(shí)輸入框這個(gè)東西,然后就自己悶頭做。我的想法是通過開辟一個(gè)子線程來實(shí)現(xiàn)監(jiān)聽,然后將這個(gè)在EditTex找到id之后就開始運(yùn)行,發(fā)現(xiàn)只要文本框一輸入就開始報(bào)錯(cuò)或者已進(jìn)入程序就來個(gè)白屏。最后再度娘的幫助下成功脫困。
下面看下android 輸入框?qū)崟r(shí)監(jiān)聽
editText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Log.e(TAG, "輸入文字中的狀態(tài),count是輸入字符數(shù)");
Log.e(TAG, editText.getText());
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
Log.e(TAG, "輸入文本之前的狀態(tài)");
}
@Override
public void afterTextChanged(Editable s) {
Log.e(TAG, "輸入文字后的狀態(tài)");
}
});
總結(jié)
到此這篇關(guān)于Android寫一個(gè)實(shí)時(shí)輸入框的文章就介紹到這了,更多相關(guān)android 實(shí)時(shí)輸入框內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android中AlertDialog用法實(shí)例分析
這篇文章主要介紹了Android中AlertDialog用法,結(jié)合實(shí)例形式簡單分析了AlertDialog的基本調(diào)用與功能實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-01-01
Android Framework Application Framework層簡單介紹
這篇文章主要介紹了 Android Framework Application Framework層簡單介紹的相關(guān)資料,需要的朋友可以參考下2016-11-11
Android開發(fā)實(shí)現(xiàn)TextView超鏈接5種方式源碼實(shí)例
這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)TextView超鏈接5種方式源碼實(shí)例,需要的朋友可以參考下2020-03-03
Android AbsoluteLayout和RelativeLayout布局詳解
本文主要講解Android AbsoluteLayout和RelativeLayout布局,這里整理了相關(guān)資料,并附示例代碼和效果圖,有興趣的小伙伴可以參考下2016-08-08
Android使用GestureOverlayView控件實(shí)現(xiàn)手勢(shì)識(shí)別
這篇文章主要為大家詳細(xì)介紹了Android使用GestureOverlayView控件實(shí)現(xiàn)手勢(shì)識(shí)別,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04
android實(shí)現(xiàn)搜索功能并將搜索結(jié)果保存到SQLite中(實(shí)例代碼)
這篇文章主要介紹了android實(shí)現(xiàn)搜索功能并將搜索結(jié)果保存到SQLite中,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04
Kotlin類型安全構(gòu)建器的一次運(yùn)用記錄
這篇文章主要給大家介紹了關(guān)于Kotlin類型安全構(gòu)建器的一次運(yùn)用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Kotlin具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06

