简体   繁体   English

EditText:一旦用户完成输入,如何更改可见性?

[英]EditText:how to change visibility once the user has done finished typing?

How can I change the visibility of an EditText view once the user stops typing or clicks done? 用户停止键入或单击完成后,如何更改EditText视图的可见性? I tried to do it this but I got an error message. 我尝试这样做,但收到错误消息。

    package com.example.android.monopolycredit;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

public class LoginLobby extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_lobby);

        final EditText usernameEditText = findViewById(R.id.usernameField);
        usernameEditText.setOnEditorActionListener(
                new EditText.OnEditorActionListener() {
                    @Override
                    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                        if (actionId == EditorInfo.IME_ACTION_SEARCH ||
                                actionId == EditorInfo.IME_ACTION_DONE ||
                                event.getAction() == KeyEvent.ACTION_DOWN &&
                                        event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
                            if (!event.isShiftPressed()) {
                                // the user is done typing.
                                usernameEditText.setVisibility(View.GONE);
                                return true; // consume.
                            }
                        }
                        return false; // pass on to other listeners.
                    }
                });
    }

}

Any help would be appreciated. 任何帮助,将不胜感激。 I'm new to android development. 我是android开发的新手。 Thanks in advance. 提前致谢。 :) :)

How can I change the visibility of an EditText view once the user stops typing... 用户停止键入后,如何更改EditText视图的可见性...

There is no such event which tells you that user has stopped typing. 没有此类事件可以告诉您用户已停止键入。 Even if user stops typing you still have a focus in that EditText field which means that he can proceed typing. 即使用户停止键入,您仍然可以在该EditText字段中保持焦点 ,这意味着他可以继续键入。 Therefore in this case, you can't determine if he really stopped entering text. 因此,在这种情况下,您无法确定他是否真的停止输入文本。

or clicks done? 或点击完成?

That's already possible. 那已经有可能了。 In order to listen to keyboard changes such as done action just add actionId == EditorInfo.IME_ACTION_DONE to your existing code: 为了收听键盘更改(例如完成的操作),只需将actionId == EditorInfo.IME_ACTION_DONE添加到现有代码中:

EditText usernameEditText = findViewById(R.id.usernameField);
usernameEditText.setOnEditorActionListener(new EditText.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event){
        if (actionId == EditorInfo.IME_ACTION_DONE && !event.isShiftPressed()) {
            // the user is done typing.
            usernameEditText.setVisibility(View.GONE);
            return true;
        }
        return false;
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM