0

I have an EditText like this in my xml file:

<EditText
    android:id="@+id/InputPass"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="right"
    android:layout_marginBottom="20dp"
    android:layout_marginLeft="40dp"
    android:layout_marginRight="40dp"
    android:drawableLeft="@drawable/ic_lock"
    android:gravity="right"
    android:hint="رمز عبور"
    android:singleLine="true"
    android:textSize="18sp" />

For some reason I want to change its inputType after user entered first Character. I try this way:

Pass.addTextChangedListener(new TextWatcher() {

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

            }

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

                if(start == 0){
                    Pass.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });

But it does not work! Is anybody there that know any solution for that?

4

3 回答 3

1

改变 :

Pass.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);

至 :

Pass.setTransformationMethod(PasswordTransformationMethod.getInstance());
于 2014-10-11T20:44:54.083 回答
0

在 - 的里面

afterTextChanged

方法,尝试以下方法:

  @Override
        public void afterTextChanged(Editable s) {
            System.out.println("Editable: "+s);
            if(s.getText().toString().length ==1)
               Pass.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
        }
于 2014-10-11T20:25:14.530 回答
0

我通过这个达到了最好的结果:

Pass.setOnFocusChangeListener(new OnFocusChangeListener() {

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if(hasFocus){
            Pass.setTransformationMethod(PasswordTransformationMethod.getInstance());
        }
    }
});
于 2014-12-05T00:53:06.423 回答