Android 如何在自定义界面上启用输入法 (How to enable inputmethod for the custom UI)
来源:互联网 发布:swisse护肝片 知乎 编辑:程序博客网 时间:2024/04/30 16:57
http://www.oschina.net/question/54100_39046
在android中经常会自定义组件,自定义的组件可以通过继承系统的已经有的组件来实现。也可以直接继承自View或者是SurfaceView 界面。有时候想在这些界面中输入文字,例如游戏中经常用到的SurfaceView上让用户输入文字。由于多数android都没有实体的输入键盘,另外 android中都启用了输入法功能,如非英文用户都需要安装指定的输入法等。 因此在这些界面中输入文字首先需要调用输入法功能。
在Android中,输入法(IME)是通过InputMethodService来提供的。 你要做的是在你的view里面启动输入法。 在View里面启用输入法,需要实现的方法是
这个方法会返回一个InputConnection对象。 InputCo
1
public
InputConnection onCreateInputConnection(EditorInfo outAttrs) {}
nnection就是建立你的View与InputMethodService之间的桥梁。输入法就是通过IntputConnection将文字内容传输到View当中。
为了实现输入功能,我们需要创建自己的InputConnection类。 他的作用是接收输入法提交的内容,并可以对输入法提交的文字内容进行做进一步的处理。
01
class
MyInputConnection
extends
BaseInputConnection{
02
03
String inputString=
""
;
04
05
public
MyInputConnection(View targetView,
boolean
fullEditor) {
06
super
(targetView, fullEditor);
07
// TODO Auto-generated constructor stub
08
}
09
public
boolean
commitText(CharSequence text,
int
newCursorPosition){
10
inputString=inputString+(String) text;
11
return
true
;
12
}
13
14
}
MyInputConnection继承自BaseInputConnection,BaseInputConnection继承自 InputConnection。 这里面必须要实现的一个方法就是public boolean commitText(CharSequence text, int newCursorPosition), 第一个参数text就是输入法在完成一次输入时提交的文字内容。我们现在直接保存到inputString中, 这样输入法输入的内容会源源不断的添加到nputString中。我们只需要读取inputString就可以得到输入法输入的文字。
接下来要帮的是在需要输入的时候显示出输入法。 当view里需要用户输入的时候,
1
InputMethodManager input=(InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
2
input.showSoftInput(
this
,
0
);
即可以调用出输入法。
以下代码是我在SurfaceView里做的一个演示。
01
import
android.content.Context;
02
import
android.graphics.Canvas;
03
import
android.graphics.Color;
04
import
android.graphics.Paint;
05
import
android.view.SurfaceHolder;
06
import
android.view.SurfaceHolder.Callback;
07
import
android.view.SurfaceView;
08
import
android.view.View;
09
import
android.view.inputmethod.BaseInputConnection;
10
import
android.view.inputmethod.CompletionInfo;
11
import
android.view.inputmethod.EditorInfo;
12
import
android.view.inputmethod.InputConnection;
13
import
android.view.inputmethod.InputMethodManager;
14
15
public
class
SufaceInput
extends
SurfaceView
implements
SurfaceHolder.Callback , Runnable {
16
SurfaceHolder holder=
null
;
17
String inputString=
"xyz"
;
18
InputMethodManager input=
null
;
19
public
SufaceInput(Context context) {
20
super
(context);
21
holder=
this
.getHolder();
22
holder.addCallback(
this
);
23
this
.setFocusable(
true
);
24
this
.setFocusableInTouchMode(
true
);
25
26
input=(InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
27
28
// TODO Auto-generated constructor stub
29
}
30
31
public
void
surfaceChanged(SurfaceHolder holder,
int
format,
int
width,
32
int
height) {
33
// TODO Auto-generated method stub
34
new
Thread(
this
).start();
35
input.showSoftInput(
this
,
0
);
36
}
37
38
public
void
surfaceCreated(SurfaceHolder holder) {
39
// TODO Auto-generated method stub
40
41
}
42
43
public
void
surfaceDestroyed(SurfaceHolder holder) {
44
// TODO Auto-generated method stub
45
46
}
47
48
class
MyInputConnection
extends
BaseInputConnection{
49
50
public
MyInputConnection(View targetView,
boolean
fullEditor) {
51
super
(targetView, fullEditor);
52
// TODO Auto-generated constructor stub
53
}
54
public
boolean
commitText(CharSequence text,
int
newCursorPosition){
55
inputString=inputString+(String) text;
56
return
true
;
57
}
58
59
}
60
61
@Override
62
public
InputConnection onCreateInputConnection(EditorInfo outAttrs) {
63
// TODO Auto-generated method stub
64
return
new
MyInputConnection(
this
,
false
);
//super.onCreateInputConnection(outAttrs);
65
}
66
67
public
void
run() {
68
// TODO Auto-generated method stub
69
while
(
true
){
70
Canvas c=holder.lockCanvas();
71
Paint p=
new
Paint();
72
p.setColor(Color.RED);
73
c.drawColor(Color.WHITE);
74
c.drawText(inputString,
100
,
100
, p);
75
holder.unlockCanvasAndPost(c);
76
77
}
78
}
79
}
出处:batfree
- Android 如何在自定义界面上启用输入法 (How to enable inputmethod for the custom UI)
- Android 如何在自定义界面上启用输入法 (How to enable inputmethod for the custom UI)
- Android 如何在自定义界面上启用输入法 (How to enable inputmethod for the custom UI)
- Android 如何在自定义界面上启用输入法 (How to enable inputmethod for the custom UI)
- Android 如何在自定义界面上启用输入法 (How to enable inputmethod for the custom UI)
- how to debug inputmethod
- 【Android】【InputMethod】隐藏输入法
- How To Create Custom Skins For DotText(如何为DotText创建自定义风格的皮肤)
- How to enable the debugfs
- How to enable the MPlayer on centOS7
- How to Enable Trace or Debug for APIs executed as SQL Script Outside of the Applications ?
- How to change the language to English for Oracle SQL Developer(如何将SQL developer改成英文界面)
- How to change the language to English for Oracle SQL Developer(如何将SQL developer改成英文界面)
- How to create a custom directory for app files inside the ~/Library/Application Support directory.
- Android InputMethod 源码分析,显示输入法流程
- Android 4.0 How to enable Bluetooth PANU
- How to enable Android ConfigFS gadgets
- How To Enable Android's Developer Options
- android定义输入法-自定义键盘(转)
- archbang 硬盘安装
- Test-Driven Development?别逗了
- 用例图(复习)
- bcm sdk 代码分析
- Android 如何在自定义界面上启用输入法 (How to enable inputmethod for the custom UI)
- ATMEL mXT224电容触摸屏调试笔记
- c/c++语言学习建议
- 通达OA 开发一个按部门查询电话号码的插件
- 数据库在有外键的时候删除
- 类似“99朵玫瑰”的程序开发
- c++ string的简单实现
- bfs+bfs/dfs hdu 1254 推箱子1
- java获取的本机时间比当前时间少8小时的解决方案