键盘控件KeyboardView的使用

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

效果为:

键盘控件KeyboardView的使用_第1张图片

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



    
    
    

        
    


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



	
		
		
		
		
		
		
		
		
		
		
	
	
		
		
		
		
		
		
		
		
		
	
	
		
		
		
		
		
		
		
		
		
	
	
		
		
		
		
		
	

补充下按键高度:50.0dip

key_preview_layout.xml

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





关于在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



你可能感兴趣的:(Android进阶)