android 制作输入法

效果如图:
[img]http://dl.iteye.com/upload/attachment/0080/6760/a2c2ea0c-51a3-3c7e-8605-8257efe965a1.jpg[/img]
[img]http://dl.iteye.com/upload/attachment/0080/6766/ac70c101-3144-346d-b59e-38f84d3834d0.jpg[/img]

在res目录下新建文件夹xml,在xml文件夹下新建一个文件input_method.xml

<?xml version="1.0" encoding="utf-8"?>
<input-method xmlns:android="http://schemas.android.com/apk/res/android" />



然后有三个类分别是:


ImfService.java


CandidateView.java


KeyboardView.java



代码如下:


ImfService.java


import android.inputmethodservice.InputMethodService;
import android.util.Log;
import android.view.View;

/**
 * 
 * <br/>
 * Title: ImfService.java<br/>

 * Http: iaiai.iteye.com<br/>
 * Create time: 2013-2-20 下午5:07:34<br/>
 * <br/>
 * 
 * @author 丸子
 * @version 0.0.1
 */
public class ImfService extends InputMethodService {

	public void onInitializeInterface() {
		Log.i("*********", "自定义输入法onInitializeInterface");
	}

	@Override
	public View onCreateInputView() {
		Log.i("*********", "自定义输入法onCreateInputView");
		KeyboardView mkeyView = new KeyboardView(this);
		return mkeyView;
	}

	@Override
	public View onCreateExtractTextView() {
		Log.i("*********", "自定义输入法onCreateExtractTextView");
		return super.onCreateExtractTextView();
	}

	@Override
	public View onCreateCandidatesView() {
		Log.i("*********", "自定义输入法onCreateCandidatesView");
		CandidateView mCandView = new CandidateView(this);
		return mCandView;
	}

}



CandidateView.java


import android.content.Context;
import android.graphics.Color;
import android.inputmethodservice.InputMethodService;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * 
 * <br/>
 * Title: CandidateView.java<br/>

 * Http: iaiai.iteye.com<br/>
 * Create time: 2013-2-21 上午10:27:28<br/>
 * <br/>
 * 
 * @author 丸子
 * @version 0.0.1
 */
public class CandidateView extends LinearLayout {

	private InputMethodService service;

	public CandidateView(Context context) {
		super(context);
		service = (InputMethodService) context;

		setBackgroundColor(Color.GRAY);
		setOrientation(LinearLayout.HORIZONTAL);

		Button btnLeft = new Button(context);
		btnLeft.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		btnLeft.setText("<");
		addView(btnLeft);

		LinearLayout layout = new LinearLayout(context);
		layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 1));

		TextView tv = new TextView(context);
		tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT));
		tv.setTextColor(Color.BLACK);
		tv.setGravity(Gravity.CENTER | Gravity.CENTER_VERTICAL);
		tv.setText("测试内容啦...");
		tv.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				pickSuggestionManually(1);
			}
		});
		layout.addView(tv);

		addView(layout);

		Button btnRight = new Button(context);
		btnRight.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		btnRight.setText(">");
		addView(btnRight);
	}

	public void pickSuggestionManually(int mSelectedIndex) {
		service.getCurrentInputConnection().commitText("A", 0); // 往输入框输出内容
		service.setCandidatesViewShown(false); // 隐藏 CandidatesView
	}

}



KeyboardView.java


import android.app.Service;
import android.content.Context;
import android.inputmethodservice.InputMethodService;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

/**
 * 
 * <br/>
 * Title: KeyboardView.java<br/>

 * Http: iaiai.iteye.com<br/>
 * Create time: 2013-2-21 上午10:29:12<br/>
 * <br/>
 * 
 * @author 丸子
 * @version 0.0.1
 */
public class KeyboardView extends LinearLayout {

	private InputMethodService service;

	public KeyboardView(Context context) {
		super(context);
		service = (InputMethodService) context;

		setOrientation(LinearLayout.HORIZONTAL);

		Button btnA = new Button(context);
		btnA.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		btnA.setText("A");
		btnA.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				service.setCandidatesViewShown(true);
			}
		});
		addView(btnA);

		Button btnB = new Button(context);
		btnB.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		btnB.setText("B");
		btnB.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				service.setCandidatesViewShown(true);
			}
		});
		addView(btnB);

		Button btnC = new Button(context);
		btnC.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		btnC.setText("C");
		btnC.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				service.getCurrentInputConnection().commitText("C", 0); // 往输入框输出内容
			}
		});
		addView(btnC);

		service.setCandidatesViewShown(false);
	}

}



最后在AndroidManifest.xml配置文件中添加上这个service


<service android:name="com.xxx.core.server.ImfService" android:label="丸子输入法" android:permission="android.permission.BIND_INPUT_METHOD">
	<intent-filter>
		<action android:name="android.view.InputMethod" />
	</intent-filter>
	<meta-data android:name="android.view.im" android:resource="@xml/input_method" />
</service>



配置好运行就可以了,然后在输入法配置那里选择上刚添加的输入法,然后在文本框输入的时候就可以选择自己添加的输入来输入内容了。