This example describes the soft keyboard usage in Android development. Share to everyone for your reference. Specifically as follows:
There are two ways to open a soft keyboard. One is Showsoftinput, one is togglesoftinput.
Package com.example.dd;
Import android.app.Activity;
Import Android.content.Context;
Import Android.os.Bundle;
Import Android.view.Menu;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.view.inputmethod.InputMethodManager;
Import Android.widget.Button;
Import Android.widget.EditText; public class Mainactivity extends activity {@Override protected void onCreate (Bundle savedinstancestate) {sup
Er.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Final EditText ed2 = (edittext) Findviewbyid (R.ID.EDITTEXT2);
Button B1 = (button) Findviewbyid (R.id.button1); B1.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {ED2.REQUESTFO
Cus ();
Show (ED2);
}
});
Button B2 = (button) Findviewbyid (R.id.button2);
B2.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {toggle (); }
}); } private void Show (EditText ed2) {Inputmethodmanager IMM = (inputmethodmanager) getsystemservice (Context.input_met
Hod_service);
Imm.showsoftinput (Ed2, inputmethodmanager.show_implicit); private void Toggle () {Inputmethodmanager IMM = (inputmethodmanager) getsystemservice (Context.input_method_servic
E);
Imm.togglesoftinput (0, 0); @Override public boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action Bar
If it is present.
Getmenuinflater (). Inflate (R.menu.main, menu);
return true; }
}
The Show method must first requestfocus its first argument before it can be used. You can see the comments for the Show method:
Synonym for Showsoftinput (View, int, resultreceiver) without a result receiver:explicitly request this current input Method ' s soft input area is shown to the user, if needed.
The last if needed two words means that the soft keyboard pops up if the method is invoked and it is really necessary to display the keyboard.
The toggle method can open and close the soft keyboard at will.
I hope this article will help you with your Android program.