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

How do i hide the keyboard from showing when EditText is clicked on

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

这是我的问题: 在我的应用程序启动后,当我单击 EditText 时,它会与键盘一起显示。我已经设置了一个 onTouchListener 来执行一些操作,它工作正常。

但是,在我的 textView onClick 侦听器中,每当我单击 EditText 小部件下方的任何 textView 时,我都会将 EditText 可聚焦设置为 false。这可以正常工作,但键盘不会消失。

那么如何在 text_view onClicklistener 中将 focusable() 设置为 false 后隐藏键盘。

我不打算禁用键盘,这就是我想要的;单击显示键盘的 EditText 后,如果我单击下面的 textView,我希望将 EditText 的 setFocusable 设置为 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);
    }

希望对您有所帮助。