带有文本密码输入类型的Android EditText不会通过双击选择所有字符

时间:2017-08-20 05:58:06

标签: android android-edittext passwords selectall

我正在尝试将EditTexttextPassword输入类型一起使用。我遇到了这样一种情况:所有进入该领域的角色都是在Android版本5和4.4中通过双击选择的,而这个功能在Android版本6上不起作用。 你能帮我在所有Android版本中使用这个功能吗?

这是我的xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <EditText
        android:hint="Text Password Input Type"
        android:inputType="textPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

结果如下:

enter image description here

1 个答案:

答案 0 :(得分:0)

通用算法将是:

private long timeLastClicked;
private static final TIME_DOUBLE_CLICK_MS = 1000;
private EditText passwordField;  //should put the result of findViewById in here

...

myEditText.setOnClickListener(new OnClickListener() {
    public void onClick(View view) {
        long time = System.currentTimeMillis();
        if(time - timeLastClicked < TIME_DOUBLE_CLICK_MS) {
           passwordField.setSelection(0,passwordField.getText().length());
        }
        timeLastClicked = time;
});

这是做什么的 - 它设置了一个点击监听器。当您看到一个单击时,检查您是否在前一秒中看到了另一次单击。如果是这样,你就会使文字变暗。如果没有,你就不会。无论哪种方式,您都可以节省此次点击​​的时间,以便下次进行比较。