標籤:方便 cti 介面 gray 限制 corners ado change etc
TextWatcher是一個用來監聽文本變化的介面,使用該介面可以很方便的對可顯示文本控制項和可編輯文本控制項中的文字進行監聽和修改
TextWatcher介面中定義了三個方法:
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
該方法在文本改變之前調用,傳入了四個參數:
- CharSequence s:文本改變之前的內容
- int start:文本開始改變時的起點位置,從0開始計算
- int count:要被改變的文本字數,即將要被替代的選中文本字數
- int after:改變後添加的文本字數,即替代選中文本後的文本字數
該方法調用是在文本沒有被改變,但將要被改變的時候調用,把四個參數組成一句話就是:
在當前文本s中,從start位置開始之後的count個字元(即將)要被after個字元替換掉
public void onTextChanged(CharSequence s, int start, int before, int count) {}
該方法是在當文本改變時被調用,同樣傳入了四個參數:
- CharSequence s:文本改變之後的內容
- int start:文本開始改變時的起點位置,從0開始計算
- int before:要被改變的文本字數,即已經被替代的選中文本字數
- int count:改變後添加的文本字數,即替代選中文本後的文本字數
該方法調用是在文本被改變時,改變的結果已經可以顯示時調用,把四個參數組成一句話就是:
在當前文本s中,從start位置開始之後的before個字元(已經)被count個字元替換掉了
public void afterTextChanged(Editable s) {}
該方法是在文本改變結束後調用,傳入了一個參數:
該方法是在執行完beforeTextChanged、onTextChanged兩個方法後才會被調用,此時的文本s為最終顯示給使用者看到的文本。我們可以再對該文本進行下一步處理,比如把文本s顯示在UI介面上
實踐使用TextWatcher監聽EditText剩餘可輸入文本字數
有個這樣的需求:使用者在EditText中只能輸入50個字元,在使用者輸入的同時在UI介面中告訴使用者還可輸入多少個字元
布局檔案activity_main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/white" android:orientation="vertical"> <!-- 文字輸入框 --> <EditText android:id="@+id/edt_text_content" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp" android:background="@drawable/bg_edit_radius" android:gravity="top" android:maxLength="50" android:minLines="5" android:padding="10dp" android:textColor="@android:color/white" android:textSize="20sp" /> <!-- 已輸入字數 --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="已輸入字數:" android:textSize="16sp"/> <TextView android:id="@+id/tv_text_now_sum" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="0" android:textColor="#ff0000" android:textSize="16sp"/> </LinearLayout> <!-- 剩餘可輸入 --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="剩餘可輸入:" android:textSize="16sp"/> <TextView android:id="@+id/tv_text_remain" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="50" android:textColor="#ff0000" android:textSize="16sp"/> </LinearLayout></LinearLayout>
在以上布局中,EditText控制項使用maxLength=”50”限制可輸入字元為50個
需要使用的控制項和變數
/** 輸入框 */ private EditText mTextContentEdt; /** 剩餘字數 */ private TextView mTextRemainTv; /** 已輸入字數 */ private TextView mTextNowSumTv; /** 總可輸入字數 */ private int textRemainAll = 50;
輸入框樣式圖片bg_edit_radius.xml
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"> <stroke android:width="1dp" android:color="@android:color/black" /> <corners android:radius="10dp" /> <solid android:color="@android:color/darker_gray" /></shape>
初始化介面
/** * 初始化介面 */private void initUI() { setContentView(R.layout.activity_main); mTextContentEdt = (EditText) findViewById(R.id.edt_text_content); mTextRemainTv = (TextView) findViewById(R.id.tv_text_remain); mTextNowSumTv = (TextView) findViewById(R.id.tv_text_now_sum);}
給EditText添加監聽,MyTextWatcher類為我們自訂的監聽類
/** * 初始化監聽 */ private void initListener() { mTextContentEdt.addTextChangedListener(new MyTextWatcher()); }
建立一個自訂的TextWatcher監聽類,實現TextWatcher介面
/** * 建立自己的TextWatcher監聽類 */private class MyTextWatcher implements TextWatcher { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { // 已經輸入的字數 mTextNowSumTv.setText(String.valueOf(s.length())); // 剩餘可輸入字數 int remainSum = textRemainAll - s.length(); mTextRemainTv.setText(String.valueOf(remainSum)); } @Override public void afterTextChanged(Editable s) { }}
運行後的
Android 文本監聽介面TextWatcher詳解