我有两个EditTexts
用户名和密码,每个都有文本“ENTER A USERNAME”和“ENTER A PASSWORD”作为默认文本。
它们的文本颜色是浅灰色。现在,当他们集中注意力时,他们应该将文本颜色以及密码 edittext 的 inputType 更改为密码类型(在其他事情之上但有效)。
它首先成功地将文本颜色更改为黑色,但无法改回并且 inputType 永远不会改变。
请建议我缺少的地方,下面是我的代码:
OnFocusChangeListener pwListener = new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus) {
//if it has focus then clear the text and make text black
if(((EditText) v).getText().toString().compareTo(getString(R.string.passwordField)) == 0) {
((EditText) v).setTextColor(R.color.Black);
((EditText) v).setText(R.string.blank);
((EditText) v).setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
}
} else if(!hasFocus) {
//if it loses focus and nothing was inputed then change back to default
if(isEmpty(((EditText) v).getText().toString())) {
((EditText) v).setTextColor(R.color.TextInput);
((EditText) v).setText(R.string.passwordField);
((EditText) v).setInputType(InputType.TYPE_TEXT_VARIATION_PERSON_NAME);
}
}
}
};
这就是密码编辑文本,这是它的 XML:
<EditText
android:id="@+id/passwordInput"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:layout_marginTop="5dip"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:paddingTop="15dip"
android:paddingBottom="15dip"
android:text="@string/passwordField"
android:textColor="@color/TextInput"
android:textSize="25sp"
android:gravity="center_vertical"
android:ems="10" >
</EditText>