android edittext remove focus after clicking a button

 ̄綄美尐妖づ 提交于 2019-12-03 00:13:32
Michael Yaworski

Put this in your button listener:

InputMethodManager inputManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 

inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);

EDIT

The solution above will break your app if no EditText is focused on. Modify your code like this:

add this method to you class:

public static void hideSoftKeyboard (Activity activity, View view) 
{
    InputMethodManager imm = (InputMethodManager)activity.getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(view.getApplicationWindowToken(), 0);
}

Then, in your button listener, call the method like this:

hideSoftKeyboard(MainActivity.this, v); // MainActivity is the name of the class and v is the View parameter used in the button listener method onClick.
Mike Redrobe

I've successfully used the following in the onClick button code:

editText.setEnabled(false);
editText.setEnabled(true);

Somewhat less complex than other methods...

cuasodayleo

One workaround is to create a fake view to transfer focus to when you clearFocus in your edittext:

<EditText
        android:id="@+id/edt_thief"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:focusable="true"
        android:focusableInTouchMode="true"

Note that this view is invisible so it doesn't require any space in the layout.

In the control class, you can add a method like the following to trigger this focus transfer:

public void clearFocus(){
        yourEdittext.clearFocus();
        edtThief.requestFocus();
    }

You can then minimize the keyboard once edtThief has focus:

 public static void hideKeyboard(final View view) {
    InputMethodManager imm = (InputMethodManager) view.getContext()
            .getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}

The most elegant solution that I could find is this:

You can save this method in your utility class:

public static void hideSoftKeyboard(Activity activity) {
   if (activity == null) return;
   if (activity.getCurrentFocus() == null) return;

   InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
   inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
}

By simply calling hideSoftKeyboad() method it will hide the keyboard but as you can see, the focus will still be present.

In order to remove the focus we will use a simple trick. Right above your input controls, add a dummy view like this:

<View
    android:id="@+id/dummy"
    android:layout_width="1dp"
    android:layout_height="1dp"
    android:focusable="true"
    android:focusableInTouchMode="true" />

Then, write this line of code at the place where you call the focus-hiding method:

 theTextView.clearFocus();

Since the app needs to pass the focus to the next control it will be passed to our invisible view.

Exceptional
private void hideDefaultKeyboard() {
    activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);//u have got lot of methods here
}

EDIT:

LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN
grebenyuksv
mTextView.setEnabled(!mTextView.isEnabled());
mTextView.setEnabled(!mTextView.isEnabled());

Sorry late to the answer, but I hope this will be the right answer, as I fixed it using

try {
InputMethodManager imm = (InputMethodManager) context
                    .getSystemService(Context.INPUT_METHOD_SERVICE);
if (v != null) {
imm.hideSoftInputFromWindow(v.getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
}
} catch (Exception ignored) {
}
mEditText.setSelected(false);
mEditText.setFocusable(false);
mEditText.setFocusableInTouchMode(true);

Write the following snippet on your button click.

How i solved it.

// xml file
<LinearLayout
...
android:id="@+id/linear_layout"
android:focusableInTouchMode="true"> // 1. make this focusableInTouchMode...
</LinearLayout>

// Activity file
private LinearLayout mLinearLayout; // 2. parent layout element
private Button mButton;

mLinearLayout = findViewById(R.id.linear_layout);
mButton = findViewById(R.id.button);

  mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mLinearLayout.requestFocus(); // 3. request focus

            }
        });

I hope this helps you :)

Why not just disable the EditText in the Button code? That should get rid of the keyboard and the focus.

edt_SearchDest.setEnabled(false);

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