Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android開發仿鹹魚鍵盤DEMO(修改版)

Android開發仿鹹魚鍵盤DEMO(修改版)

編輯:關於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開發仿鹹魚鍵盤修改版,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對本站網站的支持!

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved