How to hide keyboard just by one tap outside of an edittext?

喜你入骨 提交于 2019-12-08 22:10:11

问题


I want to hide the keyboard by tapping outside of edittext. This is my xml code:

<RelativeLayout
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:onClick="rl_main_onClick">
<RelativeLayout
  //Here there are some widgets including some edittext.
</RelativeLayout>

This is my java code (MainActivity):

public void rl_main_onClick(View view) {
    InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}

But I have to tap twice to hide keyboard. The first tap just changes "next" (for last edittext it's "done") to "enter" icon, then the second tap hides the keyboard. This is what that happen by the first tap:

Now I have two questions:

  1. How can I fix it and hide keyboard by just one tap?

  2. Is it possible to do it for all of my edittext (one code for all)?


回答1:


Try to replace onClick with onTouch. For this you need to change your layout attributes like this:

<RelativeLayout
    android:id="@+id/relativeLayout"
    android:clickable="true"
    android:focusable="true"
    android:focusableInTouchMode="true">

    <RelativeLayout>

        // widgets here

    </RelativeLayout>

</RelativeLayout>

Then remove rl_main_onClick(View view) {...} method and insert onTouch listener method inside of onCreate() :

findViewById(R.id.relativeLayout).setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
        return true;
    }
});



回答2:


Kotlin version with extension

fun View.hideKeyboard() {
    val inputMethodManager = context!!.getSystemService(android.content.Context.INPUT_METHOD_SERVICE) as? InputMethodManager
    inputMethodManager?.hideSoftInputFromWindow(this.windowToken, 0)
}

And call it like this from fragment

view.setOnClickListener {
    it.hideKeyboard()
}

Or like this from activity, contentView is the id of my root view

val contentView: View = findViewById(R.id.contentView)
    contentView.setOnClickListener {
    it.hideKeyboard()
}



回答3:


I use below Process. It's Working For Me Perfectly. Add below function in your activity Class.

  override fun dispatchTouchEvent(event: MotionEvent): Boolean {
if (event.action == MotionEvent.ACTION_DOWN) {
    val v = currentFocus
    if (v is EditText) {
        val outRect = Rect()
        v.getGlobalVisibleRect(outRect)
        if (!outRect.contains(event.rawX.toInt(), event.rawY.toInt())) {
            Log.d("focus", "touchevent")
            v.clearFocus()
            val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
            imm.hideSoftInputFromWindow(v.windowToken, 0)
        }
    }
}
return super.dispatchTouchEvent(event)}

You can check Focus status using Below Code. Both activity and Fragment

appCompatEditText.onFocusChangeListener = View.OnFocusChangeListener { view, hasFocus ->
            if (!hasFocus) {
                toast("Focus Off")
            }else {
                toast("Focus On")
            }
        }



回答4:


@Override public boolean dispatchTouchEvent(MotionEvent ev) { if (getCurrentFocus() != null) { InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0); } return super.dispatchTouchEvent(ev); }



来源:https://stackoverflow.com/questions/40452321/how-to-hide-keyboard-just-by-one-tap-outside-of-an-edittext

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!