Android EditText中输入值以星号*显示 (密码的实现)

Android Edittext中有密码的输入框,不过其显示的形式动态的变为圆点,此处我们实现的是输入内容直接显示为星号*。

以下为代码:

WordReplacement.java

package com.chuanchuan.edittextdemo;

import android.text.method.ReplacementTransformationMethod;

public class WordReplacement extends ReplacementTransformationMethod {

	String strWord = null;

	@Override
	protected char[] getOriginal() {
		//循环ASCII值 字符串形式累加到String
		for (char i = 0; i < 256; i++) {  
			strWord += String.valueOf(i);
		}
		//strWord转换为字符形式的数组
		char[] charOriginal = strWord.toCharArray();
		return charOriginal;
	}

	@Override
	protected char[] getReplacement() {
		char[] charReplacement = new char[255];
		//输入的字符在ASCII范围内,将其转换为*
		for (int i = 0; i < 255; i++) {
			charReplacement[i] = '*';
		}

		return charReplacement;
	}

}


MainActivity.java

package com.chuanchuan.edittextdemo;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.widget.EditText;
import android.widget.TextView;
import android.app.Activity;
public class MainActivity extends Activity {

	private EditText et1;
	private EditText et2;
	private EditText et3;
	private EditText et4;
	String strHint;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		et1=(EditText) findViewById(R.id.editText1);
		et2=(EditText) findViewById(R.id.editText2);
		et3=(EditText) findViewById(R.id.editText3);
		et4=(EditText) findViewById(R.id.editText4);
		strHint=getResources().getString(R.string.app_name);
		final EditText[] mArray = new EditText[] { et1,et2, et3, et4 };
		
		
		
		for (int i = 0; i < mArray.length; i++) {
			final int j = i;
			
			
			//输入字符星号化  
			mArray[j].setTransformationMethod(new WordReplacement());
			
			//EditText 获得焦点时hint消失,失去焦点时hint显示
			mArray[j].setOnFocusChangeListener(new OnFocusChangeListener() {
				@Override
				public void onFocusChange(View v, boolean hasFocus) {
	
			        if (!hasFocus) {
			        	((TextView) v).setHint(strHint);
			        } else {
			        	((TextView) v).setHint("");
			        }
					
				}
			});

			
			mArray[j].addTextChangedListener(new TextWatcher() {
				private CharSequence temp;
				private int sStart;
				private int sEnd;

				@Override
				public void onTextChanged(CharSequence s, int start,
						int before, int count) {
					temp = s;
				}

				@Override
				public void beforeTextChanged(CharSequence s, int start,
						int count, int after) {
				}

				@Override
				public void afterTextChanged(Editable s) {
					sStart = mArray[j].getSelectionStart();
					sEnd = mArray[j].getSelectionEnd();

					//前三个EditText输入满位(此处设置 为3)后,光标自动移到下一个TextView
					if (temp.length() == 3 && (j == 0 || j == 1 || j == 2)) {
						mArray[j + 1].setFocusable(true);
						mArray[j + 1].setFocusableInTouchMode(true);
						mArray[j + 1].requestFocus();
					}
					//输入超出三位时,删除多出的一位
					if (temp.length() > 3) {

						s.delete(sStart - 1, sEnd);
						int tSelection = sStart;
						mArray[j].setText(s);
						mArray[j].setSelection(tSelection);

					}
				}
			});
		}
		
	}



}


activity_main.xml

<?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="horizontal" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:ems="10"
        android:hint="@string/app_name"
        android:inputType="number" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/editText2"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:ems="10"
        android:hint="@string/app_name"
        android:inputType="number" />

    <EditText
        android:id="@+id/editText3"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:ems="10"
        android:hint="@string/app_name"
        android:inputType="number" />

    <EditText
        android:id="@+id/editText4"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:ems="10"
        android:hint="@string/app_name"
        android:inputType="number" />
 
</LinearLayout>


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值