如何隐藏键盘,以显示何时单击EditText



我有多个文本视图和一个编辑文本小部件。

这是我的问题:在我的应用启动后,当我单击EditText时,它会带有键盘。我已经设置了一个执行一些操作的OnTouchListener,这正常工作。

但是,在我的textView onclick侦听器中,每当我单击Edittext窗口小部件下方的任何文本视图时,我都会将EditText焦点设置为False。这可以正常工作,但是键盘不会消失。

那么,在将focusable((设置为false的text_view onclicklistener中,我如何隐藏键盘。

我不打算禁用键盘,这就是我想要的。单击显示键盘的EditText后,如果我单击下面的文本视图,我希望EditText的SetFususable设置为false(正在工作(并隐藏键盘(我不知道该如何实现(。

(。

这是我的代码

public class FacilityScreen extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_facilty_screen);
    EditText editText = (EditText) findViewById(R.id.enter_location);
    editText.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            view.setFocusable(true);
            view.setFocusableInTouchMode(true);
            if (MotionEvent.ACTION_UP == motionEvent.getAction()) {
                    TextView textView = (TextView) findViewById(R.id.enter_location);
                    textView.setBackgroundColor(Color.TRANSPARENT);
                }
            }
            return false;
        }
    });
}
public void text_view(View view) {
    EditText editText = (EditText) findViewById(R.id.enter_location);
    editText.setFocusable(false);
        TextView textView = (TextView) findViewById(R.id.my_text);
        textView.setBackgroundColor(Color.parseColor("#3F51B5"));
}

之后将其添加到您的方法中
editText.setFocusable(false);
editText.clearFocus();

如果那不起作用,请使用下面的方法解散键盘

 public static void dismissKeyboard(EditText editText, Context context) {
        InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
    }

希望这会有所帮助。

最新更新