简体   繁体   中英

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? 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. Thanks in advance. :)

How can I change the visibility of an EditText view once the user stops typing...

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. 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:

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;
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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