Android custom numeric keyboard

前端 未结 3 1773
渐次进展
渐次进展 2020-12-02 17:26

I want to add numeric keyboard like the one in vault application

\"enter

3条回答
  •  攒了一身酷
    2020-12-02 18:23

    1. Use TableLayout to create the numeric keyboard layout.
    2. Bind View.OnClickListener on each custom key view to response user input.
    3. In responses, append or delete text to that password field which implements by EditText.You can use append() or setText() to control what will be filled in the password field.

    I write a custom view for reusing in anywhere, here is the code:

    KeyboardView.java

    public class KeyboardView extends FrameLayout implements View.OnClickListener {
    
        private EditText mPasswordField;
    
        public KeyboardView(Context context) {
            super(context);
            init();
        }
    
        public KeyboardView(Context context, AttributeSet attrs) {
            super(context, attrs);
            init();
        }
    
        public KeyboardView(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            init();
        }
    
        private void init() {
            inflate(getContext(), R.layout.keyboard, this);
            initViews();
        }
    
        private void initViews() {
            mPasswordField = $(R.id.password_field);
            $(R.id.t9_key_0).setOnClickListener(this);
            $(R.id.t9_key_1).setOnClickListener(this);
            $(R.id.t9_key_2).setOnClickListener(this);
            $(R.id.t9_key_3).setOnClickListener(this);
            $(R.id.t9_key_4).setOnClickListener(this);
            $(R.id.t9_key_5).setOnClickListener(this);
            $(R.id.t9_key_6).setOnClickListener(this);
            $(R.id.t9_key_7).setOnClickListener(this);
            $(R.id.t9_key_8).setOnClickListener(this);
            $(R.id.t9_key_9).setOnClickListener(this);
            $(R.id.t9_key_clear).setOnClickListener(this);
            $(R.id.t9_key_backspace).setOnClickListener(this);
        }
    
        @Override
        public void onClick(View v) {
            // handle number button click
            if (v.getTag() != null && "number_button".equals(v.getTag())) {
                mPasswordField.append(((TextView) v).getText());
                return;
            }
            switch (v.getId()) {
                case R.id.t9_key_clear: { // handle clear button
                    mPasswordField.setText(null);
                }
                break;
                case R.id.t9_key_backspace: { // handle backspace button
                    // delete one character
                    Editable editable = mPasswordField.getText();
                    int charCount = editable.length();
                    if (charCount > 0) {
                        editable.delete(charCount - 1, charCount);
                    }
                }
                break;
            }
        }
    
        public String getInputText() {
            return mPasswordField.getText().toString();
        }
    
        protected  T $(@IdRes int id) {
            return (T) super.findViewById(id);
        }
    }
    

    layout keyboard.xml

    
    
        
    
        
    
            
    
                
    
                
    
                
            
    
            
    
                
    
                
    
                
            
    
            
    
                
    
                
    
                
            
    
            
    
                
    
                
    
                
            
        
    
    

    style.xml

    
    
    
    
    
    

    drawable keyboard_button_bg.xml

    
    
        
            
                
            
        
        
            
                
            
        
     
    

    drawable keyboard_divider.xml

    
    
        
        
    
    

    strings.xml

    0
    1
    2
    3
    4
    5
    6
    7
    8
    9
    Clear
    Back
    

    Use the custom KeyboardView in your layout :

    
    

    Old answer:

    The keyboard layout code looks like this:

    
    
    
    
    
    
        
    
        
    
        
    
    
    
    
        
    
        
    
        
    
    
    
    
        
    
        
    
        
    
    
    
    
        
    
        
    
        
    
    
    
    

    Each key style:

    
    

    Responses of each key:

    private EditText mEtPassword ;
    
    private void setViews(){
        // find view references...
        // set OnClickListener to each key view...
    }
    
    private void onT9KeyClicked(int key) {
        switch (key) {
        case R.id.anti_theft_t9_key_0:
            mEtPassword.append("0");
            break;
        case R.id.anti_theft_t9_key_1:
            mEtPassword.append("1");
            break;
        case R.id.anti_theft_t9_key_2:
            mEtPassword.append("2");
            break;
        case R.id.anti_theft_t9_key_3:
            mEtPassword.append("3");
            break;
        case R.id.anti_theft_t9_key_4:
            mEtPassword.append("4");
            break;
        case R.id.anti_theft_t9_key_5:
            mEtPassword.append("5");
            break;
        case R.id.anti_theft_t9_key_6:
            mEtPassword.append("6");
            break;
        case R.id.anti_theft_t9_key_7:
            mEtPassword.append("7");
            break;
        case R.id.anti_theft_t9_key_8:
            mEtPassword.append("8");
            break;
        case R.id.anti_theft_t9_key_9:
            mEtPassword.append("9");
            break;
        case R.id.anti_theft_t9_key_backspace: {
            // delete one character
            String passwordStr = mEtPassword.getText().toString();
            if (passwordStr.length() > 0) {
                String newPasswordStr = new StringBuilder(passwordStr)
                        .deleteCharAt(passwordStr.length() - 1).toString();
                mEtPassword.setText(newPasswordStr);
            }
        }
            break;
        case R.id.anti_theft_t9_key_clear:
            // clear password field
            mEtPassword.setText(null);
            break;
        }
    }
    

提交回复
热议问题