android 制作输入法

android 制作输入法

效果如图:



在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/>
 * E-Mail: [email protected]<br/>
 * QQ: 176291935<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/>
 * E-Mail: [email protected]<br/>
 * QQ: 176291935<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/>
 * E-Mail: [email protected]<br/>
 * QQ: 176291935<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>


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

猜你喜欢

转载自iaiai.iteye.com/blog/1810983