2012-05-04 39 views
0

在编辑一个EditText我有两个EditTexts用户名和密码,每一个都具有文本“输入用户名”“输入密码”分别作为缺省的文本。问题而焦点变化

他们的文字颜色是浅灰色。现在,当他们专注于焦点时,他们应该将文本颜色以及密码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> 

回答

3

为什么你不设置输入用户名“和”请输入密码“作为提示文本使用

android:hint="Enter User Name" 

它会自动di在用户点击Edittext时出现。

+0

哇哈哈,将已经救了我这么多时间,如果我只是早点知道。谢谢 –

+0

Upvote并接受帖子,如果你觉得有用。 –

0

使用,而不是

android:hint="@string/passwordField" 

android:text="@string/passwordField" 

并做用户名

1

在XML同样使用Hint属性值设置为默认值。

,如:

android:hint="@string/username"

android:hint="@string/password"

相反

android:text="@string/username"

android:text="@string/password"