在android中经常会自定义组件,自定义的组件可以通过继承系统的已经有的组件来实现。也可以直接继承自View或者是SurfaceView 界面。有时候想在这些界面中输入文字,例如游戏中经常用到的SurfaceView上让用户输入文字。由于多数android都没有实体的输入键盘,另外 android中都启用了输入法功能,如非英文用户都需要安装指定的输入法等。 因此在这些界面中输入文字首先需要调用输入法功能。
在Android中,输入法(IME)是通过InputMethodService来提供的。 你要做的是在你的view里面启动输入法。 在View里面启用输入法,需要实现的方法是
这个方法会返回一个InputConnection对象。 InputCo
| public InputConnection onCreateInputConnection(EditorInfo outAttrs) {} |
nnection就是建立你的View与InputMethodService之间的桥梁。输入法就是通过IntputConnection将文字内容传输到View当中。
为了实现输入功能,我们需要创建自己的InputConnection类。 他的作用是接收输入法提交的内容,并可以对输入法提交的文字内容进行做进一步的处理。
| class MyInputConnection extends BaseInputConnection{ public MyInputConnection(View targetView, boolean fullEditor) { super (targetView, fullEditor); public boolean commitText(CharSequence text, int newCursorPosition){ inputString=inputString+(String) text; |
MyInputConnection继承自BaseInputConnection,BaseInputConnection继承自 InputConnection。 这里面必须要实现的一个方法就是public boolean commitText(CharSequence text, int newCursorPosition), 第一个参数text就是输入法在完成一次输入时提交的文字内容。我们现在直接保存到inputString中, 这样输入法输入的内容会源源不断的添加到nputString中。我们只需要读取inputString就可以得到输入法输入的文字。
接下来要帮的是在需要输入的时候显示出输入法。 当view里需要用户输入的时候,
| InputMethodManager input=(InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); input.showSoftInput( this , 0 ); |
即可以调用出输入法。
以下代码是我在SurfaceView里做的一个演示。
| 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) { holder= this .getHolder(); holder.addCallback( this ); this .setFocusable( true ); this .setFocusableInTouchMode( true ); input=(InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); public void surfaceChanged(SurfaceHolder holder, int format, int width, new Thread( this ).start(); input.showSoftInput( this , 0 ); public void surfaceCreated(SurfaceHolder holder) { public void surfaceDestroyed(SurfaceHolder holder) { class MyInputConnection extends BaseInputConnection{ public MyInputConnection(View targetView, boolean fullEditor) { super (targetView, fullEditor); public boolean commitText(CharSequence text, int newCursorPosition){ inputString=inputString+(String) text; public InputConnection onCreateInputConnection(EditorInfo outAttrs) { return new MyInputConnection( this , false ); Canvas c=holder.lockCanvas(); c.drawColor(Color.WHITE); c.drawText(inputString, 100 , 100 , p); holder.unlockCanvasAndPost(c); |