键盘控件KeyboardView的使用

关于编写键盘,部分为了安全而APP自带键盘,比如手机银行;

效果为:


1 布局中增加 KeyboardView 控件;

2 设置 KeyboardView 监听键盘事件修改EditText


全部代码:
src/cn.demo.inputmethod/MainActivity.java
src/cn.demo.inputmethod/KeyboardUtil.java
res/layout/activity_main.xml
res/layout/key_preview_layout.xml
res/xml/qwerty.xml

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="none"
        android:text="@string/hello_world"
        android:textSize="30sp" />
    
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <android.inputmethodservice.KeyboardView
            android:id="@+id/keyboardView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:keyPreviewLayout="@layout/key_preview_layout"
            android:keyBackground="@drawable/btn_keyboard_key" 
            android:keyTextColor="@android:color/white"/>
    </RelativeLayout>

</LinearLayout>

MainActivity.java

public class MainActivity extends Activity {

	private EditText editText;
	private KeyboardView keyboardView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		editText = (EditText) findViewById(R.id.editText);
		keyboardView = (KeyboardView) findViewById(R.id.keyboardView);
		editText.setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if(!editText.hasFocus()){
					int inputback = editText.getInputType();
					editText.setInputType(InputType.TYPE_NULL);
					new KeyboardUtil(keyboardView,editText).showKeyboard();
					editText.setInputType(inputback);
				}
				return false;
			}
		});
	}
}

KeyboardUtil.java

public class KeyboardUtil {

	private KeyboardView keyboardView;
	private EditText editText;
	private Keyboard k1;// 字母键盘

	public KeyboardUtil(KeyboardView keyboardView1, EditText editText) {
		super();
		keyboardView = keyboardView1;
		this.editText = editText;
		keyboardView.setOnKeyboardActionListener(listener);
		k1 = new Keyboard(editText.getContext(), R.xml.qwerty);
		keyboardView.setKeyboard(k1);
		keyboardView.setEnabled(true);
		keyboardView.setPreviewEnabled(true);
	}

	private OnKeyboardActionListener listener = new OnKeyboardActionListener() {

		@Override
		public void swipeUp() {
		}

		@Override
		public void swipeRight() {
		}

		@Override
		public void swipeLeft() {
		}

		@Override
		public void swipeDown() {
		}

		@Override
		public void onText(CharSequence text) {
		}

		@Override
		public void onRelease(int primaryCode) {
		}

		@Override
		public void onPress(int primaryCode) {
		}

		@Override
		public void onKey(int primaryCode, int[] keyCodes) {
			Editable editable = editText.getText();
			int start = editText.getSelectionStart();
			switch (primaryCode) {
			case Keyboard.KEYCODE_DELETE:
				if (editable != null && editable.length() > 0) {
					if (start > 0) {
						editable.delete(start - 1, start);
					}
				}
				break;
			case Keyboard.KEYCODE_CANCEL:
				keyboardView.setVisibility(View.GONE);
				break;
			default:
				editable.insert(start, Character.toString((char) primaryCode));
				break;
			}
		}
	};

	// Activity中获取焦点时调用,显示出键盘
	public void showKeyboard() {
		int visibility = keyboardView.getVisibility();
		if (visibility == View.GONE || visibility == View.INVISIBLE) {
			keyboardView.setVisibility(View.VISIBLE);
		}
	}

}
其中需要建立键盘布局

使用xml文件建立

private Keyboard k1;
k1 = new Keyboard(editText.getContext(), R.xml.qwerty);
keyboardView.setKeyboard(k1);

其中有:

xml/qwerty.xml

