ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2022-01-10 13:02:24  阅读:191  来源: 互联网

标签:输入法 codes 自定义 void keyLabel Override Android public 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.CodeCance
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”);

标签:输入法,codes,自定义,void,keyLabel,Override,Android,public,android
来源: https://blog.csdn.net/m0_66144765/article/details/122408049

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有