Android custom keyboard (fix pop-up tips for font color issues)

Source: Internet
Author: User

Recently prepared to do a project, you need to use a custom keypad to ensure security, but also need to accurately obtain the user click on the keyboard location, intensity, fingertip touch screen area and other parameters.

In the writing of custom keyboard, the use of the domestic network of some code, the source is

http://blog.csdn.net/hfsu0419/article/details/7924673

Salute the Ancestors!

Then I found that the down code was used in my project and there were a variety of problems:

1. First, is an open application, will appear to pop up is the system input keyboard, but do not customize the keyboard, this problem is because the EditText will be opened in the application of the use of the focus, resulting in direct pop-up system input, rather than custom input method. The solution is to make EditText not get the focus when the app is open, so I have the global layout Add to the label

Android:focusable= "true"   android:focusableintouchmode= "true"

Then, when the activity runs,EditText loses focus, and the system input method does not pop up. Then Add a touch listener to the EditText, and when you click the EditText control, the response pops up our keyboard

Et.setontouchlistener (New View.ontouchlistener () {@Overridepublic Boolean onTouch (View V, motionevent event) {int Inputback = Et.getinputtype (); Et.setinputtype (Inputtype.type_null); Advantagekeyboard KB = new Advantagekeyboard (act,ctx,et); Kb.showkeyboard (); Et.setinputtype (Inputback); V.performclick (); return false;});


2. when clicked, there is no font in the keyboard pop-up prompt box. such as this


This is due to the display of the font when white, and the background is also white caused. Later a variety of Baidu, some people say becauseThemethe problem because I used to beandroid5.0.1of theAPI, when the project is automatically generated, the generatedActivityis directly inheritedactionbaractivity, directly change theThemewill cause a second fallback to occur. If you want to changeThemewe have to change the inheritance firstactionbaractivityto beActivity, and then to changeTheme, but I do not like this, because it is not universal, and later inGooglea foreign post, only to understand this pop-up thing calledPreview, we can modify the layout of it.

Add the android:keypreviewlayout tag to our custom Keyboardview and add the following:

<pre name= "code" class= "HTML" > <android.inputmethodservice.keyboardview              android:id= "@+id/keyboard_ View "              android:layout_width=" fill_parent "              android:layout_height=" wrap_content "              android:layout_ Alignparentbottom= "true"              android:focusable= "true"              android:focusableintouchmode= "true"              Android: background= "@color/black"              android:keybackground= "@drawable/btn_keyboard_key"            android:keypreviewlayout= "@layout/key_preview_layout"            android:keytextcolor= "@color/white"    android:visibility= "Gone"/>  


Then in the Layout folder, create a new key_preview_layout.xml file, which writes

<?xml version= "1.0" encoding= "Utf-8"? ><textview xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Wrap_content "android:layout_height=" wrap_content "android:textsize=" 40sp "Android: Textcolor= "@android: Color/white" android:gravity= "center" android:background= "#ff8888ff"/>

where android:background is the background color of our cue box,android:textcolor is the font color, change to you like the OK up.



3. Add the touch response of the keyboard and record the Click Information (intensity, contact area, landing point coordinates, etc.).

We can add a touch listener to the Keyboardview in a constructor in a custom keyboard , such as writing a construction method.

Public Advantagekeyboard (Activity Act, Context ctx, EditText edit) {this.act = Act;this.ctx = Ctx;this.ed = Edit;kb_letter = new Keyboard (This.ctx, r.xml.qwerty); kb_number = new Keyboard (This.ctx, r.xml.symbols); Keyboardview = (Keyboardview) t His.act.findViewById (R.id.keyboard_view); Keyboardview.setkeyboard (kb_number); Is_nun = true; Keyboardview.setenabled (True); keyboardview.setpreviewenabled (true); Keyboardview.setonkeyboardactionlistener ( Action_listener); Keyboardview.setontouchlistener (touch_listener);//Join the Touch Listener}

Then new a listener that we process these data, we need to "overload" (perhaps called "overwrite" more appropriate)OnTouch function, the collected data displayed to locat on.

Private Ontouchlistener Touch_listener = new Ontouchlistener () {@Overridepublic Boolean onTouch (View V, Android.view.MotionEvent event) {Float pressure, size, Rx, ry, X, Y, Interval;long time, down_time;int action = Event.geta    Ction ();        Switch (action) {case (Motionevent.action_down): pressure = event.getpressure ();        Size = Event.getsize ();        Time = Event.geteventtime ();        Down_time = Event.getdowntime ();        x = Event.getx ();        y = event.gety ();        Rx = Event.getrawx ();        ry = Event.getrawy ();        Interval = (float) 0.0;        if (is_first_press) {interval = Down_time-last_down_time;        } is_first_press = true;                Last_down_time = Down_time; LOG.I ("!!!!!!!!!", "Pressure:" +string.valueof (pressure) + "\ n" + "Size:" +string.valueof (size) + "\ n" + "Tim  E: "+string.valueof (time) +" \ n "+" Downtime: "+string.valueof (down_time) +" \ n "+" x: "+string.valueof (x) +" Y: "+string.valUeof (y) + "\ n" + "Rx:" +string.valueof (RX) + "ry:" +string.valueof (ry) + "\ n" + "interval:" +string.valueof (i        Nterval) + "\ n");        Break        Case (MOTIONEVENT.ACTION_UP): V.performclick ();        Break    Case (Motionevent.action_move): break; }return false;}};

And then it 's done ...

SOURCE Download:http://download.csdn.net/detail/arrow2013/8414521




Android custom keyboard (fix pop-up tips for font color issues)

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.