Android 如何在自定义界面上启用输入法 (How to enable inputmethod for the custom UI)

在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);
             
         }
     }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值