Analysis on usage of a soft keyboard in Android Development

Source: Internet
Author: User

Analysis on usage of a soft keyboard in Android Development

This document describes how to use a keyboard in Android development. Share it with you for your reference. The details are as follows:

Enable the soft keyboard in two ways. One is showSoftInput and the other is toggleSoftInput.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

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 ){

Super. 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.requestFocus ();

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_METHOD_SERVICE );

Imm. showSoftInput (ed2, InputMethodManager. SHOW_IMPLICIT );

}

Private void toggle (){

InputMethodManager imm = (InputMethodManager) getSystemService (Context. INPUT_METHOD_SERVICE );

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;

}

}

Before using the show method, you must first set its first parameter requestFocus. You can see the notes of the show method:

Synonym for showSoftInput (View, int, ResultReceiver) without a result handler Er: explicitly request that the current input method's soft input area be shown to the user, if needed.

The last two words if needed mean that if this method is called and it is indeed necessary to display the keyboard, the soft keyboard will pop up.

The toggle method allows you to open and close the keyboard at will.

I hope this article will help you design 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.