An example analysis of soft keyboard usage in Android development _android

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.