Android自定義收銀鍵盤

秦子帥發表於2017-09-21

推薦閱讀:
Android學習資源分享合集(1)-視訊資源

效果圖:

原始碼地址(github)

最近寫了一個收銀鍵盤,鍵盤主要是根據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>複製程式碼

2.然後自己寫了一個PayKeyAdapter,設定了鍵盤的文字和型別。

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",  
       "num",  
       "num",  
       "num",  
       "num",  
       "num",  
       "num",  
       "point",  
       "num",
     "back",
     };
     @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(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]);
      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 iv_back;
         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(".")<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.");
                    }
                }

            }
        });複製程式碼

4.具體的完整專案請訪問我的github,地址在上方已貼出.
我的公眾號如下,希望大家多多支援:

相關文章