android 監聽EditText 的變化

來源:互聯網
上載者:User

標籤:android   edittext   

使用EditText的addTextChangedListener(TextWatcher watcher)方法對EditText實現監聽,TextWatcher是一個介面類,所以必須實現TextWatcher裡的抽象方法:

當EditText裡面的內容有變化的時候,觸發TextChangedListener事件,就會調用TextWatcher裡面的抽象方法。
布局:
<?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" ><EditText    android:id="@+id/main_et"     android:layout_width="fill_parent"       android:layout_height="wrap_content" /> <TextView    android:id="@+id/main_tv"     android:layout_width="fill_parent"       android:layout_height="wrap_content" /> </LinearLayout>
一、實現監聽文字框字數,提示還能輸入多少個字元
MainActivity:
package chay.mian;import android.app.Activity;import android.os.Bundle;import android.text.Editable;import android.text.TextWatcher;import android.view.View;import android.widget.EditText;import android.widget.TextView;public class MainActivity extends Activity {private EditText editText;private TextView tip;private final int charMaxNum = 10; // 允許輸入的字數@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);init();}private void init() {editText = (EditText) findViewById(R.id.main_et);editText.addTextChangedListener(new EditChangedListener());tip = (TextView) findViewById(R.id.main_tv);tip.setText("0/"+charMaxNum);}class EditChangedListener implements TextWatcher {private CharSequence temp; // 監聽前的文本private int editStart; // 游標開始位置private int editEnd; // 游標結束位置// 輸入文本之前的狀態@Overridepublic void beforeTextChanged(CharSequence s, int start, int count,int after) {temp = s;}// 輸入文字中的狀態,count是一次性輸入字元數@Overridepublic void onTextChanged(CharSequence s, int start, int before, int count) {//if (charMaxNum - s.length() <= 5) {//tip.setText("還能輸入" + (charMaxNum - s.length()) + "字元");//}tip.setText((s.length()) + "/" + charMaxNum);}// 輸入文字後的狀態@Overridepublic void afterTextChanged(Editable s) {/** 得到游標開始和結束位置 ,超過最大數後記錄剛超出的數字索引進行控制 */editStart = editText.getSelectionStart();editEnd = editText.getSelectionEnd();if (temp.length() > charMaxNum) {//Toast.makeText(getApplicationContext(), "最多輸入10個字元", Toast.LENGTH_SHORT).show();s.delete(editStart - 1, editEnd);editText.setText(s);editText.setSelection(s.length());}}};}


二、資料類型檢驗,當輸入不是整型數位時候,會立刻彈出輸入框,提示改正
MainActivity:

package chay.mian;import android.app.Activity;import android.app.AlertDialog;import android.content.DialogInterface;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 Activity {private EditText editText;private TextView tip;String str;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_mian);init();}private void init() {editText = (EditText) findViewById(R.id.mian_et);editText.addTextChangedListener(new EditChangedListener());tip = (TextView) findViewById(R.id.mian_tv);tip.setText("請輸入整型數字");}class EditChangedListener implements TextWatcher {// 輸入文本之前的狀態@Overridepublic void beforeTextChanged(CharSequence s, int start, int count, int after) {Log.d("TAG", "beforeTextChanged--------------->");}// 輸入文字中的狀態,count是一次性輸入字元數@Overridepublic void onTextChanged(CharSequence s, int start, int before, int count) {Log.d("TAG", "onTextChanged--------------->");}// 輸入文字後的狀態@Overridepublic void afterTextChanged(Editable s) {Log.d("TAG", "afterTextChanged--------------->");str = editText.getText().toString();try {//if ((editText.getText().toString()) != null)Integer.parseInt(str);} catch (Exception e) {showDialog();}}};private void showDialog() {AlertDialog dialog;AlertDialog.Builder builder = new AlertDialog.Builder(this);builder.setTitle("訊息").setIcon(android.R.drawable.stat_notify_error);builder.setMessage("你輸出的整型數字有誤,請改正");builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stub}});dialog = builder.create();dialog.show();}}

在LogCat查看調用這些方法的順序:
beforeTextChanged-->onTextChanged-->onTextChanged


歡迎交流 http://blog.csdn.net/ycwol/article/details/46594275

android 監聽EditText 的變化

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.