首先晒一个自定义键盘的效果图:
大多数的交易软件,都会自定义键盘,以便于让用户更好的输入,比如手数等其他现成的数字,对于这种要求美观比较复杂的自定义键盘,使用自带的谷歌的自定义键盘,我尝试过,哪些圆角还有图片等等,都不好弄,所以最后选择了使用弹出popuwindow方式,然后将键盘的写在布局里面,类似的文章已经有写过,具体的实现可以参考这个:自定义布局的模拟键盘,其实布局不难,难点在于edittext的控件,对于小数点的处理,还有删除的按键,点击加1等这些操作,需要使用到stringbuffer还有控制整数位数和小数位数,如下界面和控制类:
package com.allone.CTPMobile.component.popuwindow.chartmenu.impl;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;
import com.allone.CTPMobile.R;
import com.allone.CTPMobile.component.popuwindow.chartmenu.IPopupMenu;
import com.allone.CTPMobile.event.IStationEventName;
import com.allone.CTPMobile.event.StationEventCaptain;
import com.allone.CTPMobile.event.StationEventData;
import com.allone.CTPMobile.util.CommData;
import com.allone.CTPMobile.util.SharepreferencesUtilSystemSettings;
/**
* 小数数字键盘,点击edittext控件,下方弹出键盘
*
* @author qiulinhe
* @createTime 2018年5月16日14:19:29
*/
public class DonteKeyboardPopupMenu implements IPopupMenu {
//标记整数的位数
public static final int left_int_MAXLENGTH = 99999999;
//加1或者减1的变量
private long addLong = 0;
private long reduceLong = 0;
private LinearLayout add_selfdefine, reduce_define, delete_layout;
private Button add_button, reduce_button;
private int digit;
//=============键盘数字初始化============
private TextView hide_keyboard;//隐藏键盘
private Button seven, eight, nine, first_selfdefine;//键盘的第一行
private Button four, five, six, second_selfdefine;//键盘的第一行
private Button one, two, three, three_selfdefine;//键盘的第一行
private Button double_zero, zero, donte, delete_btn;//键盘的第一行
private StringBuffer inputStringAdd = new StringBuffer();//用来存放输入数字的sb
//==========================================
private PopupWindow popupMenu;
private Activity activity;
private EditText sourceEditView;//输入的edittext,弹出键盘
private View keylinearlayout;//为了计算键盘的尺寸,传入布局
private int keyboardType;//键盘类型
public DonteKeyboardPopupMenu(Activity activity, View sourceEditView, View sourLayout, int
keyboardType, int instrumentdigit) {
this.activity = activity;
this.sourceEditView = (EditText) sourceEditView;
this.keylinearlayout = sourLayout;
this.keyboardType = keyboardType;
this.digit = instrumentdigit;
initComponent();
}
private void initComponent() {
//当从键盘切换到另一个键盘,重新回来,需要获得原来的已经输入的字符串
inputStringAdd.append(sourceEditView.getText().toString());
View view = activity.getLayoutInflater().inflate(R.layout.keyboard_donte, null);
// 创建弹出窗口
// 窗口内容为layoutLeft,里面包含一个ListView
// 窗口宽度跟tvLeft一样
popupMenu = new PopupWindow(view, keylinearlayout.getWidth(), LayoutParams.WRAP_CONTENT);
ColorDrawable cd = new ColorDrawable(activity.getResources().getColor(R.color.white));
//popupMenu.setBackgroundDrawable(cd);
popupMenu.setBackgroundDrawable(null);
if (CommData.isScreenOriatationPortrait(activity)) {
popupMenu.setAnimationStyle(R.style.PortKeyAnimationFade);
} else {
popupMenu.setAnimationStyle(R.style.AnimBottom);
}
popupMenu.update();
popupMenu.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
popupMenu.setTouchable(true); // 设置popupwindow可点击
popupMenu.setOutsideTouchable(false); // 设置popupwindow外部可点击
popupMenu.setFocusable(false); // 获取焦点
popupMenu.setTouchInterceptor(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// 如果点击了popupwindow的外部,popupwindow也会消失
// 这里如果返回true的话,touch事件将被拦截
// 拦截后 PopupWindow的onTouchEvent不被调用,这样点击外部区域无法dismiss
if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
//popupMenu.dismiss();
StationEventCaptain.getInstance().fireEventDataChange(new StationEventData
(IStationEventName.HEDGE_KEYBORAD_DIMISS, true));
return true;
}
return false;
}
});
//初始化键盘上的数字
initALLDig(view);
}
@Override
public PopupWindow createPupupWindow() {
return popupMenu;
}
@Override
public void showPopupMenu() {
if (popupMenu != null && popupMenu.isShowing()) {
popupMenu.dismiss();
StationEventCaptain.getInstance().fireEventDataChange(new StationEventData
(IStationEventName.HEDGE_KEYBORAD_DIMISS, true));
} else {
popupMenu.showAtLocation(keylinearlayout, Gravity.BOTTOM | Gravity.LEFT, 0, 0);
//设置layout在PopupWindow中显示的位置
}
}
/**
* 将dp转成像素的
*
* @param context
* @param dp
* @return
*/
static int dpToPx(final Context context, final float dp) {
return (int) (dp * context.getResources().getDisplayMetrics().density);
}
@Override
public void destroyPopupMenu() {
popupMenu.dismiss();
StationEventCaptain.getInstance().fireEventDataChange(new StationEventData
(IStationEventName.HEDGE_KEYBORAD_DIMISS, true));
}
/**
* 初始化键盘上的所有数字,进行监听处理
*
* @param view
* @author qiulinhe
* @createTime 2016年5月31日 下午2:24:34
*/
private void initALLDig(View view) {
// 自定义输入金额key,2015年12月21日14:28:06:当用户点击自定义金额,清除掉之前的内容,输入自定义金额
final String selfdig20 = SharepreferencesUtilSystemSettings.getValue(activity, "amount2",
"200,000");
final String selfdig50 = SharepreferencesUtilSystemSettings.getValue(activity, "amount5",
"500,000");
final String selfdig100 = SharepreferencesUtilSystemSettings.getValue(activity,
"amount10", "1,000,000");
//=====================第一行,隐藏键盘的按钮===============================
hide_keyboard = (TextView) view.findViewById(R.id.hide_keyboard);
hide_keyboard.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
popupMenu.dismiss();
StationEventCaptain.getInstance().fireEventDataChange(new StationEventData
(IStationEventName.HEDGE_KEYBORAD_DIMISS, true));
}
});
//=================第二行,7,8,9,第一个自定义金额=======================
seven = (Button) view.findViewById(R.id.seven);
seven.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
//追加数字
inputAppendData(seven);
}
});
eight = (Button) view.findViewById(R.id.eight);
eight.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
//追加数字
inputAppendData(eight);
}
});
nine = (Button) view.findViewById(R.id.nine);
nine.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
//追加数字
inputAppendData(nine);
}
});
//=================第二行4,5,6,第二个自定义金额=======================
four = (Button) view.findViewById(R.id.four);
four.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
//追加数字
inputAppendData(four);
}
});
five = (Button) view.findViewById(R.id.five);
five.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
//追加数字
inputAppendData(five);
}
});
six = (Button) view.findViewById(R.id.six);
six.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
//追加数字
inputAppendData(six);
}
});
add_selfdefine = (LinearLayout) view.findViewById(R.id.second_selfdefine);
add_selfdefine.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
//如果是输入为小数位,禁止自定义金额点击
if (keyboardType == IPopupMenu.MENU_TYPE_JPY) {
} else {
//进行加1的操作
addOne();
}
}
});
add_button = (Button) view.findViewById(R.id.add_button);
add_button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
//如果是输入为小数位,禁止自定义金额点击
if (keyboardType == IPopupMenu.MENU_TYPE_JPY) {
} else {
//进行加1的操作
addOne();
}
}
});
//=================第二行1,2,3,第三个自定义金额=======================
one = (Button) view.findViewById(R.id.one);
one.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
//追加数字
inputAppendData(one);
}
});
two = (Button) view.findViewById(R.id.two);
two.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
//追加数字
inputAppendData(two);
}
});
three = (Button) view.findViewById(R.id.three);
three.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
//追加数字
inputAppendData(three);
}
});
reduce_define = (LinearLayout) view.findViewById(R.id.three_selfdefine);
reduce_button = (Button) view.findViewById(R.id.reduce_button);
//three_selfdefine.setText(selfdig100);
reduce_define.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
//如果是输入为小数位,禁止自定义金额点击
if (keyboardType == IPopupMenu.MENU_TYPE_JPY) {
} else {
//进行减1的操作
reduceOne();
}
}
});
reduce_button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
if (keyboardType == IPopupMenu.MENU_TYPE_JPY) {
} else {
//进行减1的操作
reduceOne();
}
}
});
//=================第二行00,0,.,第四个自定义金额=======================
zero = (Button) view.findViewById(R.id.zero);
zero.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
//追加数字
inputAppendData(zero);
}
});
donte = (Button) view.findViewById(R.id.donte);
donte.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
//当键盘为输入金额的时候,小数点不能点击
if (keyboardType == IPopupMenu.MENU_TYPE_AMOUNT || keyboardType == IPopupMenu
.MENU_TYPE_HEDGE_Left || keyboardType == IPopupMenu.MENU_TYPE_HEDGE_RIGHT) {
donte.setEnabled(false);
} else {
//如果已经有有一个小数点,则不能再点击
if (sourceEditView.getText().toString().contains(".")) {
//donte.setEnabled(false);
} else if (sourceEditView.getText().toString().equals("") || sourceEditView
.getText().toString() == null) {//当一开始什么都没有输入的时候,输入.,应该显示0.
//donte.setEnabled(true);
Handler handler = new Handler();
handler.post(new Runnable() {
@Override
public void run() {
sourceEditView.setText("0.");
inputStringAdd.append(sourceEditView.getText().toString().trim());
//设置光标位于最后一位
sourceEditView.setSelection(sourceEditView.getText().length());
}
});
} else {
//追加数字,只能有一个小数点
inputAppendData(donte);
}
}
}
});
delete_btn = (Button) view.findViewById(R.id.delete_btn);
delete_layout = (LinearLayout) view.findViewById(R.id.delete_layout);
delete_btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
//删除功能
deleteAppendData();
}
});
delete_layout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
//删除功能
deleteAppendData();
}
});
}
/**
* 点击数字,进行追加
*
* @author qiulinhe
* @createTime 2016年5月31日 下午2:59:29
*/
private void inputAppendData(Button digBtn) {
//当键盘为输入金额的时候,小数点不能点击,且只能输入7位数,不能输入小数
if (keyboardType == IPopupMenu.MENU_TYPE_AMOUNT || keyboardType == IPopupMenu
.MENU_TYPE_HEDGE_Left || keyboardType == IPopupMenu.MENU_TYPE_HEDGE_RIGHT) {
String oldValue = inputStringAdd.toString().trim();
String newValue = digBtn.getText().toString().trim();//千分位格式化
String dValue = oldValue + newValue;
if (!dValue.equals("") && parseDonte(dValue) > 7) {
Toast.makeText(activity, "只能输入8位数", Toast.LENGTH_SHORT).show();
} else {
inputStringAdd.append(digBtn.getText().toString().trim());
sourceEditView.setText(inputStringAdd.toString().trim());
//设置光标位于最后一位
sourceEditView.setSelection(sourceEditView.getText().length());
}
} else if (keyboardType == IPopupMenu.MENU_TYPE_JPY) {//当商品含有JPY的,输入的数值:整数位最多为3位,小数位最多为2位
//如果有文字的话,先删除掉文字,重置为空字符串
String oriValue = inputStringAdd.toString().trim();
if (oriValue.contains("价")) {
inputStringAdd.delete(0, inputStringAdd.length());
}
sourceEditView.setText(inputStringAdd.append(ifJPYReturnData(digBtn)));
//设置光标位于最后一位
sourceEditView.setSelection(sourceEditView.getText().length());
}
}
/**
* 对小数进行处理
*
* @param digBtn
* @author qiulinhe
* @createTime 2016年6月1日 上午10:45:19
*/
private String ifJPYReturnData(Button digBtn) {
//去除文字的部分
String oriValue = inputStringAdd.toString().trim();
if (oriValue.contains("价")) {
oriValue = "0";
}
StringBuffer sb = new StringBuffer(oriValue);
sb.append(digBtn.getText().toString().trim());
String newValue = sb.toString();
String[] newValueVec = newValue.split("\\.");
if (newValueVec.length == 2) {
double number = Double.parseDouble(newValueVec[0]);
boolean numberflag = true;
//控制整数在8位以内
numberflag = ((number - left_int_MAXLENGTH > 0.000001) ? false : true);
boolean digitflag = true;
try {
String digitNumber = newValueVec[1];
digitflag = digitNumber.toCharArray().length > digit ? false : true;
} catch (Exception ex) {
digitflag = false;
}
if (numberflag && digitflag) {
return digBtn.getText().toString().trim();
} else {
return "";
}
} else {
double value = Double.parseDouble(newValue);
//控制整数在8位以内
return value > left_int_MAXLENGTH ? "" : digBtn.getText().toString().trim();
}
}
/**
* 删除功能
*
* @author qiulinhe
* @createTime 2016年5月31日 下午3:03:03
*/
private void deleteAppendData() {
//当键盘为输入金额的时候,小数点不能点击,且只能输入7位数,不能输入小数
if (keyboardType == IPopupMenu.MENU_TYPE_AMOUNT || keyboardType == IPopupMenu
.MENU_TYPE_HEDGE_Left || keyboardType == IPopupMenu.MENU_TYPE_HEDGE_RIGHT) {
String dValue = sourceEditView.getText().toString();
//格式化之后重新赋值
inputStringAdd.setLength(0);
inputStringAdd.append(dValue);
if (inputStringAdd.length() - 1 >= 0) {
inputStringAdd.delete(inputStringAdd.length() - 1, inputStringAdd.length());
sourceEditView.setText(inputStringAdd.toString().trim());
sourceEditView.setSelection(sourceEditView.getText().length());
}
} else {
if (inputStringAdd.length() - 1 >= 0) {
inputStringAdd.delete(inputStringAdd.length() - 1, inputStringAdd.length());
sourceEditView.setText(inputStringAdd.toString().trim());
sourceEditView.setSelection(sourceEditView.getText().length());
}
}
}
/**
* 自定义金额,输入自定义金额时,将原来的界面清空,填入自定义金额
*
* @author qiulinhe
* @createTime 2016年5月31日 下午3:04:02
*/
private void selfDefAppendData(String selfData) {
inputStringAdd.replace(0, inputStringAdd.length(), selfData);
sourceEditView.setText(inputStringAdd.toString().trim());
sourceEditView.setSelection(sourceEditView.getText().length());
}
/**
* 去掉千分位转换成double类型
*
* @param data
* @return
* @author qiulinhe
* @createTime 2016年7月18日 下午3:27:31
*/
public int parseDonte(String data) {
int strlength;
if (data.equalsIgnoreCase("") || data == null) {
strlength = 0;
} else {
String parStr = data.replaceAll(",", "").trim();
strlength = parStr.length();
}
return strlength;
}
//点击增加的按钮
private void addOne() {
addLong = 0;
//先获取已经输入的数值,然后加1的操作
String dValue = sourceEditView.getText().toString();
if (dValue == null || "".equals(dValue)) {
} else {
addLong = (long) Long.parseLong(dValue);
addLong = addLong + 1;
}
sourceEditView.setText(addLong + "");
sourceEditView.setSelection(sourceEditView.getText().length());
}
//点击减少的按钮
private void reduceOne() {
reduceLong = 0;
//先获取已经输入的数值,然后加1的操作
String dValue = sourceEditView.getText().toString();
if (dValue == null || "".equals(dValue)) {
} else {
reduceLong = (long) Long.parseLong(dValue);
if (reduceLong <= 1) {
reduceLong = 1;
} else {
reduceLong = reduceLong - 1;
}
}
sourceEditView.setText(reduceLong + "");
sourceEditView.setSelection(sourceEditView.getText().length());
}
}
因为是公司的项目,有些代码不全,有问题可以问我.欢迎赐教