<?xml version="1.0" encoding="UTF-8"?>
<Keyboard android:keyWidth="10.000002%p" android:keyHeight="@dimen/key_height"
	android:horizontalGap="0.0px" android:verticalGap="0.0px"
	xmlns:android="http://schemas.android.com/apk/res/android">
	<Row>
		<Key android:codes="113" android:keyEdgeFlags="left"
			android:keyLabel="q" />
		<Key android:codes="119" android:keyLabel="w" />
		<Key android:codes="101" android:keyLabel="e" />
		<Key android:codes="114" android:keyLabel="r" />
		<Key android:codes="116" android:keyLabel="t" />
		<Key android:codes="121" android:keyLabel="y" />
		<Key android:codes="117" android:keyLabel="u" />
		<Key android:codes="105" android:keyLabel="i" />
		<Key android:codes="111" android:keyLabel="o" />
		<Key android:codes="112" android:keyEdgeFlags="right"
			android:keyLabel="p" />
	</Row>
	<Row>
		<Key android:horizontalGap="4.999995%p" android:codes="97"
			android:keyEdgeFlags="left" android:keyLabel="a" />
		<Key android:codes="115" android:keyLabel="s" />
		<Key android:codes="100" android:keyLabel="d" />
		<Key android:codes="102" android:keyLabel="f" />
		<Key android:codes="103" android:keyLabel="g" />
		<Key android:codes="104" android:keyLabel="h" />
		<Key android:codes="106" android:keyLabel="j" />
		<Key android:codes="107" android:keyLabel="k" />
		<Key android:codes="108" android:keyEdgeFlags="right"
			android:keyLabel="l" />
	</Row>
	<Row>
		<Key android:keyWidth="14.999998%p" android:codes="-1"
			android:keyEdgeFlags="left" android:isModifier="true"
			android:isSticky="true" android:keyIcon="@drawable/sym_keyboard_shift" />
		<Key android:codes="122" android:keyLabel="z" />
		<Key android:codes="120" android:keyLabel="x" />
		<Key android:codes="99" android:keyLabel="c" />
		<Key android:codes="118" android:keyLabel="v" />
		<Key android:codes="98" android:keyLabel="b" />
		<Key android:codes="110" android:keyLabel="n" />
		<Key android:codes="109" android:keyLabel="m" />
		<Key android:keyWidth="14.999998%p" android:codes="-5"
			android:keyEdgeFlags="right" android:isRepeatable="true"
			android:keyIcon="@drawable/sym_keyboard_delete" />
	</Row>
	<Row android:rowEdgeFlags="bottom">
		<Key android:keyWidth="20.000004%p" android:codes="-2"
			android:keyLabel="12#" />
		<Key android:keyWidth="14.999998%p" android:codes="44"
			android:keyLabel="," />
		<Key android:keyWidth="29.999996%p" android:codes="32"
			android:isRepeatable="true" android:keyIcon="@drawable/sym_keyboard_space" />
		<Key android:keyWidth="14.999998%p" android:codes="46"
			android:keyLabel="." />
		<Key android:keyWidth="20.000004%p" android:codes="-3"
			android:keyEdgeFlags="right" android:keyLabel="完成" />
	</Row>
</Keyboard>

补充下按键高度:<dimen name="key_height">50.0dip</dimen>

key_preview_layout.xml

作为提示的popWindow的布局编写:


<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#ff8888ff"
    android:gravity="center"
    android:textColor="@android:color/white"
    android:textSize="40sp" />

关于在MainActivity.java的editText.setOnTouchListener的监听中增加判断

if(!editText.hasFocus())

为了不重复设置,导致光标一直停留在文字末尾

但是如果初始时候,已经获取焦点了,就没有办法进入if里面导致键盘没有setVisible;

那么加点小技巧,比如一开始就不让EditText获取到焦点;

所以看到布局文件中有最外层布局LinearLayout:

android:focusable="true"

android:focusableInTouchMode="true"

参考 http://www.2cto.com/kf/201206/134893.html

那么最后放上项目源码的链接:

http://pan.baidu.com/s/1dEcflYh



发布了105 篇原创文章 · 获赞 40 · 访问量 27万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 精致技术 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览