EditText監(jiān)聽(tīng)方法,實(shí)時(shí)的判斷輸入多少字符
最近在寫(xiě)一個(gè)小項(xiàng)目,其中有一點(diǎn)用到了顯示EditText中輸入了多少個(gè)字符,像微博中顯示剩余多少字符的功能。在EditText提供了一個(gè)方法addTextChangedListener實(shí)現(xiàn)對(duì)輸入文本的監(jiān)控。下邊是我自己寫(xiě)的一個(gè)Demo。
代碼實(shí)現(xiàn):
布局文件main.xml
[html] view plain copy <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/tv" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="@android:color/white" android:text="Please input the text:" /> <EditText android:id="@+id/ET" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
Activity
[java] view plain copy package com.damai.test; import android.app.Activity; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class TestActivity extends Activity { private TextView mTextView; private EditText mEditText; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mTextView = (TextView)findViewById(R.id.tv); mEditText = (EditText)findViewById(R.id.ET); mEditText.addTextChangedListener(mTextWatcher); } TextWatcher mTextWatcher = new TextWatcher() { private CharSequence temp; private int editStart ; private int editEnd ; @Override public void onTextChanged(CharSequence s, int start, int before, int count) { // TODO Auto-generated method stub temp = s; } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { // TODO Auto-generated method stub // mTextView.setText(s);//將輸入的內(nèi)容實(shí)時(shí)顯示 } @Override public void afterTextChanged(Editable s) { // TODO Auto-generated method stub editStart = mEditText.getSelectionStart(); editEnd = mEditText.getSelectionEnd(); mTextView.setText("您輸入了" + temp.length() + "個(gè)字符"); if (temp.length() > 10) { Toast.makeText(TestActivity.this, "你輸入的字?jǐn)?shù)已經(jīng)超過(guò)了限制!", Toast.LENGTH_SHORT) .show(); s.delete(editStart-1, editEnd); int tempSelection = editStart; mEditText.setText(s); mEditText.setSelection(tempSelection); } } }; }
以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,同時(shí)也希望多多支持腳本之家!
相關(guān)文章
解決Android從相冊(cè)中獲取圖片出錯(cuò)圖片卻無(wú)法裁剪問(wèn)題的方法
這篇文章主要介紹了解決Android從相冊(cè)中獲取圖片出錯(cuò)圖片卻無(wú)法裁剪問(wèn)題的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-01-01Android WebViewClient 的 `shouldOverrideUrlLoa
這篇文章主要介紹了Android WebViewClient 的 shouldOverrideUrlLoading方法,了解并正確實(shí)現(xiàn) WebViewClient 中的 shouldOverrideUrlLoading 方法對(duì)于在你的 Android 應(yīng)用中提供順暢且安全的瀏覽體驗(yàn)至關(guān)重要,需要的朋友可以參考下2024-07-07Android實(shí)戰(zhàn)RecyclerView頭部尾部添加方法示例
本篇文章主要介紹了Android實(shí)戰(zhàn)RecyclerView頭部尾部添加方法示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11Android自定義控件實(shí)現(xiàn)帶數(shù)值和動(dòng)畫(huà)的圓形進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了Android自定義控件實(shí)現(xiàn)帶數(shù)值和動(dòng)畫(huà)的圓形進(jìn)度條,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12Android PraiseTextView實(shí)現(xiàn)朋友圈點(diǎn)贊功能
這篇文章主要為大家詳細(xì)介紹了PraiseTextView簡(jiǎn)單實(shí)現(xiàn)朋友圈點(diǎn)贊功能的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01Android ViewFlipper簡(jiǎn)單應(yīng)用
這篇文章主要為大家詳細(xì)介紹了Android ViewFlipper簡(jiǎn)單應(yīng)用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01