必须在键盘上按两次键才能登录

时间:2016-11-23 12:11:02

标签: android performance android-layout android-studio

我正在尝试构建一个登录表单。这是我的密码字段xml:

<android.support.design.widget.TextInputLayout
        android:id="@+id/password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="4dp"
        app:errorEnabled="true">

        <android.support.design.widget.TextInputEditText
            android:id="@+id/password_input"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/password"
            android:inputType="textPassword"
            android:imeOptions="actionGo"/>
    </android.support.design.widget.TextInputLayout>

我正在收听go键的loginActivity如下所示:

@OnClick(R.id.password_input)
public void Start() {
    EditText editText = (EditText) findViewById(R.id.password_input);
    editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            boolean handled = false;
            if (actionId == EditorInfo.IME_ACTION_GO) {
                logIn();
                handled = true;
            }
            return handled;
        }
    });
}

当我按下密码字段时,会弹出一个键盘然后我必须按两次go键来调用logIn()函数。可能是什么原因以及如何解决这个问题?

4 个答案:

答案 0 :(得分:2)

更改onEditorAction中的条件,如下所示:

EditText editText = (EditText) findViewById(R.id.password_input);
    editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {

            if (actionId == EditorInfo.IME_ACTION_GO) {
                logIn();

            }
            return false;
        }
    });

我创建了一个测试用例,请检查一下:

在xml布局中添加:

<EditText
        android:id="@+id/edtInput"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:imeOptions="actionGo"
        android:inputType="textPassword" />

在您的Activity / Fragment代码中检查:

EditText edtInput = (EditText) findViewById(R.id.edtInput);
        edtInput.setImeOptions(EditorInfo.IME_ACTION_GO);
        edtInput.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_GO) {
                    Toast.makeText(MainActivity.this, " GO ", Toast.LENGTH_SHORT).show();
                }
                return false;
            }
        });

现在运行并检查它是否适用于我,它也适用于你。

最后我得到了解决方案,我们正在做一个愚蠢的错误,你能否删除

@OnClick(R.id.password_input)
public void Start() {
如果没有使用,

代码行,这是不会让所有事情发生的问题。

将此代码直接添加到Activity的onCreate(),无需向EditText提供click事件:

EditText editText = (EditText) findViewById(R.id.password_input);
    editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_GO) {
                logIn();

            }
            return false;
        }
    });

答案 1 :(得分:1)

只需替换你的if条件。

  if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
        Log.e(TAG, "enter_key_pressed");
    }

答案 2 :(得分:0)

@OnClick(R.id.password_input)
public void Start() {
    if (doublePressed) {
        //your intent or Operation
            return;
    }
    this.doublePressed = true;
    Toast.makeText(this, "Please click again to do”, Toast.LENGTH_SHORT).show();

    new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {
            doublePressed=false;                       
        }
    }, 2000);
}

答案 3 :(得分:0)

@OnClick(R.id.password_input)
public void Start() { 
EditText editText = (EditText) findViewById(R.id.password_input);
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override 
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {

        if (actionId == EditorInfo.IME_ACTION_GO) {
            logIn(); 
            return true;
        } 
        return false;
    } 
}); 
}