Android寫一個實時輸入框功能
我們在做安卓項目時通常都會對Android的 EditText輸入框的內(nèi)容實時監(jiān)聽,這里我們就做一個實時監(jiān)聽框,EditText實時輸入,而TextView實現(xiàn)實時顯示。話不多說,直接上效果圖:


以下是代碼
配置文件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) {
}
}
}
小牢騷:
最開始我還沒有百度過實時輸入框這個東西,然后就自己悶頭做。我的想法是通過開辟一個子線程來實現(xiàn)監(jiān)聽,然后將這個在EditTex找到id之后就開始運行,發(fā)現(xiàn)只要文本框一輸入就開始報錯或者已進入程序就來個白屏。最后再度娘的幫助下成功脫困。
下面看下android 輸入框?qū)崟r監(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é)
到此這篇關于Android寫一個實時輸入框的文章就介紹到這了,更多相關android 實時輸入框內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
logcat命令使用方法和查看android系統(tǒng)日志緩沖區(qū)內(nèi)容的方法
這篇文章主要介紹了logcat命令使用方法和查看android系統(tǒng)日志緩沖區(qū)內(nèi)容的方法,需要的朋友可以參考下2014-02-02
詳解Android獲得系統(tǒng)GPU參數(shù) gl.glGetString
這篇文章主要介紹了詳解Android獲得系統(tǒng)GPU參數(shù) gl.glGetString的相關資料,需要的朋友可以參考下2017-07-07
Android自定義控件開發(fā)實戰(zhàn)之實現(xiàn)ListView下拉刷新實例代碼
這篇文章主要介紹了Android自定義控件開發(fā)實戰(zhàn)之實現(xiàn)ListView下拉刷新實例代碼的相關資料,需要的朋友可以參考下2016-04-04
Android如何獲取系統(tǒng)通知的開啟狀態(tài)詳解
這篇文章主要給大家介紹了關于Android如何獲取系統(tǒng)通知開啟狀態(tài)的相關資料,文中通過示例代碼介紹的非常詳細,對各位Android開發(fā)者們具有一定的參考學習價值,需要的朋友們下面跟著小編來一起看看吧啊。2017-08-08

