android edittext remove focus after clicking a but

2020-05-20 05:15发布

I have an Activity with an EditText and a Button. When the User clicks on the EditText, the keyboard is shown and he can type in some Text - fine. But when the user clicks on the Button I want the EditText to be no more in focus i.e. the keyboard hides til the user clicks again on the EditText.

What can I do to 'hide the focus' of the EditText, after the Button is clicked. Some Code I can add in the OnClick Method of the Button to do that?

EDIT:

<LinearLayout 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <EditText 
        android:id="@+id/edt_SearchDest"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="0.8"
        android:textSize="18sp"
        android:hint="Enter your look-up here.." />

    <Button
        android:id="@+id/btn_SearchDest"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="0.2"
        android:text="Search" />

</LinearLayout>

Best Regards

9条回答
Anthone
2楼-- · 2020-05-20 05:20
mTextView.setEnabled(!mTextView.isEnabled());
mTextView.setEnabled(!mTextView.isEnabled());
查看更多
我命由我不由天
3楼-- · 2020-05-20 05:21

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

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

Somewhat less complex than other methods...

查看更多
Rolldiameter
4楼-- · 2020-05-20 05:22

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.

查看更多
Bombasti
5楼-- · 2020-05-20 05:24

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

edt_SearchDest.setEnabled(false);

查看更多
▲ chillily
6楼-- · 2020-05-20 05:31

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.
查看更多
姐就是有狂的资本
7楼-- · 2020-05-20 05:33

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.

查看更多
登录 后发表回答