Android custom keypad

Source: Internet
Author: User

Oh, we found the Android. inputmethodservice. keyboard class, that is, the android can customize the keyboard class. A simple example is provided for your reference,

First take a look:

Keyboard content layout: keycontent. xml

<?xml version="1.0" encoding="utf-8"?><Keyboard xmlns:android="http://schemas.android.com/apk/res/android"    android:keyWidth="25%p"    android:horizontalGap="0px"    android:verticalGap="0px"    android:keyHeight="50dip">    <Row>        <Key android:codes="49" android:keyLabel="1" />        <Key android:codes="50" android:keyLabel="2" />        <Key android:codes="51" android:keyLabel="3" />        <Key android:codes="57419"            android:keyEdgeFlags="right"            android:keyIcon="@drawable/keyboard_capslock" />    </Row>    <Row>        <Key android:codes="52" android:keyLabel="4" />        <Key android:codes="53" android:keyLabel="5" />        <Key android:codes="54" android:keyLabel="6" />        <Key android:codes="57421"            android:keyEdgeFlags="right"            android:keyIcon="@drawable/keyboard_big_capslock" />    </Row>    <Row>        <Key android:codes="55" android:keyLabel="7" />        <Key android:codes="56" android:keyLabel="8" />        <Key android:codes="57" android:keyLabel="9" />        <Key android:codes="-5"            android:keyEdgeFlags="right"            android:isRepeatable="true"            android:keyLabel="delete"            android:keyIcon="@drawable/keyboard_delete" />    </Row>    <Row>        <Key android:codes="-3" android:keyIcon="@drawable/keyboard_return" />        <Key android:codes="48" android:keyLabel="0" />        <Key android:codes="88" android:keyLabel="X" />    </Row></Keyboard>

Keyboardview. xml

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="wrap_content">    <EditText         android:id="@+id/edit"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        />    <android.inputmethodservice.KeyboardView        android:id="@+id/keyboard_view"        android:visibility="gone"        android:focusable="true"        android:focusableInTouchMode="true"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true" /></RelativeLayout>

Keyboardactivity;

package com.pioneersoft.temp;import android.app.Activity;import android.inputmethodservice.Keyboard;import android.inputmethodservice.KeyboardView;import android.inputmethodservice.KeyboardView.OnKeyboardActionListener;import android.os.Bundle;import android.text.Editable;import android.text.InputType;import android.view.View;import android.view.View.OnClickListener;import android.widget.EditText;public class KeyboardActivity extends Activity{EditText edit;KeyboardView keyboardView;@Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.keyboardview);         edit = (EditText)findViewById(R.id.edit);        edit.setInputType(InputType.TYPE_NULL);        edit.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                showKeyboard();            }        });         keyboardView = (KeyboardView)findViewById(R.id.keyboard_view);        keyboardView.setKeyboard(new Keyboard(this, R.layout.keycontent));        keyboardView.setEnabled(true);        keyboardView.setPreviewEnabled(true);        keyboardView.setOnKeyboardActionListener(new OnKeyboardActionListener() {            @Override            public void onKey(int primaryCode, int[] keyCodes) {                Editable editable = edit.getText();                int start = edit.getSelectionStart();                if (primaryCode == Keyboard.KEYCODE_CANCEL) {                    hideKeyboard();                } else if (primaryCode == Keyboard.KEYCODE_DELETE) {                    if (editable != null && editable.length() > 0) {                        editable.delete(start - 1, start);                    }                } else if (primaryCode == 57419) { // go left                    if (start > 0) {                        edit.setSelection(start - 1);                    }                } else if (primaryCode == 57421) { // go right                    if (start < edit.length()) {                        edit.setSelection(start + 1);                    }                } else {                    editable.insert(start, Character.toString((char)primaryCode));                }            }@Overridepublic void onPress(int primaryCode) {// TODO Auto-generated method stub}@Overridepublic void onRelease(int primaryCode) {// TODO Auto-generated method stub}@Overridepublic void onText(CharSequence text) {// TODO Auto-generated method stub}@Overridepublic void swipeDown() {// TODO Auto-generated method stub}@Overridepublic void swipeLeft() {// TODO Auto-generated method stub}@Overridepublic void swipeRight() {// TODO Auto-generated method stub}@Overridepublic void swipeUp() {// TODO Auto-generated method stub}        });    }     private void showKeyboard() {        int visibility = keyboardView.getVisibility();        if (visibility == View.GONE || visibility == View.INVISIBLE) {            keyboardView.setVisibility(View.VISIBLE);        }    }     private void hideKeyboard() {        int visibility = keyboardView.getVisibility();        if (visibility == View.VISIBLE) {            keyboardView.setVisibility(View.INVISIBLE);        }    }}

The above is the content of the custom keyboard layout. For details, refer to the API.

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.