当虚拟键盘消失时,不再关注EditText

时间:2018-02-12 15:43:11

标签: android

我对某些文字字段有点问题。当我关闭虚拟键盘时,我想失去对它们的关注。

假设我选择了一个字段,我在那里写了一些东西,而不是按回来关闭键盘。在这种情况下,我希望自动失去对所选文本字段的关注。

这是我到目前为止所尝试的内容。问题是,如果我从searchByName按完成(输入),它会立即切换到第二个文本字段(searchByZipcode),但不会应用focusChangeListener。它仅在我关闭键盘后才适用。

searchByName.setOnFocusChangeListener(new View.OnFocusChangeListener() {

        @Override
        public void onFocusChange(View v, boolean hasFocus) {

            if (hasFocus) {
                v.getLayoutParams().width = 900;
                searchByZipcode.setVisibility(View.INVISIBLE);
                searchByDistance.setVisibility(View.INVISIBLE);
            } else {
                v.getLayoutParams().width=405;
                searchByZipcode.setVisibility(View.VISIBLE);
                searchByDistance.setVisibility(View.VISIBLE);

            }
        }
    });


    searchByZipcode.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            searchByZipcode.getLayoutParams().width=660;
            searchByName.setVisibility(View.INVISIBLE);
        }
    });

    searchByZipcode.setOnFocusChangeListener(new View.OnFocusChangeListener() {

        @Override
        public void onFocusChange(View v, boolean hasFocus) {
    /* When focus is lost check that the text field
    * has valid values.
    */
            if (hasFocus) {
                v.getLayoutParams().width=663;
                searchByName.setVisibility(View.INVISIBLE);
            } else {
                v.getLayoutParams().width=240;
                searchByName.setVisibility(View.VISIBLE);
            }
        }
    });


    searchByName.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View view, int keyCode, KeyEvent event) {
            if (event.getAction() != KeyEvent.ACTION_DOWN) {
                return false;
            }
            if(keyCode == KeyEvent.KEYCODE_ENTER)
            {
                searchByName.clearFocus();
                return true;
            }
            else
            {
                return false;
            }
        }
    });

    searchByZipcode.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View view, int keyCode, KeyEvent event) {
            if (event.getAction() != KeyEvent.ACTION_DOWN) {
                return false;
            }
            if(keyCode == KeyEvent.KEYCODE_ENTER)
            {
                searchByZipcode.clearFocus();
                return true;
            }
            else
            {
                return false;
            }
        }
    });

    searchByDistance.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View view, int keyCode, KeyEvent event) {
            if (event.getAction() != KeyEvent.ACTION_DOWN) {
                return false;
            }
            if(keyCode == KeyEvent.KEYCODE_ENTER)
            {
                searchByDistance.clearFocus();
                return true;
            }
            else
            {
                return false;
            }
        }
    });

1 个答案:

答案 0 :(得分:1)

要做到这一点,你可以通过这样做清除当前的焦点:

@Override
public void onBackPressed() {
    super.onBackPressed();

    View view = getCurrentFocus();
    if (view != null) {
        view.clearFocus();
    }
}