兩種方法,針對EditText密碼的顯示和隱藏(包括激將光標移動到文本末尾)

<span style="font-size:18px;">注:示例代碼是封裝的方法體,demo運行需要自填剩餘

package com.sunzhen.files;


import android.text.Selection;
import android.text.Spannable;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;


/**
 * 文本輸入框的常見操作
 * 
 * @author 
 * 
 */
public class EditTextUtil {


	/**
	 * 設置密碼的顯示和隱藏
	 * 兩種操作,其中註釋掉的也是一種做法
	 * @param ck
	 * @param pwdEditText
	 */
	public static void IsShowPassWord(CheckBox ck, final EditText pwdEditText) {
		ck.setOnCheckedChangeListener(new OnCheckedChangeListener() {


			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				if (isChecked) { // 顯示密碼
				// pwdEditText.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
					pwdEditText.setTransformationMethod(
							HideReturnsTransformationMethod.getInstance());
				} else { //隱藏密碼
					// pwdEditText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
					pwdEditText.setTransformationMethod(
							PasswordTransformationMethod.getInstance());
				}
				// 設置光標到未能本末尾
				CharSequence charSequence = pwdEditText.getText();
				if (charSequence instanceof Spannable) {
					Spannable spanText = (Spannable) charSequence;
					Selection.setSelection(spanText, charSequence.length());
				}
			}
		});
	}
}</span>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章