【安卓开发】Android中自定义软键盘的使用
程序开发
2023-09-18 07:11:04
自定义的键盘布局和颜色等形式,识别点击识别,正确将内容绑定在 E d i t T e x t EditText EditText上。
编写自定义布局文件
jianpan.xml
属性解释:
keyHeight 软键盘一个键的高度
keyWidth=“25%p” 软键盘一个键的占比
horizontalGap 横向间隔
verticalGap 纵向间隔
codes里值得ASCII值
|
|
|
设置键盘的点击事件
独立的JAVA类
public class Jianpan {private KeyboardView keyboardView;private EditText editText;private Keyboard mykey;//自定义键盘public interface OnEnsureListener{public void onEnsure();}OnEnsureListener onEnsureListener;public void setOnEnsureListener(OnEnsureListener onEnsureListener) {this.onEnsureListener = onEnsureListener;}public Jianpan(KeyboardView keyboardView, EditText editText) {this.keyboardView = keyboardView;this.editText = editText;this.editText.setInputType(InputType.TYPE_NULL);//不弹出系统键盘mykey = new Keyboard(this.editText.getContext(), R.xml.jianpan);this.keyboardView.setKeyboard(mykey);//设置要显示键盘的样式this.keyboardView.setEnabled(true);this.keyboardView.setPreviewEnabled(false);this.keyboardView.setOnKeyboardActionListener(listener);}KeyboardView.OnKeyboardActionListener listener=new KeyboardView.OnKeyboardActionListener() {@Overridepublic void onPress(int i) {}@Overridepublic void onRelease(int i) {}@Overridepublic void onKey(int i, int[] ints) {Editable editable=editText.getText();int start=editText.getSelectionStart();if(i==-1){//删除if(editable!=null&&editable.length()>0){if(start>0){editable.delete(start-1,start);}}}else if(i==-2){//确定onEnsureListener.onEnsure();//接口回调System.out.println(editText.getText().toString());}else if(i==-3){//清零editable.clear();}else{editable.insert(start,Character.toString((char)i));}}@Overridepublic void onText(CharSequence charSequence) {}@Overridepublic void swipeLeft() {}@Overridepublic void swipeRight() {}@Overridepublic void swipeDown() {}@Overridepublic void swipeUp() {}};public void show(){//显示键盘int now=keyboardView.getVisibility();if(now==View.INVISIBLE||now==View.GONE){keyboardView.setVisibility(View.VISIBLE);}}public void hide(){//隐藏键盘int now=keyboardView.getVisibility();if(now==View.VISIBLE||now==View.INVISIBLE){keyboardView.setVisibility(View.GONE);}}
}
主页面的JAVA代码调用自定义软键盘
Jianpan jp=new Jianpan(my,et);jp.show();jp.setOnEnsureListener(new Jianpan.OnEnsureListener() {@Overridepublic void onEnsure() {String nowmoney=et.getText().toString();if(TextUtils.isEmpty(nowmoney)||nowmoney.equals("0")) {getActivity().finish();return;}getActivity().finish();return ;}});
线没咋对齐,懒得调了。
标签:
上一篇:
UE4物体的碰撞检测
下一篇:
相关文章
-
无相关信息