Android:TextWatcher

1、文本框動態跟隨顯示輸入以及控制字數 前端

在編寫代碼的時候,若是須要讓文本框可以跟隨編輯框輸入的內容而顯示出來的話,可使用TextWatcher。 java

另外,使用該接口能夠控制編輯狀態和效果,具體看代碼。 android

完整代碼: app

package com.xsjayz.textwatcher;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

/**
 * EditText在佈局中常常用到,對EditText中輸入的內容也常常須要進行限制,
 * 能夠經過TextWatcher去觀察輸入框中輸入的內容來限制輸入字符個數。
 * 
 * @author XuShaoJie
 * @version 2012-08-30
 */
public class TextWatcherActivity extends Activity {

	private TextView textView;
	private EditText editText;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		textView = (TextView) findViewById(R.id.textview);
		editText = (EditText) findViewById(R.id.edittext);

		editText.addTextChangedListener(textWatcher);
	}

	/**
	 * TextWatcher:接口,繼承它要實現其三個方法,分別爲Text改變以前、改變的過程當中、改變以後各自發生的動做
	 */
	TextWatcher textWatcher = new TextWatcher() {

		private CharSequence charSequence;
		private int editStart;
		private int editEnd;

		@Override
		public void beforeTextChanged(CharSequence s, int arg1, int arg2,
				int arg3) {
			charSequence = s;
		}

		@Override
		public void onTextChanged(CharSequence s, int arg1, int arg2, int arg3) {
			// 讓TextView一直跟隨EditText輸入的內容同步顯示
			textView.setText(s);
		}

		@Override
		public void afterTextChanged(Editable s) {

			// 輸入的時候,只有一個光標,那麼這兩個值應該是相等的。。。
			editStart = editText.getSelectionStart();
			editEnd = editText.getSelectionEnd();

			// 限定EditText只能輸入10個數字,而且達到10個的時候用紅色顯示
			if (charSequence.length() > 10) {
				Toast.makeText(TextWatcherActivity.this, "你輸入的字數已經超過了限制!",
						Toast.LENGTH_SHORT).show();
				// 默認光標在最前端,因此當輸入第11個數字的時候,刪掉(光標位置從11-1到11)的數字,這樣就沒法輸入超過10個之後的數字
				s.delete(editStart - 1, editEnd);
				// 當輸入超過第10個數字的時候,改變字體顏色爲紅色
				editText.setTextColor(Color.RED);
			}
		}
	};
}

佈局文件: ide

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/input_number"
        android:textSize="16sp"
        android:textColor="@android :color/white" />

    <EditText
        android:id="@+id/edittext"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="number" />

</LinearLayout>



2、輸入驗證碼 佈局

舉個例子,有兩個編輯框,第一個用於輸入手機號碼獲取驗證碼,當手機收到驗證碼後,點擊提交按鈕,出現驗證碼輸入框,容許用戶輸入驗證碼。 字體

經過使用TextWatcher,能夠很輕鬆的實現,具體看截圖: this

 

當第一個編輯框輸入的手機號碼符合要求後,提交按鈕生效,可是此時第二個編輯框仍然不能輸入: spa

點擊提交按鈕,第二個輸入框恢復有效狀態,此時能夠輸入驗證碼: .net

 

java代碼:

package com.xsjayz.textwatcher;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

/**
 * EditText在佈局中常常用到,對EditText中輸入的內容也常常須要進行限制,
 * 能夠經過TextWatcher去觀察輸入框中輸入的內容來限制輸入字符個數。
 * 
 * @author XuShaoJie
 * @version 2012-08-31
 */
public class TextWatcherActivity extends Activity {

	private TextView textView;
	private EditText editText;
	private EditText editText2;
	private Button button;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		textView = (TextView) findViewById(R.id.textview);
		editText = (EditText) findViewById(R.id.edittext);
		editText2 = (EditText) findViewById(R.id.edittext2);
		button = (Button) findViewById(R.id.button);
		button.setOnClickListener(buttonListener);
		// 開始前設置editText2和button狀態爲不可用
		editText2.setEnabled(false);
		button.setEnabled(false);

		editText.addTextChangedListener(textWatcher);
	}

	// "提交"按鈕:點擊後讓editText2從隱藏和不可用狀態恢復爲可用狀態
	OnClickListener buttonListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			editText2.setEnabled(true);
		}
	};

	/**
	 * TextWatcher:接口,繼承它要實現其三個方法,分別爲Text改變以前、改變的過程當中、改變以後各自發生的動做
	 */
	TextWatcher textWatcher = new TextWatcher() {

		private CharSequence charSequence;
		private int editStart;
		private int editEnd;

		@Override
		public void beforeTextChanged(CharSequence s, int arg1, int arg2,
				int arg3) {
			charSequence = s;
		}

		@Override
		public void onTextChanged(CharSequence s, int arg1, int arg2, int arg3) {
			// 讓TextView一直跟隨EditText輸入的內容同步顯示
			textView.setText(s);
		}

		@Override
		public void afterTextChanged(Editable s) {

			// 輸入的時候,只有一個光標,那麼這兩個值應該是相等的。。。
			editStart = editText.getSelectionStart();
			editEnd = editText.getSelectionEnd();

			// 限定EditText只能輸入10個數字,而且達到10個的時候用紅色顯示
			if (charSequence.length() > 10) {
				Toast.makeText(TextWatcherActivity.this, "你輸入的字數已經超過了限制!",
						Toast.LENGTH_SHORT).show();
				// 默認光標在最前端,因此當輸入第12個數字的時候,刪掉(光標位置從12-1到12)的數字,這樣就沒法輸入超過11個之後的數字
				s.delete(editStart - 1, editEnd);
				// 當輸入超過第11個數字的時候,改變字體顏色爲紅色
				editText.setTextColor(Color.RED);
				// 使button恢復爲可用狀態
				button.setEnabled(true);
			}
		}
	};
}

佈局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/input_number"
        android:textColor="@android:color/white"
        android:textSize="16sp" />

    <EditText
        android:id="@+id/edittext"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/edit1_hint"
        android:inputType="number" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:text="@string/input_button"
        android:textColor="@android:color/black" />

    <EditText
        android:id="@+id/edittext2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/edit2_hint" />

</LinearLayout>
相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息