有没有办法将密码文本从点(。)更改为星号(*)。
密码以edittext格式输入。
<EditText
android:id="@+id/passWord1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:inputType="number"
android:password="true"/>
答案 0 :(得分:55)
在xml文件中插入edittext,
<EditText
android:id="@+id/passWordEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:inputType="textPassword"/>
然后你的类文件继续从edittext获取findViewById并为此实现,
EditText edittext = (EditText)findViewById(R.id.passWordEditText);
edittext.setTransformationMethod(new AsteriskPasswordTransformationMethod());
和这个类的实现,
public class AsteriskPasswordTransformationMethod extends PasswordTransformationMethod {
@Override
public CharSequence getTransformation(CharSequence source, View view) {
return new PasswordCharSequence(source);
}
private class PasswordCharSequence implements CharSequence {
private CharSequence mSource;
public PasswordCharSequence(CharSequence source) {
mSource = source; // Store char sequence
}
public char charAt(int index) {
return '*'; // This is the important part
}
public int length() {
return mSource.length(); // Return default
}
public CharSequence subSequence(int start, int end) {
return mSource.subSequence(start, end); // Return default
}
}
};
答案 1 :(得分:19)
<EditText
android:id="@+id/passWord1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:inputType="textPassword"//here is the change. check it once in your xml
android:password="true"/>
在eclipse中,当您将光标移动到android:inputType
时,单击 Ctrl + Space 时会有提示。然后你可以看到选项列表。在那里你可以选择textPassword
如果您希望*
代替.
,请查看此Android: Asterisk Password Field
答案 2 :(得分:8)
在Ram kiran发布的链接的帮助下得到答案
text.setTransformationMethod(new AsteriskPasswordTransformationMethod());
public class AsteriskPasswordTransformationMethod extends PasswordTransformationMethod {
@Override
public CharSequence getTransformation(CharSequence source, View view) {
return new PasswordCharSequence(source);
}
private class PasswordCharSequence implements CharSequence {
private CharSequence mSource;
public PasswordCharSequence(CharSequence source) {
mSource = source; // Store char sequence
}
public char charAt(int index) {
return '*'; // This is the important part
}
public int length() {
return mSource.length(); // Return default
}
public CharSequence subSequence(int start, int end) {
return mSource.subSequence(start, end); // Return default
}
}
};
答案 3 :(得分:0)
对于android:inputType,有一种密码。
答案 4 :(得分:0)
试试这个
android:inputType="textPassword"
答案 5 :(得分:0)
以科特林方式:
AsteriskPasswordTransformationMethod类:PasswordTransformationMethod(){
override fun getTransformation(source: CharSequence?, view: View?): CharSequence {
return super.getTransformation(source, view)
}
abstract inner class PasswordCharSequence(val source: CharSequence) : CharSequence {
override val length: Int
get() = source.length
override fun get(index: Int): Char =
'*'
override fun subSequence(start: Int, end: Int): CharSequence {
return source.subSequence(start, end) // Return default
}
}
}`
答案 6 :(得分:0)
科特林
class AsteriskPasswordTransformationMethod : PasswordTransformationMethod() {
override fun getTransformation(source: CharSequence, view: View): CharSequence {
return PasswordCharSequence(source)
}
inner class PasswordCharSequence (private val source: CharSequence) : CharSequence {
override val length: Int
get() = source.length
override fun get(index: Int): Char = '*'
override fun subSequence(startIndex: Int, endIndex: Int): CharSequence {
return source.subSequence(startIndex, endIndex)
}
}
}