Mainactivity such as the following:
Package Cc.c;import Android.os.bundle;import Android.view.view;import android.view.view.onclicklistener;import Android.view.inputmethod.inputmethodmanager;import Android.widget.button;import Android.widget.EditText;import Android.app.activity;import android.content.context;/** * Demo Description: * 1 Toggle of soft Keyboard status * 2 force to hide IME keyboard */public class mainactivity Extends Activity {private EditText medittext; Private Button Mbutton; Private Context mcontext; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (R.layout.main); init ();} private void Init () {mcontext=this;medittext= (EditText) Findviewbyid (R.id.edittext); mbutton= (Button) Findviewbyid ( R.id.button); Mbutton.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View v) {//toggleinput ( Mcontext); Hideinput (Mcontext,medittext);});} /** * Toggle the status of the soft keyboard * If the current is closed to pop, if the current pop-up into a closed */private void Toggleinput (context context) {Inputmethodmanager Inputmethodmanager = (Inputmethodmanager) conteXt.getsystemservice (Context.input_method_service); Inputmethodmanager.togglesoftinput (0, InputMethodManager.HIDE _not_always);} /** * Force hidden IME keyboard */private void Hideinput (Context context,view View) {Inputmethodmanager Inputmethodmanager = ( Inputmethodmanager) Context.getsystemservice (Context.input_method_service); Inputmethodmanager.hidesoftinputfromwindow (View.getwindowtoken (), 0);}}
Main.xml such as the following:
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android:paddi ngbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_horizontal_margin" Android: paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" tools: Context= ". Mainactivity "> <edittext android:id=" @+id/edittext "android:layout_width=" Wrap_content "and roid:layout_height= "Wrap_content" android:layout_centerinparent= "true" android:hint= "Input here"/> &L T Button android:id= "@+id/button" android:layout_width= "wrap_content" android:layout_height= "Wrap_conte NT "android:layout_above=" @id/edittext "android:layout_centerhorizontal=" true "android:layout_marginb Ottom= "15DP" android:hint= "button"/></relativelayout>
Switching of Android soft keyboard state and its forced hiding