Android 应用内自定义随机布局输入法,刚从阿里、头条面试回来

WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

KeyboardView keyboardView = (KeyboardView) findViewById(R.id.keyboardview);

builder = new KeyboardBuilder(this, keyboardView, R.xml.keys_layout);

EditText editCustomIME = (EditText) findViewById(R.id.editCustomIME);

builder.registerEditText(editCustomIME);

}

@Override

public void onBackPressed() {

if (builder != null && builder.isCustomKeyboardVisible()) {

builder.hideCustomKeyboard();

} else {

this.finish();

}

}

}

键盘布局:

<?xml version="1.0" encoding="utf-8"?>

<Keyboard xmlns:android=“http://schemas.android.com/apk/res/android”

android:keyHeight=“10%p”

android:keyWidth=“25%p” >

<Key

android:codes=“55”

android:keyEdgeFlags=“left”

android:keyLabel=“7” />

<Key

android:codes=“56”

android:keyLabel=“8” />

<Key

android:codes=“57”

android:keyLabel=“9” />

<Key

android:codes=“60001”

android:isRepeatable=“true”

android:keyLabel=“DEL” />

<Key

android:codes=“52”

android:keyEdgeFlags=“left”

android:keyLabel=“4” />

<Key

android:codes=“53”

android:keyLabel=“5” />

<Key

android:codes=“54”

android:keyLabel=“6” />

<Key

android:codes=“48”

android:keyLabel=“0” />

<Key

android:codes=“49”

android:keyEdgeFlags=“left”

android:keyLabel=“1” />

<Key

android:codes=“50”

android:keyLabel=“2” />

<Key

android:codes=“51”

android:keyLabel=“3” />

<Key

android:codes=“60002”

android:keyLabel=“Cancel” />

常量

和输入法布局codes对应

package com.demo.customime;

public interface Constant {

int CodeDelete = 60001;

int CodeCancel = 60002;

}

KeyboardBuilder

package com.demo.customime;

import android.app.Activity;

import android.inputmethodservice.Keyboard;

import android.inputmethodservice.KeyboardView;

import android.text.Editable;

import android.text.InputType;

import android.util.Log;

import android.view.MotionEvent;

import android.view.View;

import android.view.inputmethod.InputMethodManager;

import android.widget.EditText;

public class KeyboardBuilder {

private static final String TAG = “KeyboardBuilder”;

private Activity mActivity;

private KeyboardView mKeyboardView;

public KeyboardBuilder(Activity ac, KeyboardView keyboardView,

int keyBoardXmlResId) {

mActivity = ac;

mKeyboardView = keyboardView;

Keyboard mKeyboard = new Keyboard(mActivity, keyBoardXmlResId);

// Attach the keyboard to the view

mKeyboardView.setKeyboard(mKeyboard);

// Do not show the preview balloons

mKeyboardView.setPreviewEnabled(false);

KeyboardView.OnKeyboardActionListener keyboardListener = new KeyboardView.OnKeyboardActionListener() {

@Override

public void onKey(int primaryCode, int[] keyCodes) {

// Get the EditText and its Editable

View focusCurrent = mActivity.getWindow().getCurrentFocus();

if (focusCurrent == null || !(focusCurrent instanceof EditText)) {

return;

}

EditText edittext = (EditText) focusCurrent;

Editable editable = edittext.getText();

int start = edittext.getSelectionStart();

// Handle key

if (primaryCode == Constant.CodeCanceAndroid 应用内自定义随机布局输入法,刚从阿里、头条面试回来
l) {

hideCustomKeyboard();

} else if (primaryCode == Constant.CodeDelete) {

if (editable != null && start > 0) {

editable.delete(start - 1, start);

}

} else {

// Insert character

editable.insert(start,

Character.toString((char) primaryCode));

}

}

@Override

public void onPress(int arg0) {

}

@Override

public void onRelease(int primaryCode) {

}

@Override

public void onText(CharSequence text) {

}

@Override

public void swipeDown() {

}

@Override

public void swipeLeft() {

}

@Override

public void swipeRight() {

}

@Override

public void swipeUp() {

}

};

mKeyboardView.setOnKeyboardActionListener(keyboardListener);

}

// 绑定一个EditText

public void registerEditText(EditText editText) {

// Make the custom keyboard appear

editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {

@Override

public void onFocusChange(View v, boolean hasFocus) {

if (hasFocus) {

showCustomKeyboard(v);

} else {

hideCustomKeyboard();

}

}

});

editText.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

Log.d(TAG, “onClick”);

// Make the custom keyboard appear

editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {

@Override

public void onFocusChange(View v, boolean hasFocus) {

if (hasFocus) {

showCustomKeyboard(v);

} else {

hideCustomKeyboard();

}

}

});

editText.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

Log.d(TAG, “onClick”);

上一篇:迭代器-适配器方法惯用法


下一篇:node.js 实现 token 身份验证