Recently, I was working on a TV input box and found it difficult to use the full keyboard. Because the TV only has a remote control, it is inconvenient to choose the right or left, so I implemented a fixed keyboard on the page, in T9 format, first:
Technical points:
1. Use popupwindow to implement the pop-up layer
2. Set the same onclick event for each key to facilitate unified processing
Code:
Activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="900dip" android:layout_height="match_parent" android:layout_gravity="center_horizontal" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="20dip" android:orientation="horizontal" > <TextView android:id="@+id/num_1" style="@style/KeyboardNum" android:text="@string/num_1" /> <TextView android:id="@+id/num_2" style="@style/KeyboardNum" android:text="@string/num_2" /> <TextView android:id="@+id/num_3" style="@style/KeyboardNum" android:text="@string/num_3" /> <TextView android:id="@+id/num_backspace" style="@style/KeyboardNum" android:nextFocusRight="@+id/num_4" android:text="@string/num_backspace" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:id="@+id/num_4" style="@style/KeyboardNum" android:nextFocusLeft="@+id/num_backspace" android:text="@string/num_4" /> <TextView android:id="@+id/num_5" style="@style/KeyboardNum" android:text="@string/num_5" /> <TextView android:id="@+id/num_6" style="@style/KeyboardNum" android:text="@string/num_6" /> <TextView android:id="@+id/num_clear" style="@style/KeyboardNum" android:nextFocusRight="@+id/num_7" android:text="@string/num_clear" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:id="@+id/num_7" style="@style/KeyboardNum" android:nextFocusLeft="@+id/num_clear" android:text="@string/num_7" /> <TextView android:id="@+id/num_8" style="@style/KeyboardNum" android:text="@string/num_8" /> <TextView android:id="@+id/num_9" style="@style/KeyboardNum" android:text="@string/num_9" /> <TextView android:id="@+id/num_0" style="@style/KeyboardNum" android:text="@string/num_0" /> </LinearLayout></LinearLayout>
Popup_window_keyboard.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_horizontal" android:orientation="vertical" android:background="@drawable/normal_focus" android:padding="10dip" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" > <TextView android:id="@+id/key_2" style="@style/KeyboardKey" android:text="@string/key_2" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" > <TextView android:id="@+id/key_1" style="@style/KeyboardKey" android:text="@string/key_1" /> <TextView android:id="@+id/key_0" style="@style/KeyboardKey" android:layout_width="70dip" android:text="@string/key_0" /> <TextView android:id="@+id/key_3" style="@style/KeyboardKey" android:text="@string/key_3" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" > <TextView android:id="@+id/key_4" style="@style/KeyboardKey" android:text="@string/key_4" /> </LinearLayout></LinearLayout>
Atvivity_main.java code:
Private textview num1; private keyboardwindow mkeyboardwindow; @ override protected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); initui ();}/*** initialize UI */private void initui () {getviews (); // obtain the control num1.requestfocus (); // set the default focus initlist (); // initialize the list setlistener (); // set the listener}/*** obtain the page control */private void getviews () {mkeyboardwindow = new keyboardwindow (this); num1 = (textview) findviewbyid (R. id. num_1);} private void setlistener () {// Listener Input string}/*** keyboard click Processing * @ Param v */Public void onclicknum (view V) {Switch (v. GETID () {case R. id. num_backspace: string value = searchkeyedit. gettext (). tostring (); searchkeyedit. settext (value. substring (0, value. length ()-1); break; case R. id. num_clear: searchkeyedit. settext (""); break; default: mkeyboardwindow. show (V); break;}/*** keyboard click Processing * @ Param v */Public void onclickkey (view v) {string value = (textview) V ). gettext (). tostring (); If ("null ". equals (value) {value = "" ;}// append the character in the todo input box // hide the pop-up layer mkeyboardwindow. dismiss ();}
Keyboardwindow. Java
Public class keyboardwindow extends popupwindow {private context mcontext; private textview key0; private textview key1; private textview key2; private textview key3; private textview key4; Public keyboardwindow (context) {super (context); mcontext = context; layoutinflater Inflater = layoutinflater. from (mcontext); // introduce the window configuration file view = Inflater. inflate (R. layout. popup_window_keyboard, null); key0 = (textview) view. findviewbyid (R. id. key_0); key1 = (textview) view. findviewbyid (R. id. key_1); key2 = (textview) view. findviewbyid (R. id. key_2); key3 = (textview) view. findviewbyid (R. id. key_3); key4 = (textview) view. findviewbyid (R. id. key_4); this. setcontentview (View); this. setbackgrounddrawable (New bitmapdrawable (); // remove the original background image this. setanimationstyle (R. style. popupanimation); // sets the animation to pop up and hide this. setwidth (layoutparams. wrap_content); this. setheight (layoutparams. wrap_content); this. setfocusable (true); // obtain the focus of the menu. If the control event in the focus menu is not obtained, the system cannot respond.} public void show (view v) {// obtain the view ID, and initialize the key letter initui (v. GETID (); super. showasdropdown (v,-5,-80); key0.requestfocus () ;}@ override public void dismiss () {// todo auto-generated method stub super. dismiss ();} private void initui (int id) {key0.setvisibility (view. visible); key1.setvisibility (view. visible); key2.setvisibility (view. visible); key3.setvisibility (view. visible); key4.setvisibility (view. visible); Switch (ID) {case R. id. num_1: key0.settext ("1"); key1.settext (". "); key2.setvisibility (view. invisible); key3.setvisibility (view. invisible); key4.setvisibility (view. invisible); break; case R. id. num_2: key0.settext ("2"); key1.settext ("A"); key2.settext ("B"); key3.settext ("C"); key4.setvisibility (view. invisible); break; case R. id. num_3: key0.settext ("3"); key1.settext ("D"); key2.settext ("e"); key3.settext ("F"); key4.setvisibility (view. invisible); break; case R. id. num_4: key0.settext ("4"); key1.settext ("G"); key2.settext ("H"); key3.settext ("I"); key4.setvisibility (view. invisible); break; case R. id. num_5: key0.settext ("5"); key1.settext ("J"); key2.settext ("K"); key3.settext ("L"); key4.setvisibility (view. invisible); break; case R. id. num_6: key0.settext ("6"); key1.settext ("M"); key2.settext ("N"); key3.settext ("O"); key4.setvisibility (view. invisible); break; case R. id. num_7: key0.settext ("7"); key1.settext ("p"); key2.settext ("Q"); key3.settext ("R"); key4.settext ("S"); break; case R. id. num_8: key0.settext ("8"); key1.settext ("T"); key2.settext ("U"); key3.settext ("v"); key4.setvisibility (view. invisible); break; case R. id. num_9: key0.settext ("9"); key1.settext ("W"); key2.settext ("X"); key3.settext ("Y"); key4.settext ("Z"); break; case R. id. num_0: key0.settext ("0"); key1.settext ("null"); // key1.setbackgroundresource (R. drawable. ic_launcher); key2.setvisibility (view. invisible); key3.setvisibility (view. invisible); key4.setvisibility (view. invisible); break ;}}}