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>