在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{ |
03 |
String inputString= "" ; |
05 |
public MyInputConnection(View targetView, boolean fullEditor) { |
06 |
super (targetView, fullEditor); |
09 |
public boolean commitText(CharSequence text, int newCursorPosition){ |
10 |
inputString=inputString+(String) text; |
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里做的一个演示。
view source print ?
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; |
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) { |
21 |
holder= this .getHolder(); |
22 |
holder.addCallback( this ); |
23 |
this .setFocusable( true ); |
24 |
this .setFocusableInTouchMode( true ); |
26 |
input=(InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); |
31 |
public void surfaceChanged(SurfaceHolder holder, int format, int width, |
34 |
new Thread( this ).start(); |
35 |
input.showSoftInput( this , 0 ); |
38 |
public void surfaceCreated(SurfaceHolder holder) { |
43 |
public void surfaceDestroyed(SurfaceHolder holder) { |
48 |
class MyInputConnection extends BaseInputConnection{ |
50 |
public MyInputConnection(View targetView, boolean fullEditor) { |
51 |
super (targetView, fullEditor); |
54 |
public boolean commitText(CharSequence text, int newCursorPosition){ |
55 |
inputString=inputString+(String) text; |
62 |
public InputConnection onCreateInputConnection(EditorInfo outAttrs) { |
64 |
return new MyInputConnection( this , false ); |
70 |
Canvas c=holder.lockCanvas(); |
72 |
p.setColor(Color.RED); |
73 |
c.drawColor(Color.WHITE); |
74 |
c.drawText(inputString, 100 , 100 , p); |
75 |
holder.unlockCanvasAndPost(c); |
- 微软一年通过Android获得几十亿美元收入,没错,是微软!
- Android输入法框架系统(上)
- Android: 打印Bundle内容
- Android 组件 ContentProvider 内容提供者
- Android输入法之输入系统
- 待验证内容。
- Android TextView内容居中和控件居中
随机推荐
-
Gson解析json,让json解析不再困难
-
Android(安卓)代码中设置EditText的hint
-
TextView文字横向自动滚动(跑马灯)
-
Android(安卓)framework/base 下添加新的
-
Android中Context的理解与使用技巧
-
一个常见的android内存泄露 问题
-
android点击EditText外区域收起键盘
-
Error:more than one library with packa
-
Android——SeekBar(进度、音量大小的控件
-
Android中保存图片到本地并更新到相册及