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

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'm adding a global layout tag to the activity's map file

?
12 android:focusable=trueandroid: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

?
123456789101112 et.setOnTouchListener(new View.OnTouchListener(){            @Override            public 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 said is because of the theme problem, because I use the android5.0.1 API, automatically generate the project, the activity is directly inherited actionbaractivity, direct change theme will lead to the occurrence of a second fallback phenomenon. If you want to change theme will first change to inherit actionbaractivity for activity, and then to change theme, but I do not like this, because there is no commonality, and later in Google a bit of foreign posts, Just learned that this pop-up thing called preview, we can modify its layout.

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

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

?
12 <!--?xml version=1.0 encoding=utf-8?--><textview android:background="#ff8888ff/" android:gravity="center" android:layout_height="wrap_content" android:layout_width="wrap_content" android:textcolor="@android:color/white" android:textsize="40sp" xmlns:android="http://schemas.android.com/apk/res/android"></textview>

Where Android:background is the background color of our cue box, Android:textcolor is the font color, change to you like the OK.

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.

?
1234567891011121314 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) this.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);//加入触摸监听器}

Then new a listener that we process these data, we need to "overload" (perhaps called "overwrite" more appropriate) Ontouch function, the collected data to display on the Locat. It should be noted that if the return value of this Ontouch function is true, it means that the action has been processed, and false means that it is not handled, because there are other functions that need to get this action, so this returns false, otherwise, The next response to the key action (font output, etc.) will not occur.

?
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 private OnTouchListener touch_listener = new OnTouchListener(){        @Override        public boolean onTouch(View v, android.view.MotionEvent event) {            float pressure, size, rx, ry, x, y, interval;            long time, down_time;            int action = event.getAction();            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)+                            +size:+String.valueOf(size)+                            +time:+String.valueOf(time)+                             +downtime:+String.valueOf(down_time)+                            +x:+String.valueOf(x)+  y:+String.valueOf(y)+                            +rx:+String.valueOf(rx)+  ry:+String.valueOf(ry)+                             +interval:+String.valueOf(interval)+);                    break;                case (MotionEvent.ACTION_UP):                                        v.performClick();                break;                case (MotionEvent.ACTION_MOVE):                break;            }            return false;        }    };

A companion tour, a free dating site: www.jieberu.com

Push family, free tickets, scenic spots: www.tuituizu.com

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

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.