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
Watchkit: Unable to Find Interface Controller Class
To Change the Color of Unselected Uitabbar Icon in iOS 7
Exception with Insertobject:Atindex: on iOS6
Swrevealviewcontroller Close Rear View When Tapping Front View
Core Data Store Included in App Bundle
Header Displaced in Tableview with Uirefreshcontrol
Dynamic Uicollectionview Header Size Based on Uilabel
Apple MACh-O Linker & Ditto Error - Xcode 8
Opening Word,Excel, and PDF Files Without Using Uiwebview on iOS
In Os X 10.10 (Yosemite Beta), How to Test Using iOS 6.1 Simulator
Sfspeechrecognizer - Detect End of Utterance
How to Do Something Before Unwind Segue Action
Move Gmsmarker on Google Map Like Uber
Ios: Does Force Quitting the App Disables Background Upload Using Nsurlsession
While Mail Sending Process Using iOS 8 Simulators Generates Error