EditText監(jiān)聽方法,實時的判斷輸入多少字符
最近在寫一個小項目,其中有一點用到了顯示EditText中輸入了多少個字符,像微博中顯示剩余多少字符的功能。在EditText提供了一個方法addTextChangedListener實現(xiàn)對輸入文本的監(jiān)控。下邊是我自己寫的一個Demo。
代碼實現(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);//將輸入的內容實時顯示
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
editStart = mEditText.getSelectionStart();
editEnd = mEditText.getSelectionEnd();
mTextView.setText("您輸入了" + temp.length() + "個字符");
if (temp.length() > 10) {
Toast.makeText(TestActivity.this,
"你輸入的字數(shù)已經(jīng)超過了限制!", Toast.LENGTH_SHORT)
.show();
s.delete(editStart-1, editEnd);
int tempSelection = editStart;
mEditText.setText(s);
mEditText.setSelection(tempSelection);
}
}
};
}

以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!
相關文章
解決Android從相冊中獲取圖片出錯圖片卻無法裁剪問題的方法
這篇文章主要介紹了解決Android從相冊中獲取圖片出錯圖片卻無法裁剪問題的方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-01-01
Android WebViewClient 的 `shouldOverrideUrlLoa
這篇文章主要介紹了Android WebViewClient 的 shouldOverrideUrlLoading方法,了解并正確實現(xiàn) WebViewClient 中的 shouldOverrideUrlLoading 方法對于在你的 Android 應用中提供順暢且安全的瀏覽體驗至關重要,需要的朋友可以參考下2024-07-07
Android實戰(zhàn)RecyclerView頭部尾部添加方法示例
本篇文章主要介紹了Android實戰(zhàn)RecyclerView頭部尾部添加方法示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11
Android自定義控件實現(xiàn)帶數(shù)值和動畫的圓形進度條
這篇文章主要為大家詳細介紹了Android自定義控件實現(xiàn)帶數(shù)值和動畫的圓形進度條,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-12-12
Android PraiseTextView實現(xiàn)朋友圈點贊功能
這篇文章主要為大家詳細介紹了PraiseTextView簡單實現(xiàn)朋友圈點贊功能的相關資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-01-01

