Add a Button to Hide Keyboard

Add a button to hide keyboard

You can assign a toolbar with a button that dismisses the keyboard as the text field's inputAccessoryView. A quick example would be,

UIBarButtonItem *barButton = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:textField action:@selector(resignFirstResponder)] autorelease];
UIToolbar *toolbar = [[[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)] autorelease];
toolbar.items = [NSArray arrayWithObject:barButton];

textField.inputAccessoryView = toolbar;

Android:Hide keyboard after button click

You could instead set it to your layout, ie:

LinearLayout mainLayout;

// Get your layout set up, this is just an example
mainLayout = (LinearLayout)findViewById(R.id.myLinearLayout);

// Then just use the following:
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mainLayout.getWindowToken(), 0);

This is an example assuming that your layout will be created regardless of how many EditText objects (or other objects) are placed on it.

Edit: Also, something I find very useful is to make sure that the keyboard is hidden when an activity first launches (ie: if an EditText is the first thing focused). To do that, I put this in onCreate() method of Activity:

 this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

Hide keyboard by pressing button

[self.view endEditing:YES]

Paste it in action handler of your controller.
Or for Swift:

self.view.endEditing(true)

On Button Click Hide Keyboard

public void dismissKeyboard(Activity activity) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
if (null != activity.getCurrentFocus())
imm.hideSoftInputFromWindow(activity.getCurrentFocus()
.getApplicationWindowToken(), 0);
}

Activity have to be passed to this method, Keyboard will get dismissed.

How do I make an Android EditView 'Done' button and hide the keyboard when clicked?

Use TextView.setImeOptions and pass it actionDone.
like textView.setImeOptions(EditorInfo.IME_ACTION_DONE);



Related Topics



Leave a reply



Submit