欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2024-03-02 15:46:34
在这里布局我就不贴出来了 /** * 最终被调用的修改价格dialog */ protected void editpricedialog()...

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

/** 
* 最终被调用的修改价格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开发仿咸鱼键盘修改版,希望对大家有所帮助