Control the virtual keyboard in Android

Source: Internet
Author: User

 

1 package com. dome;
2
3 import android. app. Activity;
4 import android. content. Intent;
5 import android. OS. Bundle;
6 import android. view. KeyEvent;
7 import android. view. View;
8 import android. view. WindowManager;
9 import android. view. inputmethod. InputMethodManager;
10 import android. widget. Button;
11 import android. widget. Toast;
12
13 /**
14 * This is a Dome that controls the virtual keyboard.
15 *
16 * @ author wuxin
17 *
18 */
19 public class KeyCodeDomeActivity extends Activity {
20
21 // customize a button that can open the virtual keyboard
22 private Button button;
23
24/** Called when the activity is first created .*/
25 @ Override
26 public void onCreate (Bundle savedInstanceState ){
27 super. onCreate (savedInstanceState );
28 setContentView (R. layout. main );
29
30 // obtain its resources
31 button = (Button) findViewById (R. id. button1 );
32
33 // set its listening events
34 button. setOnClickListener (new View. OnClickListener (){
35
36 @ Override
37 public void onClick (View v ){
38 // TODO Auto-generated method stub
39
40 // obtain the Class Object of the control keyboard
41 InputMethodManager imm = (InputMethodManager) KeyCodeDomeActivity. this
42. getSystemService (INPUT_METHOD_SERVICE );
43
44 // open (automatically click the button again to disappear)
45 imm. toggleSoftInput (0, InputMethodManager. HIDE_NOT_ALWAYS );
46}
47 });
48
49}
50
51 /**
52 * When the button event is triggered (only the virtual keyboard, Return key, Home key, and menu key are allowed ....)
53 */
54 @ Override
55 public boolean onKeyDown (int keyCode, KeyEvent event ){
56 // TODO Auto-generated method stub
57
58 /*
59 * the button to determine whether to click is the return key/Note: Add permission: android. permission. BIND_INPUT_METHOD
60 */
61 if (keyCode = KeyEvent. KEYCODE_BACK & event. getRepeatCount () = 0 ){
62
63 Toast. makeText (KeyCodeDomeActivity. this, "I am the return key ",
64 Toast. LENGTH_SHORT). show ();
65
66 return true; // be sure to set it to true; otherwise, roll back
67
68}
69
70 /*
71 * determine whether to click the Home Key/Note: Add permission: android. permission. DISABLE_KEYGUARD
72 * important: the following onAttachedToWindow () method must be added.
73 */
74 if (KeyEvent. KEYCODE_HOME = keyCode ){
75
76 Toast. makeText (getApplicationContext (), "I am the Home Key ",
77 Toast. LENGTH_SHORT). show ();
78
79 return true;
80
81}
82
83 /*
84 * click the menu key
85 */
86 if (KeyEvent. KEYCODE_MENU = keyCode ){
87
88 Toast. makeText (getApplicationContext (), "I am the Menu key ",
89 Toast. LENGTH_SHORT). show ();
90
91 return true;
92
93}
94
95 /*
96 * determine whether the key clicked is the search key
97 */
98 if (KeyEvent. KEYCODE_SEARCH = keyCode ){
99
100 Toast. makeText (getApplicationContext (), "I am a search key ",
101 Toast. LENGTH_SHORT). show ();
102
103 return true;
104
105}
106
107
108 return super. onKeyDown (keyCode, event );
109}
110
111 /*
112 * (non-Javadoc)
113 * @ see android. app. Activity # onAttachedToWindow ()
114 */
115 public void onAttachedToWindow (){
116 this. getWindow (). setType (WindowManager. LayoutParams. TYPE_KEYGUARD );
117 super. onAttachedToWindow ();
118}
119
120}
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.