android 仿ios 键盘
来源:互联网 发布:php 去除所有html标签 编辑:程序博客网 时间:2024/05/16 11:39
因为使用android的键盘类无法实现仿ios的效果 所以自定义布局。
自定义键盘类:
public class SafetyKeyBoard extends LinearLayout implements android.view.View.OnClickListener{/*** 身份证键盘 有X*/public static final int TYPE_ID_CARD = 0;/*** 数字键盘 有小数点*/public static final int TYPE_NUMBER = 1;/*** 银行键盘 不带小数点*/public static final int TYPE_BANK = 2;private Context mContext;private EditText mEditText;private int type = -1;private Button key_board_other;public SafetyKeyBoard(Context context) {super(context);mContext = context;initView();}public SafetyKeyBoard(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);mContext = context;initView();}public SafetyKeyBoard(Context context, AttributeSet attrs) {super(context, attrs);mContext = context;initView();}// 初始化private void initView() {LayoutInflater inflater=(LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);inflater.inflate(R.layout.key_board, this);// 查找控件Button key_board_0 = (Button) findViewById(R.id.key_board_0);Button key_board_1 = (Button) findViewById(R.id.key_board_1);Button key_board_2 = (Button) findViewById(R.id.key_board_2);Button key_board_3 = (Button) findViewById(R.id.key_board_3);Button key_board_4 = (Button) findViewById(R.id.key_board_4);Button key_board_5 = (Button) findViewById(R.id.key_board_5);Button key_board_6 = (Button) findViewById(R.id.key_board_6);Button key_board_7 = (Button) findViewById(R.id.key_board_7);Button key_board_8 = (Button) findViewById(R.id.key_board_8);Button key_board_9 = (Button) findViewById(R.id.key_board_9);key_board_other = (Button) findViewById(R.id.key_board_other);ImageButton key_board_del = (ImageButton) findViewById(R.id.key_board_del);key_board_done = (ImageButton) findViewById(R.id.key_board_done);Button key_board_ok = (Button) findViewById(R.id.key_board_ok);// 设置点击事件key_board_0.setOnClickListener(this);key_board_1.setOnClickListener(this);key_board_2.setOnClickListener(this);key_board_3.setOnClickListener(this);key_board_4.setOnClickListener(this);key_board_5.setOnClickListener(this);key_board_6.setOnClickListener(this);key_board_7.setOnClickListener(this);key_board_8.setOnClickListener(this);key_board_9.setOnClickListener(this);key_board_other.setOnClickListener(this);key_board_del.setOnClickListener(this);key_board_ok.setOnClickListener(this);key_board_done.setOnClickListener(this);// 默认显示数字键盘type = TYPE_NUMBER;}private OnOkClickListerner mOkListerner;private OnDownClickListener mDownListerner;private ImageButton key_board_done;public interface OnOkClickListerner {/** * 确定按钮点击 * @param text */void setOnOkListener(String text);}public interface OnDownClickListener{/** * 点击了隐藏按钮 */void setOnDownLitener();}public void setOklistener(OnOkClickListerner listerner) {this.mOkListerner = listerner;}public void setDownListener(OnDownClickListener downListerner) {this.mDownListerner = downListerner;}public void setTypeEdit(EditText edit,int type) {this.mEditText = edit;this.type = type; // 按类型显示键盘if(type == TYPE_NUMBER) {key_board_other.setText(".");key_board_other.setBackgroundResource(R.drawable.key_board_number);} else if(type == TYPE_BANK) {key_board_other.setText("");key_board_other.setBackgroundColor(Color.parseColor("#E1E2E6"));} else if(type == TYPE_ID_CARD) {key_board_other.setText("X");key_board_other.setBackgroundResource(R.drawable.key_board_number);}}/** * 设置隐藏按钮不显示 */public void setDownHide() {if(key_board_done != null) {key_board_done.setEnabled(false);key_board_done.setBackgroundColor(Color.parseColor("#E1E2E6"));ColorDrawable dw = new ColorDrawable(Color.parseColor("#E1E2E6")); key_board_done.setImageDrawable(dw);}}@Overridepublic void onClick(View v) {if(v == null)return;if (mEditText == null) {return;}if(type == -1) {return;}Editable text = mEditText.getText();// 获取EditText 当前光标所处的位置int start = mEditText.getSelectionStart();switch (v.getId()) {case R.id.key_board_0:text.insert(start, "0");break;case R.id.key_board_1:text.insert(start, "1");break;case R.id.key_board_2:text.insert(start, "2");break;case R.id.key_board_3:text.insert(start, "3");break;case R.id.key_board_4:text.insert(start, "4");break;case R.id.key_board_5:text.insert(start, "5");break;case R.id.key_board_6:text.insert(start, "6");break;case R.id.key_board_7:text.insert(start, "7");break;case R.id.key_board_8:text.insert(start, "8");break;case R.id.key_board_9:text.insert(start, "9");break;case R.id.key_board_other:if(type == TYPE_NUMBER) {text.insert(start, ".");} else if(type == TYPE_BANK) {// 银行卡键盘不能输入小数点} else if(type == TYPE_ID_CARD) {text.insert(start, "X");} break;case R.id.key_board_del:if(mEditText.length() > 0 && start > 0) {text.delete(start - 1, start);}break;case R.id.key_board_ok:if(mOkListerner != null) {mOkListerner.setOnOkListener(text.toString());}break;case R.id.key_board_done:if(mDownListerner != null) {mDownListerner.setOnDownLitener();}break;}}public String getText() {String text = "";text = mEditText.getText().toString();return text;}}布局:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#C9C9C9" android:baselineAligned="false" android:orientation="horizontal" android:paddingTop="1px" > <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_marginRight="1px" android:layout_weight="3" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_marginBottom="1px" android:layout_weight="1" android:orientation="horizontal" > <Button android:id="@+id/key_board_1" android:layout_width="0dp" android:layout_height="match_parent" android:layout_marginRight="1px" android:layout_weight="1" android:background="@drawable/key_board_number" android:text="1" android:textColor="#000000" android:textSize="24sp" /> <Button android:id="@+id/key_board_2" android:layout_width="0dp" android:layout_height="match_parent" android:layout_marginRight="1px" android:layout_weight="1" android:background="@drawable/key_board_number" android:text="2" android:textColor="#000000" android:textSize="24sp" /> <Button android:id="@+id/key_board_3" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/key_board_number" android:text="3" android:textColor="#000000" android:textSize="24sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_marginBottom="1px" android:layout_weight="1" android:orientation="horizontal" > <Button android:id="@+id/key_board_4" android:layout_width="0dp" android:layout_height="match_parent" android:layout_marginRight="1px" android:layout_weight="1" android:background="@drawable/key_board_number" android:text="4" android:textColor="#000000" android:textSize="24sp" /> <Button android:id="@+id/key_board_5" android:layout_width="0dp" android:layout_height="match_parent" android:layout_marginRight="1px" android:layout_weight="1" android:background="@drawable/key_board_number" android:text="5" android:textColor="#000000" android:textSize="24sp" /> <Button android:id="@+id/key_board_6" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/key_board_number" android:text="6" android:textColor="#000000" android:textSize="24sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_marginBottom="1px" android:layout_weight="1" android:orientation="horizontal" > <Button android:id="@+id/key_board_7" android:layout_width="0dp" android:layout_height="match_parent" android:layout_marginRight="1px" android:layout_weight="1" android:background="@drawable/key_board_number" android:text="7" android:textColor="#000000" android:textSize="24sp" /> <Button android:id="@+id/key_board_8" android:layout_width="0dp" android:layout_height="match_parent" android:layout_marginRight="1px" android:layout_weight="1" android:background="@drawable/key_board_number" android:text="8" android:textColor="#000000" android:textSize="24sp" /> <Button android:id="@+id/key_board_9" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/key_board_number" android:text="9" android:textColor="#000000" android:textSize="24sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="horizontal" > <Button android:id="@+id/key_board_other" android:layout_width="0dp" android:layout_height="match_parent" android:layout_marginRight="1px" android:layout_weight="1" android:background="@drawable/key_board_number" android:text="." android:textColor="#000000" android:textSize="24sp" /> <Button android:id="@+id/key_board_0" android:layout_width="0dp" android:layout_height="match_parent" android:layout_marginRight="1px" android:layout_weight="1" android:background="@drawable/key_board_number" android:text="0" android:textColor="#000000" android:textSize="24sp" /> <ImageButton android:id="@+id/key_board_done" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/key_board_number" android:src="@drawable/key_board_done" /> </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:orientation="vertical" > <ImageButton android:id="@+id/key_board_del" android:layout_width="match_parent" android:layout_height="0dp" android:layout_margin="0dp" android:layout_weight="1" android:background="@drawable/key_board_number" android:padding="0dp" android:scaleType="center" android:src="@drawable/key_board_del" /> <Button android:id="@+id/key_board_ok" android:layout_width="match_parent" android:layout_height="0dp" android:layout_margin="0dp" android:layout_weight="1" android:background="@drawable/key_board_ok" android:gravity="center" android:padding="0dp" android:text="确定" android:textColor="#ffffff" android:textSize="26sp" /> </LinearLayout></LinearLayout>支付密码输入框:
public class PayPwdEditText extends EditText {private int textLength;// 文字长度 private int borderColor;// 边框颜色 private float borderWidth;// 边框宽度 private float borderRadius;// 边框半径 private int passwordLength;// 密码长度 private int passwordColor;// 密码颜色 private float passwordWidth;// 密码宽度 private float passwordRadius;// 密码半径 private Paint passwordPaint = new Paint(Paint.ANTI_ALIAS_FLAG);// 密码的画笔 private Paint borderPaint = new Paint(Paint.ANTI_ALIAS_FLAG);// 边框的画笔 private final int defaultContMargin = 1; private final int defaultSplitLineWidth = 1;public PayPwdEditText(Context context, AttributeSet attrs) {super(context, attrs);final Resources res = getResources(); // 获取颜色 final int defaultBorderColor = res.getColor(R.color.default_ev_border_color); final float defaultBorderWidth = res.getDimension(R.dimen.default_ev_border_width); final float defaultBorderRadius = res.getDimension(R.dimen.default_ev_border_radius); // 获取长宽 final int defaultPasswordLength = res.getInteger(R.integer.default_ev_password_length); final int defaultPasswordColor = res.getColor(R.color.default_ev_password_color); final float defaultPasswordWidth = res.getDimension(R.dimen.default_ev_password_width); final float defaultPasswordRadius = res.getDimension(R.dimen.default_ev_password_radius); // 获取样式 TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.PasswordInputView, 0, 0); try { borderColor = a.getColor(R.styleable.PasswordInputView_borderColor, defaultBorderColor); borderWidth = a.getDimension(R.styleable.PasswordInputView_borderWidths, defaultBorderWidth); borderRadius = a.getDimension(R.styleable.PasswordInputView_borderRadius, defaultBorderRadius); passwordLength = a.getInt(R.styleable.PasswordInputView_passwordLength, defaultPasswordLength); passwordColor = a.getColor(R.styleable.PasswordInputView_passwordColor, defaultPasswordColor); passwordWidth = a.getDimension(R.styleable.PasswordInputView_passwordWidth, defaultPasswordWidth); passwordRadius = a.getDimension(R.styleable.PasswordInputView_passwordRadius, defaultPasswordRadius); } finally { a.recycle(); } borderPaint.setStrokeWidth(borderWidth); borderPaint.setColor(borderColor); passwordPaint.setStrokeWidth(passwordWidth); passwordPaint.setStyle(Paint.Style.FILL); passwordPaint.setColor(passwordColor);} @Override protected void onDraw(Canvas canvas) { int width = getWidth(); int height = getHeight(); // 外边框 RectF rect = new RectF(0, 0, width, height); borderPaint.setColor(borderColor); canvas.drawRoundRect(rect, borderRadius, borderRadius, borderPaint); // 内容区 RectF rectIn = new RectF(rect.left + defaultContMargin, rect.top + defaultContMargin,rect.right - defaultContMargin, rect.bottom - defaultContMargin); borderPaint.setColor(Color.WHITE); canvas.drawRoundRect(rectIn, borderRadius, borderRadius, borderPaint); // 分割线 borderPaint.setColor(borderColor); borderPaint.setStrokeWidth(defaultSplitLineWidth); for (int i = 1; i < passwordLength; i++) { float x = width * i / passwordLength; canvas.drawLine(x, 0, x, height, borderPaint); } // 密码 float cx, cy = height/ 2; float half = width / passwordLength / 2; for(int i = 0; i < textLength; i++) { cx = width * i / passwordLength + half; canvas.drawCircle(cx, cy, passwordWidth, passwordPaint); } } @Override protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) { super.onTextChanged(text, start, lengthBefore, lengthAfter); this.textLength = text.toString().length(); invalidate(); } public int getBorderColor() { return borderColor; } public void setBorderColor(int borderColor) { this.borderColor = borderColor; borderPaint.setColor(borderColor); invalidate(); } public float getBorderWidth() { return borderWidth; } public void setBorderWidth(float borderWidth) { this.borderWidth = borderWidth; borderPaint.setStrokeWidth(borderWidth); invalidate(); } public float getBorderRadius() { return borderRadius; } public void setBorderRadius(float borderRadius) { this.borderRadius = borderRadius; invalidate(); } /**获取密码的长度*/ public int getPasswordLength() { return passwordLength; } public void setPasswordLength(int passwordLength) { this.passwordLength = passwordLength; invalidate(); } public int getPasswordColor() { return passwordColor; } public void setPasswordColor(int passwordColor) { this.passwordColor = passwordColor; passwordPaint.setColor(passwordColor); invalidate(); } public float getPasswordWidth() { return passwordWidth; } public void setPasswordWidth(float passwordWidth) { this.passwordWidth = passwordWidth; passwordPaint.setStrokeWidth(passwordWidth); invalidate(); } public float getPasswordRadius() { return passwordRadius; } public void setPasswordRadius(float passwordRadius) { this.passwordRadius = passwordRadius; invalidate(); }}支付密码输入框自定义属性:
<declare-styleable name="PasswordInputView"> <attr name="borderWidths" format="dimension" /> <attr name="borderColor" format="color" /> <attr name="borderRadius" format="dimension" /> <attr name="passwordLength" format="integer" /> <attr name="passwordWidth" format="dimension" /> <attr name="passwordColor" format="color" /> <attr name="passwordRadius" format="dimension" /> </declare-styleable>确认支付密码弹出框:
public class PayPwdDialog implements OnClickListener,OnOkClickListerner {private Context context;private Dialog dialog;private View view ;private LinearLayout lLayout_bg;private Display display;private ImageButton ib_back; // 返回按钮private TextView tv_phone;// 账号private PayPwdEditText et_pwd;// 密码private TextView tv_forgot_pwd;// 忘记密码private SafetyKeyBoard safetyKeyBoard;private String phone;public PayPwdDialog(Context context,PayPwdChangeListener listener) {this.context = context;changeListener = listener;WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);display = windowManager.getDefaultDisplay();}public PayPwdDialog builder() {// 获取Dialog布局loadingDialog = new LoadingDialog(context);view = LayoutInflater.from(context).inflate(R.layout.view_pwd_once, null);// 获取自定义Dialog布局中的控件lLayout_bg = (LinearLayout) view.findViewById(R.id.lLayout_bg);safetyKeyBoard = (SafetyKeyBoard) view.findViewById(R.id.safetyKeyBoard);ib_back = (ImageButton) view.findViewById(R.id.ib_back);tv_phone = (TextView) view.findViewById(R.id.tv_phone);et_pwd = (PayPwdEditText) view.findViewById(R.id.pwd_etd_pwd);tv_forgot_pwd = (TextView) view.findViewById(R.id.tv_forgot_pwd);// 设置键盘safetyKeyBoard.setTypeEdit(et_pwd, SafetyKeyBoard.TYPE_BANK);safetyKeyBoard.setDownHide();safetyKeyBoard.setOklistener(this);// 定义Dialog布局和参数dialog = new Dialog(context, R.style.ActionSheetDialogStyle);dialog.requestWindowFeature(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);dialog.setContentView(view); dialog.setCancelable(false);// 调整dialog背景大小lLayout_bg.setLayoutParams(new FrameLayout.LayoutParams((int) (display.getWidth()), LayoutParams.WRAP_CONTENT));// 设置dialog属性Window window = dialog.getWindow(); window.setGravity(Gravity.BOTTOM); //此处可以设置dialog显示的位置 ib_back.setOnClickListener(this);tv_forgot_pwd.setOnClickListener(this);// 绑定自定义键盘int inputback = et_pwd.getInputType();et_pwd.setInputType(inputback);// 不弹出输入法disableShowSoftInput();// 设置默认的账户UserInfo userInfo = UserMannage.getInstance().getUserInfo();if(userInfo != null) {if(!TextUtils.isEmpty(userInfo.getMobile())) {setAccount(userInfo.getMobile());}}return this;}/** * 获取输入的密码 * @return */public String getPwd() {String pwd = null;if(et_pwd != null) {pwd = et_pwd.getText().toString().trim();if(TextUtils.isEmpty(pwd)) {ToastUtils.show(context, "密码不能为空");return null;} else if(pwd.length() < 6) {ToastUtils.show(context, "支付密码为6位数");return null;} if(!AuthUtils.isNumber(pwd)) {ToastUtils.show(context, "支付密码必须为数字");return null;}}return pwd;}public void show() {if(dialog != null) { dialog.show();}}public void dismiss() {if(dialog != null)dialog.dismiss();} /** * 禁止Edittext弹出软件盘,光标依然正常显示。 */ public void disableShowSoftInput() { if (android.os.Build.VERSION.SDK_INT <= 10) { et_pwd.setInputType(InputType.TYPE_NULL); } else { Class<EditText> cls = EditText.class; Method method; try { method = cls.getMethod("setShowSoftInputOnFocus",boolean.class); method.setAccessible(true); method.invoke(et_pwd, false); }catch (Exception e) { } try { method = cls.getMethod("setSoftInputShownOnFocus",boolean.class); method.setAccessible(true); method.invoke(et_pwd, false); }catch (Exception e) { } } } private PayPwdChangeListener changeListener;private LoadingDialog loadingDialog;/**校验密码回调*/public interface PayListener {/*** * 校验 * @param payPwd */void onPayClickListener(String payPwd);}// 设置回调public void setPayListener(PayPwdChangeListener listener) {changeListener = listener;}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.ib_back:// 返回键if(dialog != null) {dialog.dismiss();}break;case R.id.tv_forgot_pwd:// 忘记密码break;default:break;}}@Overridepublic void setOnOkListener(String text) {if(changeListener != null) {changeListener.onPayClickListener(getPwd());}}}支付密码弹出层布局:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/lLayout_bg" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/white" android:orientation="vertical" > <View android:layout_width="match_parent" android:layout_height="0.5dp" android:background="@color/line" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <ImageButton android:id="@+id/ib_back" android:layout_width="49dp" android:layout_height="49dp" android:layout_alignParentLeft="true" android:layout_gravity="center_vertical" android:background="@drawable/tv_item_selector" android:paddingRight="18dp" android:scaleType="center" android:src="@drawable/back_left" /> <TextView android:layout_width="match_parent" android:layout_height="49dp" android:gravity="center" android:paddingLeft="10dp" android:text="校验支付密码" android:textColor="@color/text_black" android:textSize="20sp" /> </RelativeLayout> <View android:layout_width="match_parent" android:layout_height="0.5dp" android:background="@color/line" /> <LinearLayout android:layout_width="match_parent" android:layout_height="49dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_marginTop="10dp" android:focusable="true" android:focusableInTouchMode="true" android:orientation="horizontal" > <com.gonghuipay.gongfubao.view.PayPwdEditText xmlns:app="http://schemas.android.com/apk/res/com.gonghuipay.gongfubao" android:id="@+id/pwd_etd_pwd" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="@android:color/transparent" android:gravity="center_vertical" android:inputType="number" android:maxLength="6" android:paddingLeft="10dp" android:paddingRight="10dp" android:textSize="17sp" app:borderColor="@color/grayslate" app:borderRadius="@dimen/custom_ev_border_radius" app:borderWidth="@dimen/custom_ev_border_width" app:passwordColor="@color/text_black" app:passwordLength="6" app:passwordWidth="@dimen/custom_ev_password_width" /> </LinearLayout> <TextView android:id="@+id/tv_forgot_pwd" android:layout_width="wrap_content" android:layout_height="32dp" android:layout_gravity="right" android:layout_margin="10dp" android:gravity="center_vertical" android:text="忘记支付密码?" android:textColor="#1C96FE" android:textSize="14sp" /> <View android:layout_width="match_parent" android:layout_height="0.5dp" android:layout_marginTop="20dp" android:background="@color/line" /> <com.gonghuipay.gongfubao.view.keyboard.SafetyKeyBoard android:id="@+id/safetyKeyBoard" android:layout_width="match_parent" android:layout_height="200dp" /></LinearLayout>
0 0
- android 仿ios 键盘
- Android仿IOS日历
- android仿ios弹性
- Android仿ios对话框
- Android仿IOS UIAlertView对话框
- android仿ios开关按钮
- Android仿IOS UIAlertView对话框
- Android仿IOS UIAlertView对话框
- Android仿iOS启动界面
- ios 仿android gallery控件
- Android仿IOS UIAlertView对话框
- android 仿ios开关控件
- Android View 仿iOS SwitchButton
- android仿ios时间控件
- android仿ios时间控件
- Android仿IOS有弹性ScrollView
- Android仿ios录音动画
- android-仿iOS弹出框
- pAdPt_-9 活动的生命周期[LogCat java关键字] 传递数据[Intent 静态变量 全局变量 剪切板]
- 【OC语法】Protocol协议的介绍
- C语言基本数据类型
- C语言基础--函数和结构体
- 【黑马程序员】C语言—通讯录系统的实现
- android 仿ios 键盘
- SpriteBuilder实现2D精灵光影明暗反射效果(一)
- CentOS7 初次安装记录(二)配置apache服务器
- java程序员第七课 java基础加强
- solr主从配置
- Hadoop分布式安装02
- centos7 关闭防火墙 获取外网IP
- iOS开发中数据持久化方案小结
- CC2538开发板ipv6准备工作,编译lib6lowpan,libcoap