摘要:效果圖源碼地址最近開發項目需要自定義收銀鍵盤,網上查了查都感覺不是太好,于是自己寫了一個,自定義的鍵盤主要是根據結合來寫的如下然后自己寫了一個,設置了鍵盤的文字和類型。
效果圖:
[(
1)]
源碼地址(github)
最近開發項目需要自定義收銀鍵盤,網上查了查都感覺不是太好,于是自己寫了一個,自定義的鍵盤主要是根據GridView+EditText結合來寫的.
1.GridView如下:
2.然后自己寫了一個PayKeyAdapter,設置了鍵盤的文字和類型。
package com.example.qzs.keyboard_cash; import android.content.Context; import android.graphics.Color; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; public class PayKeyAdapter extends BaseAdapter { private Context mContext; private Context context; public PayKeyAdapter(Context context) { this.context=context; } private String[] texts = { //九宮格圖片下方文字的設置 "1", "2", "3", "清空", "4", "5", "6", "←", "7", "8", "9", "=", ".", "0", "+", "收款", }; private String[] types = { //存放在text的tag中 "num", "num", "num", "cancel", "num", "num", "num", "back", "num", "num", "num", "equal", "point", "num", "plus", "pay", }; @Override public int getCount() { return texts.length; } @Override public Object getItem(int position) { return position; } @Override public long getItemId(int position) { return position; } @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(3, 3, 3, 3); //每格的間距 } else { wrapper = (ImgTextWrapper)view.getTag(); } wrapper.textView = (TextView)view.findViewById(R.id.GridTextView); wrapper.textView.setText(texts[position]); wrapper.textView.setTag(types[position]); if(types[position].equals("back") || types[position].equals("cancel") || types[position].equals("equal")){ wrapper.textView.setBackgroundResource(R.drawable.btn_cell_delete); }else if(types[position].equals("pay")){ wrapper.textView.setBackgroundResource(R.drawable.btn_cell_pay); wrapper.textView.setTextColor(Color.WHITE); } // wrapper.textView = (TextView)view.findViewById(R.id.GridItemText); // wrapper.textView.setText(texts[position]); // wrapper.textView.setTag(payTypes[position]); return view; } class ImgTextWrapper { ImageView imageView; TextView textView; } }
3.核心的語法在GridView的setOnItemClickListener事件.
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(".")=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 (); 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 (); 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 (); numList.add("0."); editMemberAmount.setText("0."); } } if(keyType.equals("plus")){ //加 try{ if(editText.lastIndexOf("+") == editText.length()-1){ //最后為加則不處理 }else{ numIndex++; numList.add(""); editMemberAmount.setText(editText+textValue); } }catch(Exception e){ numIndex=0; numList=new ArrayList (); numList.add("0"); editMemberAmount.setText(""); } } if(keyType.equals("cancel")){ //清空 //確認是否清空 AlertDialog.Builder clearBuilder = new AlertDialog.Builder(MainActivity.this);// 添加按鈕的單擊事件 clearBuilder.setMessage("是否確認要清空當前收款數據?") . // 設置確定按鈕 setPositiveButton("確定", new DialogInterface.OnClickListener() { // 單擊事件 public void onClick( DialogInterface dialog, int which) { // 設置TextView文本 clearInputData(); } }) . // 設置取消按鈕 setNegativeButton("取消", new DialogInterface.OnClickListener() { public void onClick( DialogInterface dialog, int which) { } }); // 創建對話框 AlertDialog ad = clearBuilder.create(); // 顯示對話框 ad.show(); } if(keyType.equals("equal")||keyType.equals("pay")){ //等于 try{ double sum = 0.0; for(String value :numList){ if(!value.equals("")){ sum+= Double.parseDouble(value)+0.00d; } } String sumStr=new DecimalFormat("##.##").format(sum); if(!sumStr.contains(".")){ sumStr+=".00"; } if(sumStr.substring(sumStr.lastIndexOf(".")+1).length()<2){ sumStr+="0"; } numIndex=0; numList=new ArrayList (); numList.add(sumStr); editMemberAmount.setText(sumStr); }catch(Exception e){ numIndex=0; numList=new ArrayList (); numList.add("0"); editMemberAmount.setText(""); } } if(keyType.equals("pay")){ //收款 if (editMemberAmount.getText().toString().length() < 1||editMemberAmount.getText().toString().equals("0.00") || editMemberAmount.getText().toString().equals("0")) { Toast.makeText(getApplicationContext(),"請輸入金額",Toast.LENGTH_SHORT).show(); return; }else { Toast.makeText(getApplicationContext(),"收款成功",Toast.LENGTH_SHORT).show(); } } } });
4.具體的完整項目請訪問我的github,地址在上方已貼出.
我的公眾號如下:
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/70221.html
摘要:首先先祝大家國慶節快樂今天距離為我運營公眾號已經一個月了,今天把文章整合一下,希望對大家有幫助,也謝謝朋友的支持,我會繼續堅持原創,寫更好的文章給大家一視頻獲取學習資源分享合集二功能篇實現金額的語音播報功能基于模式風格的封裝之路炫酷動畫跳 showImg(https://segmentfault.com/img/remote/1460000011437678?w=900&h=500);...
摘要:首先先祝大家國慶節快樂今天距離為我運營公眾號已經一個月了,今天把文章整合一下,希望對大家有幫助,也謝謝朋友的支持,我會繼續堅持原創,寫更好的文章給大家一視頻獲取學習資源分享合集二功能篇實現金額的語音播報功能基于模式風格的封裝之路炫酷動畫跳 showImg(https://segmentfault.com/img/remote/1460000011437678?w=900&h=500);...
閱讀 1784·2023-04-25 15:51
閱讀 2497·2021-10-13 09:40
閱讀 2134·2021-09-23 11:22
閱讀 3244·2019-08-30 14:16
閱讀 2652·2019-08-26 13:35
閱讀 1847·2019-08-26 13:31
閱讀 874·2019-08-26 11:39
閱讀 2732·2019-08-26 10:33