Android custom keyboard (solves the font color problem of the pop-up prompt)
Recently, we are preparing to create a project. We need to use a custom keypad to ensure security. We also need to precisely obtain parameters such as the position, intensity, and area of the fingertip contact screen when you click the keyboard.
Some code on the Chinese Internet is used when writing a custom keyboard. The source is
Salute to the ancestors!
Then I found that the down code used in my project encountered various problems:
1. first, the system's input method Keyboard will pop up as soon as the application is opened, instead of the custom keyboard. This problem is because EditText will get the focus when the application is opened, the system input method instead of the custom input method is displayed. The solution is to make EditText not get the focus when the application is opened, so I add it to the global Layout label of the Layout file corresponding to the activity.
android:focusable=true android:focusableInTouchMode=true
When the activity is running, the EditText loses its focus and the system input method does not pop up. Then add the touch listener to EditText. When you click the EditText control, the system responds to the pop-up of 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 you click the button, there is no font in the prompt box displayed on the keyboard. For example
This is because the font displayed is white and the background is white. Later, some people said that it was because of theme problems because I used the android5.0.1 API. when a project is automatically generated, the generated activity directly inherits ActionBarActivity, directly changing theme will result in a second retreat. If you want to change theme, You need to first inherit ActionBarActivity as Activity and then change theme. But I do not like this because it is not universal, and then google the foreign post, this pop-up is called preview. We can modify its layout.
Add the android: keyPreviewLayout tag to the custom KeyboardView, as follows:
Create a new key_preview_layout.xml file in the layout folder.
Android: background is the background color of the prompt box, android: textColor is the font color, and it is OK if you change it to your favorite.
3. Add the touch response of the keyboard and record the click information (strength, contact area, and placement coordinates ).
We can add a touch listener to the keyboardView In the constructor of the custom keyboard. For example, we can write the constructor in this way.
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); // Add the touch listener}
Then a new listener for processing the data requires the OnTouch function to display the collected data to Locat. It should be noted that if the return value of this OnTouch function is true, it indicates that the action has been processed. If it is false, it indicates that the action has not been processed. Because other functions need to obtain this action, therefore, false is returned here. Otherwise, the subsequent response to the button action (such as the font output) will not occur.
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.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;}};