Android开发仿咸鱼键盘DEMO(修改版)

 更新时间:2016年11月11日 14:13:46   作者:Hu_wenpeng  
本文给大家分享一段代码关于android开发高仿咸鱼键盘修改版的实例代码,代码简单易懂,非常实用,需要的朋友参考下吧

在这里布局我就不贴出来了

/** 
* 最终被调用的修改价格dialog 
*/ 
protected void editPriceDialog() { 
// TODO Auto-generated method stub 
editPriceView = View.inflate(this, R.layout.dialog_price_input_keyboard, null); 
priceDialog = new Dialog(this, R.style.contactdialog); 
priceDialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
Window PriceDialogwindow = priceDialog.getWindow(); 
PriceDialogwindow.setGravity(Gravity.BOTTOM); 
priceDialog.setContentView(editPriceView); 
priceDialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation; 
priceDialog.show(); 
tv_softkb_point = (TextView) editPriceView.findViewById(R.id.tv_softkb_point); 
buffer = new StringBuffer(); 
stringbuffer = buffer.toString(); 
buffer2 = new StringBuffer(); 
stringbuffer2 = buffer2.toString(); 
etGoodsPrice = (EditText) editPriceView.findViewById(R.id.et_goodsPrice); 
etGoodsPrice.addTextChangedListener(new TextWatcher() { 
private String etPricetrim; 
@Override 
public void onTextChanged(CharSequence s, int start, int before, int count) { 
etPricetrim = s.toString().trim(); 
if (etPricetrim.length() == 1 && s.toString().equals(".")) { //如果edittext的长度为1并且用户只输入了一个.那么buffer里追加"0" + "." 同时删除我们输入的点 
//如果不删除的在则 buffer里的数据会是 .0. 
buffer.append("0").append(".").deleteCharAt(0); 
etGoodsPrice.setText(buffer.toString()); 
} 
if (s.toString().contains(".")) { //如果s里包含点 
if (s.length() - 1 - s.toString().indexOf(".") > 2) { 
if(tv_softkb_point!=null){ 
tv_softkb_point.setClickable(false); 
} 
if(buffer!=null&&buffer.length()>=1){ 
buffer.deleteCharAt(buffer.length()-1); 
} 
s = s.toString().subSequence(0, s.toString().indexOf(".") + 3); 
etGoodsPrice.setText(s); 
etGoodsPrice.setSelection(s.length()); 
} 
else if(s.length()-1-s.toString().indexOf(".")<=2){ 
if(tv_softkb_point!=null){ 
tv_softkb_point.setClickable(false); 
} 
} 
} 
else { 
if(tv_softkb_point!=null) { 
tv_softkb_point.setClickable(true); 
tv_softkb_point.setEnabled(true); 
} 
} 
if (s.toString().startsWith("0") && s.toString().trim().length() > 1) { //如果以0开头长度大于1 第二个元素不是. 
if (!s.toString().substring(1, 2).equals(".")) { 
buffer.deleteCharAt(1); 
etGoodsPrice.setText(s.subSequence(0, 1)); 
etGoodsPrice.setSelection(1); 
return; 
} 
} 
} 
@Override 
public void beforeTextChanged(CharSequence s, int start, int count, int after) { 
} 
@Override 
public void afterTextChanged(Editable s) { 
int length = etPricetrim.length(); 
if (length >=1) { 
etGoodsPrice.setSelection(length); 
} else { 
etGoodsPrice.setSelection(0); 
} 
// Toast.makeText(PostIssueActivity.this,s.toString(), 0).show(); 
if(s.toString()!=null&&s.toString().contains(".")){ //通过s取出当前editext上的传 来判断是否包含. 如果包含的话则把 clickbale设置成false同时置为不可点击 
if(tv_softkb_point!=null) { 
tv_softkb_point.setClickable(false); 
tv_softkb_point.setEnabled(false); 
} 
} 
// if(s.toString().length()==1&&s.toString().equals("0")) { //如果长度是1 并且是以0开头 这个时候光标是定位在0后 则删除buffer 
// //里的第一个char 并设置 
// 
//// buffer.deleteCharAt(0); 
//// etGoodsPrice.setText(buffer.toString()); 
// 
// } 
} 
}); 
if (!iPriceEdit.getText().toString().substring(1, iPriceEdit.getText().length()).toString().equals("0.00")) { 
etGoodsPrice 
.setText(iPriceEdit.getText().toString().substring(1, iPriceEdit.getText().length()).toString()); 
etGoodsPrice.setSelection(etGoodsPrice.length()); 
buffer.append(iPriceEdit.getText().toString().substring(1, iPriceEdit.getText().length()).toString()); 
} else { 
etGoodsPrice.setText(""); 
etGoodsPrice.setHint("0.00"); 
buffer.append(""); 
} 
etOriginalPrice = (EditText) editPriceView.findViewById(R.id.et_originalPrice); 
etOriginalPrice.addTextChangedListener(new TextWatcher() { 
private String trim; 
@Override 
public void onTextChanged(CharSequence s, int start, int before, int count) { 
trim = s.toString().trim(); 
if (etOriginalPrice.length() == 1 && s.toString().equals(".")) { 
buffer2.append("0").append(".").deleteCharAt(0); 
etOriginalPrice.setText(buffer2.toString()); 
} 
if (s.toString().contains(".")) { 
if (s.length() - 1 - s.toString().indexOf(".") > 2) { 
if(tv_softkb_point!=null){ 
tv_softkb_point.setClickable(false); 
} 
if(buffer2!=null&&buffer2.length()>=1){ 
buffer2.deleteCharAt(buffer2.length()-1); 
} 
s = s.toString().subSequence(0, s.toString().indexOf(".") + 3); 
etOriginalPrice.setText(s); 
etOriginalPrice.setSelection(s.length()); 
} 
else if(s.length()-1-s.toString().indexOf(".")<=2){ 
if(tv_softkb_point!=null){ 
tv_softkb_point.setClickable(false); 
} 
} 
} 
else { 
if(tv_softkb_point!=null) { 
tv_softkb_point.setClickable(true); 
tv_softkb_point.setEnabled(true); 
} 
} 
if (s.toString().startsWith("0") && s.toString().trim().length() > 1) { 
if (!s.toString().substring(1, 2).equals(".")) { 
buffer2.deleteCharAt(1); 
etOriginalPrice.setText(s.subSequence(0, 1)); 
etOriginalPrice.setSelection(1); 
return; 
} 
} 
} 
@Override 
public void beforeTextChanged(CharSequence s, int start, int count, int after) { 
// TODO Auto-generated method stub 
} 
@Override 
public void afterTextChanged(Editable s) { 
// TODO Auto-generated method stub 
int length = trim.length(); 
if (length >=1) { 
etOriginalPrice.setSelection(length); 
} else { 
etOriginalPrice.setSelection(0); 
} 
if(s.toString()!=null&&s.toString().contains(".")){ 
if(tv_softkb_point!=null) { 
tv_softkb_point.setClickable(false); 
tv_softkb_point.setEnabled(false); 
} 
} 
// if(s.toString().length()==1&&s.toString().equals("0")) { //如果长度是1 并且是以0开头 这个时候光标是定位在0后 则删除buffer 
// //里的第一个char 并设置 
// 
// buffer2.deleteCharAt(0); 
// etOriginalPrice.setText(buffer2.toString()); 
// 
// } 
} 
}); 
etOriginalPrice.setSelection(etOriginalPrice.length()); 
if (!iOldPriceEdit.getText().subSequence(1, iOldPriceEdit.getText().length()).toString().equals("0.00")) { 
etOriginalPrice 
.setText(iOldPriceEdit.getText().subSequence(1, iOldPriceEdit.getText().length()).toString()); 
buffer2.append(iOldPriceEdit.getText().subSequence(1, iOldPriceEdit.getText().length()).toString()); 
} else { 
etOriginalPrice.setText(""); 
etOriginalPrice.setHint("0.00"); 
buffer2.append(""); 
} 
// etOriginalPrice.requestFocus(); 
// etOriginalPrice.setSelection(etGoodsPrice.getText().length()); 
ImageView tv_softkb_hiddenkb = (ImageView) editPriceView.findViewById(R.id.tv_softkb_hiddenkb); 
ImageView ivDelete = (ImageView) editPriceView.findViewById(R.id.iv_delete); 
ivDelete.setOnClickListener(new OnClickListener() { 
@Override 
public void onClick(View v) { 
// Toast.makeText(PostIssueActivity.this, "dddd", 0).show(); 
delNumber(); 
} 
}); 
// 长按删除键 事件 
ivDelete.setOnLongClickListener(new OnLongClickListener() { 
@Override 
public boolean onLongClick(View v) { 
if (etGoodsPrice.hasFocus()) { 
buffer.delete(0, etGoodsPrice.getText().length()); 
// if(etGoodsPrice.getText().length()>=1){ 
// buffer.deleteCharAt(etGoodsPrice.getText().length()-1); 
// } 
stringbuffer = buffer.toString(); 
etGoodsPrice.setText(stringbuffer.substring(0, stringbuffer.length())); 
} 
if (etOriginalPrice.hasFocus()) { 
buffer2.delete(0, etOriginalPrice.getText().length()); 
// if(etOriginalPrice.getText().length()>=1){ 
// buffer2.deleteCharAt(etOriginalPrice.getText().length()-1); 
// } 
stringbuffer2 = buffer2.toString(); 
etOriginalPrice.setText(stringbuffer2.substring(0, stringbuffer2.length())); 
} 
return false; 
} 
}); 
setNumber(buffer, buffer2, etGoodsPrice, etOriginalPrice); 
tv_softkb_hiddenkb.setOnClickListener(new OnClickListener() { 
@Override 
public void onClick(View v) { 
editPriceView.clearAnimation(); 
priceDialog.cancel(); 
} 
}); 
TextView confirmKB = (TextView) editPriceView.findViewById(R.id.tv_confirmKB); 
hiddenSoftKB(etGoodsPrice, etOriginalPrice); 
confirmKB.setOnClickListener(new OnClickListener() { 
@SuppressLint("NewApi") 
@Override 
public void onClick(View v) { 
// iPriceEdit 
// iOldPriceEdit 
if (buffer != null) { 
// iPriceEdit.setText("¥"+buffer.toString()); 
iPriceEdit.setText("¥" + etGoodsPrice.getText().toString()); 
if (etGoodsPrice.getText().toString().equals("")) { 
iPriceEdit.setText("¥" + "0.00"); 
} 
} 
if (buffer2 != null) { 
// iOldPriceEdit.setText("¥"+buffer2.toString()); 
iOldPriceEdit.setText("¥" + etOriginalPrice.getText().toString()); 
if (etOriginalPrice.getText().toString().equals("")) { 
iOldPriceEdit.setText("¥" + "0.00"); 
} 
} 
if(etGoodsPrice.getText().toString().equals("")){ 
etGoodsPrice.setText("0.00"); 
} 
if(etOriginalPrice.getText().toString().equals("")){ 
etOriginalPrice.setText("0.00"); 
} 
if(Double.valueOf(etGoodsPrice.getText().toString())>Double.valueOf(etOriginalPrice.getText().toString())) { 
// Toast.makeText(PostIssueActivity.this, "售价高于原价,是否确认?", 0).show(); 
PublicDialog PriceIsMaxDialog = PublicDialog.createDialog(PostIssueActivity.this, new DialogBtnClickListener() { 
@Override 
public void onClick(View view) { 
switch (view.getId()) { 
case R.id.public_dialog_cancel: 
editPriceView.clearAnimation(); 
priceDialog.cancel(); 
break; 
case R.id.public_dialog_confirm: 
editPriceView.clearAnimation(); 
priceDialog.cancel(); 
break; 
default: 
break; 
} 
} 
}); 
Window dialogwindow = PriceIsMaxDialog.getWindow(); 
Display display = getWindowManager().getDefaultDisplay(); 
WindowManager.LayoutParams lp = dialogwindow.getAttributes(); 
lp.width = (int) (display.getWidth() * 0.99); 
dialogwindow.setAttributes(lp); 
PriceIsMaxDialog.show(); 
LinearLayout llPublic = (LinearLayout) PriceIsMaxDialog.findViewById(R.id.ll_public_dialog); 
llPublic.setBackground(getResources().getDrawable(R.drawable.public_dialog_botom_corner_shape_wite)); 
PriceIsMaxDialog.onWindowFocusChanged(true); 
// View splitling = PriceIsMaxDialog.findViewById(R.id.v_line); 
// splitling.setBackgroundColor(color.color_eaeaea); 
PriceIsMaxDialog.setLineColor(Color.parseColor("#E9E9EB")); 
PriceIsMaxDialog.setTitle("提示"); 
PriceIsMaxDialog.setContent("出价高于原价,是否确认?"); 
PriceIsMaxDialog.setTitleVisibility(true); 
TextView contentView = PriceIsMaxDialog.getContentView(); 
contentView.setTextSize(16); 
contentView.setGravity(Gravity.CENTER); 
contentView.setPadding(0, 0, 0, 10); 
android.view.ViewGroup.LayoutParams layout = contentView.getLayoutParams(); 
layout.width = lp.MATCH_PARENT; 
layout.height = 120; 
contentView.setLayoutParams(layout); 
TextView publicdialogCancle = (TextView) PriceIsMaxDialog.findViewById(R.id.public_dialog_cancel); 
publicdialogCancle.setText("否"); 
PriceIsMaxDialog.setCancelTextColor(Color.parseColor("#037BFE")); 
// PriceIsMaxDialog.setCancelBackGround(Color.parseColor("#ffffff")); 
// publicdialogCancle.setTextColor(color.app_green); 
TextView publicdialogConfirm = (TextView) PriceIsMaxDialog.findViewById(R.id.public_dialog_confirm); 
publicdialogConfirm.setText("是"); 
PriceIsMaxDialog.setConfirmTextColor(Color.parseColor("#037BFE")); 
// PriceIsMaxDialog.setConfirmBackGround(Color.parseColor("#ffffff")); 
// publicdialogConfirm.setTextColor(color.app_green); 
// publicdialogConfirm.setBackground(getResources().getDrawable(R.drawable.coners_save_bg)); 
PriceIsMaxDialog.findViewById(R.id.vv_hor).setVisibility(View.VISIBLE); 
} 
editPriceView.clearAnimation(); 
priceDialog.cancel(); 
} 
}); 
} 
/** 
* 删除输入的金额 
*/ 
protected void delNumber() { 
if (etGoodsPrice.hasFocus()) { 
// buffer.delete(0, etGoodsPrice.getText().length()); 
if (etGoodsPrice.getText().length() >= 1) { 
buffer.deleteCharAt(etGoodsPrice.getText().length() - 1); 
// buffer.deleteCharAt(etGoodsPrice.getText().length()); 
} 
stringbuffer = buffer.toString(); 
etGoodsPrice.setText(stringbuffer.substring(0, stringbuffer.length())); 
etGoodsPrice.setSelection(etGoodsPrice.length()); 
} 
if (etOriginalPrice.hasFocus()) { 
// buffer2.delete(0,etOriginalPrice.getText().length()); 
if (etOriginalPrice.getText().length() >= 1) { 
buffer2.deleteCharAt(etOriginalPrice.getText().length() - 1); 
// buffer2.deleteCharAt(etOriginalPrice.getText().length()); 
} 
stringbuffer2 = buffer2.toString(); 
etOriginalPrice.setText(stringbuffer2.substring(0, stringbuffer2.length())); 
etOriginalPrice.setSelection(etOriginalPrice.length()); 
} 
} 
/** 
* 输入金额 
* 
* @param buffer 
* @param buffer2 
* @param etGoodsPrice 
* @param etOriginalPrice 
*/ 
private void setNumber(final StringBuffer buffer, final StringBuffer buffer2, final EditText etGoodsPrice, 
final EditText etOriginalPrice) { 
TextView tv_softkb_1 = (TextView) editPriceView.findViewById(R.id.tv_softkb_1); 
tv_softkb_1.setOnClickListener(new OnClickListener() { 
@Override 
public void onClick(View v) { 
if (etGoodsPrice.hasFocus()) { 
buffer.append("1"); 
etGoodsPrice.setText(buffer.toString()); 
} 
if (etOriginalPrice.hasFocus()) { 
buffer2.append("1"); 
etOriginalPrice.setText(buffer2.toString()); 
} 
} 
}); 
TextView tv_softkb_2 = (TextView) editPriceView.findViewById(R.id.tv_softkb_2); 
tv_softkb_2.setOnClickListener(new OnClickListener() { 
@Override 
public void onClick(View v) { 
if (etGoodsPrice.hasFocus()) { 
buffer.append("2"); 
etGoodsPrice.setText(buffer.toString()); 
} 
if (etOriginalPrice.hasFocus()) { 
buffer2.append("2"); 
etOriginalPrice.setText(buffer2.toString()); 
} 
} 
}); 
TextView tv_softkb_3 = (TextView) editPriceView.findViewById(R.id.tv_softkb_3); 
tv_softkb_3.setOnClickListener(new OnClickListener() { 
@Override 
public void onClick(View v) { 
if (etGoodsPrice.hasFocus()) { 
buffer.append("3"); 
etGoodsPrice.setText(buffer.toString()); 
} 
if (etOriginalPrice.hasFocus()) { 
buffer2.append("3"); 
etOriginalPrice.setText(buffer2.toString()); 
} 
} 
}); 
TextView tv_softkb_4 = (TextView) editPriceView.findViewById(R.id.tv_softkb_4); 
tv_softkb_4.setOnClickListener(new OnClickListener() { 
@Override 
public void onClick(View v) { 
if (etGoodsPrice.hasFocus()) { 
buffer.append("4"); 
etGoodsPrice.setText(buffer.toString()); 
} 
if (etOriginalPrice.hasFocus()) { 
buffer2.append("4"); 
etOriginalPrice.setText(buffer2.toString()); 
} 
} 
}); 
TextView tv_softkb_5 = (TextView) editPriceView.findViewById(R.id.tv_softkb_5); 
tv_softkb_5.setOnClickListener(new OnClickListener() { 
@Override 
public void onClick(View v) { 
if (etGoodsPrice.hasFocus()) { 
buffer.append("5"); 
etGoodsPrice.setText(buffer.toString()); 
} 
if (etOriginalPrice.hasFocus()) { 
buffer2.append("5"); 
etOriginalPrice.setText(buffer2.toString()); 
} 
} 
}); 
TextView tv_softkb_6 = (TextView) editPriceView.findViewById(R.id.tv_softkb_6); 
tv_softkb_6.setOnClickListener(new OnClickListener() { 
@Override 
public void onClick(View v) { 
if (etGoodsPrice.hasFocus()) { 
buffer.append("6"); 
etGoodsPrice.setText(buffer.toString()); 
} 
if (etOriginalPrice.hasFocus()) { 
buffer2.append("6"); 
etOriginalPrice.setText(buffer2.toString()); 
} 
} 
}); 
TextView tv_softkb_7 = (TextView) editPriceView.findViewById(R.id.tv_softkb_7); 
tv_softkb_7.setOnClickListener(new OnClickListener() { 
@Override 
public void onClick(View v) { 
if (etGoodsPrice.hasFocus()) { 
buffer.append("7"); 
etGoodsPrice.setText(buffer.toString()); 
} 
if (etOriginalPrice.hasFocus()) { 
buffer2.append("7"); 
etOriginalPrice.setText(buffer2.toString()); 
} 
} 
}); 
TextView tv_softkb_8 = (TextView) editPriceView.findViewById(R.id.tv_softkb_8); 
tv_softkb_8.setOnClickListener(new OnClickListener() { 
@Override 
public void onClick(View v) { 
if (etGoodsPrice.hasFocus()) { 
buffer.append("8"); 
etGoodsPrice.setText(buffer.toString()); 
} 
if (etOriginalPrice.hasFocus()) { 
buffer2.append("8"); 
etOriginalPrice.setText(buffer2.toString()); 
} 
} 
}); 
TextView tv_softkb_9 = (TextView) editPriceView.findViewById(R.id.tv_softkb_9); 
tv_softkb_9.setOnClickListener(new OnClickListener() { 
@Override 
public void onClick(View v) { 
if (etGoodsPrice.hasFocus()) { 
buffer.append("9"); 
etGoodsPrice.setText(buffer.toString()); 
} 
if (etOriginalPrice.hasFocus()) { 
buffer2.append("9"); 
etOriginalPrice.setText(buffer2.toString()); 
} 
} 
}); 
TextView tv_softkb_0 = (TextView) editPriceView.findViewById(R.id.tv_softkb_0); 
tv_softkb_0.setOnClickListener(new OnClickListener() { 
@Override 
public void onClick(View v) { 
if (etGoodsPrice.hasFocus()) { 
buffer.append("0"); 
etGoodsPrice.setText(buffer.toString()); 
} 
if (etOriginalPrice.hasFocus()) { 
buffer2.append("0"); 
etOriginalPrice.setText(buffer2.toString()); 
} 
} 
}); 
tv_softkb_point.setOnClickListener(new OnClickListener() { 
@Override 
public void onClick(View v) { 
if (etGoodsPrice.hasFocus()) { 
buffer.append("."); 
etGoodsPrice.setText(buffer.toString()); 
} 
if (etOriginalPrice.hasFocus()) { 
buffer2.append("."); 
etOriginalPrice.setText(buffer2.toString()); 
} 
} 
}); 
} 
/** 
* 使用edittext获取焦点弹不出软件键盘 
* 
* @param etGoodsPrice 
* @param etOriginalPrice 
*/ 
private void hiddenSoftKB(final EditText etGoodsPrice, final EditText etOriginalPrice) { 
etGoodsPrice.setOnTouchListener(new OnTouchListener() { 
@Override 
public boolean onTouch(View v, MotionEvent event) { 
int inType = etGoodsPrice.getInputType(); // backup the input 
// type 
etGoodsPrice.setInputType(InputType.TYPE_NULL); // disable soft 
// input 
etGoodsPrice.onTouchEvent(event); // call native handler 
etGoodsPrice.setInputType(inType); // restore input type 
etGoodsPrice.setSelection(etGoodsPrice.getText().length()); 
return true; 
} 
}); 
etOriginalPrice.setOnTouchListener(new OnTouchListener() { 
@Override 
public boolean onTouch(View v, MotionEvent event) { 
int inType = etOriginalPrice.getInputType(); // backup the input 
// type 
etOriginalPrice.setInputType(InputType.TYPE_NULL); // disable 
// soft 
// input 
etOriginalPrice.onTouchEvent(event); // call native handler 
etOriginalPrice.setInputType(inType); // restore input type 
etOriginalPrice.setSelection(etOriginalPrice.getText().length()); 
return true; 
} 
}); 
} 
/** 
* 输入原价和价格dialog 
*/ 
private void showMofityPrice() { 
// TODO Auto-generated method stub 
mofityPrice.setOnClickListener(new OnClickListener() { 
@Override 
public void onClick(View v) { 
// iPriceEdit 
// iOldPriceEdit 
// iPriceEdit.setFocusable(true); 
hiddenKeyboard(); 
editPriceDialog(); 
} 
}); 
}

以上所述是小编给大家介绍的Android开发仿咸鱼键盘修改版,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

相关文章

最新评论