Android寫(xiě)一個(gè)實(shí)時(shí)輸入框功能
我們?cè)谧霭沧宽?xiàng)目時(shí)通常都會(huì)對(duì)Android的 EditText輸入框的內(nèi)容實(shí)時(shí)監(jiān)聽(tīng),這里我們就做一個(gè)實(shí)時(shí)監(jiān)聽(tīng)框,EditText實(shí)時(shí)輸入,而TextView實(shí)現(xiàn)實(shí)時(shí)顯示。話不多說(shuō),直接上效果圖:
以下是代碼
配置文件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: android:text='你好' android:textSize='20dp' android:textColor='@android:color/holo_red_light' android:gravity='center'/> <EditText android:layout_weight='3' android: 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: 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) { } }}
小牢騷:
最開(kāi)始我還沒(méi)有百度過(guò)實(shí)時(shí)輸入框這個(gè)東西,然后就自己悶頭做。我的想法是通過(guò)開(kāi)辟一個(gè)子線程來(lái)實(shí)現(xiàn)監(jiān)聽(tīng),然后將這個(gè)在EditTex找到id之后就開(kāi)始運(yùn)行,發(fā)現(xiàn)只要文本框一輸入就開(kāi)始報(bào)錯(cuò)或者已進(jìn)入程序就來(lái)個(gè)白屏。最后再度娘的幫助下成功脫困。
下面看下android 輸入框?qū)崟r(shí)監(jiān)聽(tīng)
editText.addTextChangedListener(new TextWatcher() { @Overridepublic void onTextChanged(CharSequence s, int start, int before, int count) { Log.e(TAG, '輸入文字中的狀態(tài),count是輸入字符數(shù)'); Log.e(TAG, editText.getText());}@Overridepublic void beforeTextChanged(CharSequence s, int start, int count, int after) { Log.e(TAG, '輸入文本之前的狀態(tài)');}@Overridepublic void afterTextChanged(Editable s) { Log.e(TAG, '輸入文字后的狀態(tài)');} });
總結(jié)
到此這篇關(guān)于Android寫(xiě)一個(gè)實(shí)時(shí)輸入框的文章就介紹到這了,更多相關(guān)android 實(shí)時(shí)輸入框內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. CSS hack用法案例詳解2. JSP servlet實(shí)現(xiàn)文件上傳下載和刪除3. ASP.NET Core實(shí)現(xiàn)中間件的幾種方式4. Jsp+Servlet實(shí)現(xiàn)文件上傳下載 刪除上傳文件(三)5. 詳解瀏覽器的緩存機(jī)制6. 怎樣才能用js生成xmldom對(duì)象,并且在firefox中也實(shí)現(xiàn)xml數(shù)據(jù)島?7. input submit、button和回車鍵提交數(shù)據(jù)詳解8. 怎樣打開(kāi)XML文件?xml文件如何打開(kāi)?9. 詳解盒子端CSS動(dòng)畫(huà)性能提升10. css代碼優(yōu)化的12個(gè)技巧
