哦然间发现了android.inputmethodservice.Keyboard类,即android可以自定义键盘类,做了一个简单例子供大家参考,

首先看看效果图:

android 自定义软键盘_第1张图片

android 自定义软键盘_第2张图片


键盘内容布局: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);        }    }}

以上就是自定义键盘布局的内容,如需详细内容需要参考api。



更多相关文章

  1. Android AlertDialog包含EditText,软键盘不能弹出的解决方法
  2. Android PopUpWindow 软键盘
  3. Android获取指定URL的内容
  4. android之自定义ViewGroup和自动换行的布局的实现
  5. 自定义Android标题栏TitleBar布局
  6. [Android]取得Dialog中EditText的内容问题
  7. Android入门:HTML布局中Android程序与JAVASCRIPT的交互

随机推荐

  1. android手机图片查看
  2. Android(安卓)- Looper.prepare()和Loope
  3. runONUIThread 分析与使用
  4. Android NDK之JNI使用例子
  5. Android之android:theme设置在Applicatio
  6. 深入了解android平台的jni(二)
  7. Android弹出软键盘布局是否上移问题
  8. Android AlarmManager的取消
  9. Mediaplayer中通过create函数获取资源时P
  10. android sharedpreference保存boolean,int