欢迎大家来到IT世界,在知识的湖畔探索吧!
置顶文章有IT资源合集!
实现步骤:
自定义的键盘主要是根据GridView+EditText结合来写的。
1.首先看下GridView的布局:
<GridView android:horizontalSpacing="2dp" android:verticalSpacing="2dp" android:inputType="numberDecimal" android:editable="false" android:layout_width="match_parent" android:layout_height="match_parent" android:numColumns="3" android:id="@+id/gridview"></GridView>
欢迎大家来到IT世界,在知识的湖畔探索吧!
2.PayKeyAdapter 适配器里设置键盘12个按键的文字和类型
欢迎大家来到IT世界,在知识的湖畔探索吧!private String[] texts = { //九宫格图片下方文字的设置 "1", "2", "3", "4", "5", "6", "7", "8", "9", ".", "0", "✖" }; private String[] types = { //存放在text的tag中 "num", "num", "num", "num", "num", "num", "num", "num", "num", "point", "num", "back", };
3.然后再适配器的getView方法里给键盘文字设置TAG,下面是getView方法
@Override public View getView(int position, View view, ViewGroup viewgroup) { ImgTextWrapper wrapper; if(view==null) { wrapper = new ImgTextWrapper(); LayoutInflater inflater = LayoutInflater.from(context); view = inflater.inflate(R.layout.grid_menu_item_pay, null); view.setTag(wrapper); view.setPadding(26, 26, 26, 26); //每格的间距 } else { wrapper = (ImgTextWrapper)view.getTag(); } wrapper.textView = (TextView)view.findViewById(R.id.GridTextView); wrapper.iv_back= (ImageView) view.findViewById(R.id.iv_back); if (position<11){ wrapper.textView.setVisibility(View.VISIBLE); wrapper.iv_back.setVisibility(View.GONE); wrapper.textView.setText(texts[position]); }else { wrapper.textView.setVisibility(View.GONE); wrapper.iv_back.setVisibility(View.VISIBLE); } wrapper.textView.setTag(types[position]); return view; } class ImgTextWrapper { ImageView iv_back; TextView textView; }
4.核心的语法在GridView的setOnItemClickListener事件,定义String类型的KeyType来获取TAG。
欢迎大家来到IT世界,在知识的湖畔探索吧!gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { String keyType = view.findViewById(R.id.GridTextView).getTag().toString(); String textValue = ((TextView)view.findViewById(R.id.GridTextView)).getText().toString(); String editText = editMemberAmount.getText().toString(); if(keyType.equals("num")){ //数字键 try{ String orgValue = numList.get(numIndex); if(editText.equals("0")){ editMemberAmount.setText(textValue); }else{ //小数点最多只能后两位 if(orgValue.equals("0.00")){ orgValue=textValue; numList.set(numIndex, textValue); editMemberAmount.setText(textValue); return; } if(orgValue.lastIndexOf(".")!=-1 && orgValue.lastIndexOf(".")<orgValue.length()-2){ Toast.makeText(getApplicationContext(),"只能输入小数点后两位",Toast.LENGTH_SHORT).show(); }else{ if(editText.length()>=18){ Toast.makeText(getApplicationContext(),"一次输入算式无法超过18个字符",Toast.LENGTH_SHORT).show(); return; } int numLen = orgValue.length(); if(orgValue.lastIndexOf(".")!=-1){ numLen = orgValue.lastIndexOf(".")+1; } if(numLen==7){ Toast.makeText(getApplicationContext(),"收款金额不能超过7位数",Toast.LENGTH_SHORT).show(); }else{ numList.set(numIndex, orgValue+textValue); editMemberAmount.setText(editText+textValue); } } } }catch(Exception e){ numIndex=0; numList=new ArrayList<String>(); numList.add(textValue); editMemberAmount.setText(textValue); } } if(keyType.equals("back")){ //退格 try{ if(editText.lastIndexOf("+") == editText.length()-1){ //最后为加则减少一位 numList.set(numIndex,""); numIndex--; editMemberAmount.setText(editText.substring(0,editText.length()-1)); }else{ String orgValue = numList.get(numIndex); if(!orgValue.equals("")){ numList.set(numIndex, orgValue.substring(0,orgValue.length()-1)); } if(editText.equals("0")||editText.length()==1){ editMemberAmount.setText("0.00"); numList.set(numIndex,"0.00"); }else{ editMemberAmount.setText(editText.substring(0,editText.length()-1)); } } }catch(Exception e){ numIndex=0; numList=new ArrayList<String>(); numList.add("0"); editMemberAmount.setText(""); } } if(keyType.equals("point")){ //小数点 try{ String orgValue = numList.get(numIndex); if(orgValue.lastIndexOf(".")!=-1){ Toast.makeText(getApplicationContext(),"已存在小数点",Toast.LENGTH_SHORT).show(); }else{ if(editText.lastIndexOf("+") == editText.length()-1){ //最后为加则补0 numList.set(numIndex, orgValue+"0"+textValue); editMemberAmount.setText(editText+"0"+textValue); }else{ numList.set(numIndex, orgValue+textValue); editMemberAmount.setText(editText+textValue); } } }catch(Exception e){ numIndex=0; numList=new ArrayList<String>(); numList.add("0."); editMemberAmount.setText("0."); } } } } });
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://itzsg.com/18016.html