亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

Android寫一個實時輸入框功能

瀏覽:83日期:2022-09-25 09:05:08

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

Android寫一個實時輸入框功能Android寫一個實時輸入框功能

以下是代碼

配置文件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='點擊輸入' 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) { } }}

小牢騷:

最開始我還沒有百度過實時輸入框這個東西,然后就自己悶頭做。我的想法是通過開辟一個子線程來實現監聽,然后將這個在EditTex找到id之后就開始運行,發現只要文本框一輸入就開始報錯或者已進入程序就來個白屏。最后再度娘的幫助下成功脫困。

下面看下android 輸入框實時監聽

editText.addTextChangedListener(new TextWatcher() { @Overridepublic void onTextChanged(CharSequence s, int start, int before, int count) { Log.e(TAG, '輸入文字中的狀態,count是輸入字符數'); Log.e(TAG, editText.getText());}@Overridepublic void beforeTextChanged(CharSequence s, int start, int count, int after) { Log.e(TAG, '輸入文本之前的狀態');}@Overridepublic void afterTextChanged(Editable s) { Log.e(TAG, '輸入文字后的狀態');} });

總結

到此這篇關于Android寫一個實時輸入框的文章就介紹到這了,更多相關android 實時輸入框內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Android
相關文章:
主站蜘蛛池模板: 亚洲性69影院在线观看 | 99久久免费中文字幕精品 | 特级毛片8级毛片免费观看 特级毛片aaaaaa蜜桃 | 一级待一黄aaa大片在线还看 | 国语一区 | 91在线激情在线观看 | 欧美a级黄 | 在线播放精品 | 亚洲精品一区二区三区在线播放 | 911短视频| 中国人xxxxxxx免费看 | 亚洲阿v天堂2021在线观看 | 亚洲地址一地址二地址三 | 8mav模特福利视频在线观看 | 亚洲综合二区 | 国产97在线 | 亚洲 | 国产中文字幕在线 | 96免费精品视频在线观看 | 中文在线观看永久免费 | 亚洲第一视频在线观看 | 久久免费播放视频 | 看一级黄色毛片 | 中日韩黄色大片 | 亚洲色图 在线视频 | 亚洲欧美日韩在线精品一区二区 | 亚洲美女视频一区 | 看片在线观看免费 | 午夜国产精品久久影院 | 国产精品9999久久久久 | 免费污片在线观看 | 欧美人与动物xxxx | 日韩国产有码在线观看视频 | 男女性高爱潮免费网站 | 热久久影院 | 亚洲精品午夜久久aaa级久久久 | wwxxx日本 | 久久高清一区二区三区 | 91久久国产情侣真实对白 | 亚洲欧美日韩国产一区二区三区精品 | 亚洲a图 | 永久在线 |