//首先我们得重写View中的一个方法,返回true,就是让这个View变成文本可编辑的状态,默认返回false。
@Override
public boolean onCheckIsTextEditor() {
return true;
}
//第二个就是重写
public InputConnection onCreateInputConnection(EditorInfo outAttrs);
//方法,需要返回一个InputConnect对象,这个是和输入法输入内容的桥梁。
// outAttrs就是我们需要设置的输入法的各种类型最重要的就是:
outAttrs.imeOptions = EditorInfo.IME_FLAG_NO_EXTRACT_UI;
outAttrs.inputType = InputType.TYPE_NULL;
//一般我们都是些一个BaseInputConnection的子类,而BaseInputConnection是实现了InputConnection接口的。
需要注意的就是几个方法注意重写。
@Override
public boolean commitText(CharSequence text, int newCursorPosition) {
Log.d("hickey", "commitText:" + text + "\t" + newCursorPosition);
if (containsEmoji(text.toString())) {
Log.d("hickey", "send emoji");
return true;
}
if (mPlayer != null && mPlayFragment.isInputMethodStatus()) {
Log.d("hickey", "text:" + text);
mPlayerView.sendCharEvent(text.toString());
}
return true;
}
note:这个是当输入法输入了字符,包括表情,字母、文字、数字和符号。我们可以通过text筛选出我们不想让显示到自定义view上面。
//有文本输入,当然也有按键输入,也别注意的是有些输入法输入数字并非用commitText方法传递,而是用按键来代替,比如KeyCode_1是代表1等。
@Override
public boolean sendKeyEvent(KeyEvent event) {
/** 当手指离开的按键的时候 */
if (event.getAction() == KeyEvent.ACTION_DOWN) {
Log.d("hickey", "sendKeyEvent:KeyCode=" + event.getKeyCode());
if (event.getKeyCode() == KeyEvent.KEYCODE_DEL) {
mPlayerView.sendFunctionKeyCodeEvent(KeyEvent.KEYCODE_DEL);
} else if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
mPlayerView.sendFunctionKeyCodeEvent(KeyEvent.KEYCODE_ENTER);
mPlayFragment.setInputMethodStatus(false, 1);
} else {
mPlayerView.sendCharKeyCodeEvent(event.getKeyCode());
}
}
return true;
}
note:这里我只做了删除,回车按键的处理,由于会触发动作按下和松开两次,所以在这里只做了按下的处理。
//当然删除的时候也会触发
@Override
public boolean deleteSurroundingText(int beforeLength, int afterLength) {
Log.d("hickey", "deleteSurroundingText " + "beforeLength=" + beforeLength + " afterLength=" + afterLength);
mPlayerView.sendFunctionKeyCodeEvent(KeyEvent.KEYCODE_DEL);
return true;
}
@Override
public boolean finishComposingText() {
//结束组合文本输入的时候
Log.d("hickey", "finishComposingText");
return true;
}
//这个方法基本上会出现在切换输入法类型,点击回车(完成、搜索、发送、下一步)点击输入法右上角隐藏按钮会触发。
InputMethodHelper(Context mContext) {
inputMethodManager = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
}
public synchronized static InputMethodHelper getInstance(Context mContext) {
synchronized (InputMethodHelper.class) {
if (inputMethodHelper == null) {
inputMethodHelper = new InputMethodHelper(mContext);
}
return inputMethodHelper;
}
}
/**
* 显示软键盘
*
* @param view
*/
public void showSoftInput(View view) {
inputMethodManager.showSoftInput(view, 0);
}
/**
* 隐藏输入法
*/
public void hideSoftInput(View view) {
if (inputMethodManager.isActive()) {
Log.d("hickey", "hideSoftInput:" + "hideSoftInputFromWindow");
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
public boolean isActive(View view){
return inputMethodManager.isActive(view);
}
public boolean isActive(){
return inputMethodManager.isActive();
}
public boolean isWatchingCursor (View view){
return inputMethodManager.isWatchingCursor(view);
}
public boolean isAcceptingText(){
return inputMethodManager.isAcceptingText();
}
new Thread(new Runnable() {
@Override
public void run() {
RedFinger.simulationEvent = true;
Instrumentation instrumentation = new Instrumentation();
instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_BACK);
}
}).start();
//这里弄了个bool标志是防止输入已经隐藏还会分发返回按键事件到Activity上,所以需要在可能退出到的页面上做处理。
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有