博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android 如何在自定义界面上启用输入法 (How to enable inputmethod for the custom UI)
阅读量:4051 次
发布时间:2019-05-25

本文共 3790 字,大约阅读时间需要 12 分钟。

在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类。 他的作用是接收输入法提交的内容,并可以对输入法提交的文字内容进行做进一步的处理。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class
MyInputConnection
extends
BaseInputConnection{
 
        
String inputString=
""
;
 
        
public
MyInputConnection(View targetView,
boolean
fullEditor) {
            
super
(targetView, fullEditor);
            
// TODO Auto-generated constructor stub
        
}
        
public
boolean
commitText(CharSequence text,
int
newCursorPosition){
            
inputString=inputString+(String) text;
            
return
true
;
        
}
         
    
}

MyInputConnection继承自BaseInputConnection,BaseInputConnection继承自 InputConnection。 这里面必须要实现的一个方法就是public boolean commitText(CharSequence text, int newCursorPosition), 第一个参数text就是输入法在完成一次输入时提交的文字内容。我们现在直接保存到inputString中, 这样输入法输入的内容会源源不断的添加到nputString中。我们只需要读取inputString就可以得到输入法输入的文字。

接下来要帮的是在需要输入的时候显示出输入法。 当view里需要用户输入的时候,

1
2
InputMethodManager  input=(InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
input.showSoftInput(
this
,
0
);

即可以调用出输入法。

以下代码是我在SurfaceView里做的一个演示。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import
android.content.Context;
import
android.graphics.Canvas;
import
android.graphics.Color;
import
android.graphics.Paint;
import
android.view.SurfaceHolder;
import
android.view.SurfaceHolder.Callback;
import
android.view.SurfaceView;
import
android.view.View;
import
android.view.inputmethod.BaseInputConnection;
import
android.view.inputmethod.CompletionInfo;
import
android.view.inputmethod.EditorInfo;
import
android.view.inputmethod.InputConnection;
import
android.view.inputmethod.InputMethodManager;
   
public
class
SufaceInput
extends
SurfaceView
implements
SurfaceHolder.Callback , Runnable {
    
SurfaceHolder holder=
null
;
    
String inputString=
"xyz"
;
    
InputMethodManager input=
null
;
    
public
SufaceInput(Context context) {
        
super
(context);
        
holder=
this
.getHolder();
        
holder.addCallback(
this
);
        
this
.setFocusable(
true
);
        
this
.setFocusableInTouchMode(
true
);
         
        
input=(InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
         
        
// TODO Auto-generated constructor stub
    
}
 
    
public
void
surfaceChanged(SurfaceHolder holder,
int
format,
int
width,
            
int
height) {
        
// TODO Auto-generated method stub
        
new
Thread(
this
).start();
        
input.showSoftInput(
this
,
0
);
    
}
 
    
public
void
surfaceCreated(SurfaceHolder holder) {
        
// TODO Auto-generated method stub
         
    
}
 
    
public
void
surfaceDestroyed(SurfaceHolder holder) {
        
// TODO Auto-generated method stub
         
    
}
 
    
class
MyInputConnection
extends
BaseInputConnection{
 
        
public
MyInputConnection(View targetView,
boolean
fullEditor) {
            
super
(targetView, fullEditor);
            
// TODO Auto-generated constructor stub
        
}
        
public
boolean
commitText(CharSequence text,
int
newCursorPosition){
            
inputString=inputString+(String) text;
            
return
true
;
        
}
         
    
}
 
    
@Override
    
public
InputConnection onCreateInputConnection(EditorInfo outAttrs) {
        
// TODO Auto-generated method stub
        
return
new
MyInputConnection(
this
,
false
);
//super.onCreateInputConnection(outAttrs);
    
}
 
    
public
void
run() {
        
// TODO Auto-generated method stub
        
while
(
true
){
            
Canvas c=holder.lockCanvas();
            
Paint p=
new
Paint();
            
p.setColor(Color.RED);
            
c.drawColor(Color.WHITE);
            
c.drawText(inputString,
100
,
100
, p);
            
holder.unlockCanvasAndPost(c);
             
        
}
    
}
}

转载地址:http://kbici.baihongyu.com/

你可能感兴趣的文章
cppcheck c++静态代码检查
查看>>
在C++中使用Lua
查看>>
一些socket的编程经验
查看>>
socket编程中select的使用
查看>>
可以在线C++编译的工具站点
查看>>
关于无人驾驶的过去、现在以及未来,看这篇文章就够了!
查看>>
所谓的进步和提升,就是完成认知升级
查看>>
为什么读了很多书,却学不到什么东西?
查看>>
长文干货:如何轻松应对工作中最棘手的13种场景?
查看>>
如何用好碎片化时间,让思维更有效率?
查看>>
No.174 - LeetCode1305 - 合并两个搜索树
查看>>
No.175 - LeetCode1306
查看>>
No.176 - LeetCode1309
查看>>
No.182 - LeetCode1325 - C指针的魅力
查看>>
mysql:sql alter database修改数据库字符集
查看>>
mysql:sql truncate (清除表数据)
查看>>
yuv to rgb 转换失败呀。天呀。谁来帮帮我呀。
查看>>
yuv420 format
查看>>
yuv420 还原为RGB图像
查看>>
LED恒流驱动芯片
查看>>