Android实现数字键盘效果

先看效果:

Android实现数字键盘效果_第1张图片

1、先自定义个背景图片:

bg_num.xml




    
    
    

2、布局代码:number_bord.xml




    

    

        
            
            
            
            

        

    

3、activity 代码:

package com.example.weiwenyi.androidtest.input_view;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.TextView;

import com.example.weiwenyi.androidtest.R;
import com.example.weiwenyi.androidtest.adapter.TestNumberAdapter;
import java.util.ArrayList;
import java.util.List;

public class TestInputActivity extends AppCompatActivity implements TestNumberAdapter.OnClickItemListener {

    List numList;
    private GridView gridList;
    TextView edit;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.nunber_bord);

        gridList = findViewById(R.id.gridList);
        edit     = findViewById(R.id.edit);

        addData();

        final TestNumberAdapter adapter1 = new TestNumberAdapter(numList,this);
        gridList.setAdapter(adapter1);
        adapter1.setItemListener(this);

        edit.addTextChangedListener(new TextWatch());
//        gridList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView parent, View view, int position, long id) {
//                view.setOnKeyListener(new View.OnKeyListener() {
//                    @Override
//                    public boolean onKey(View v, int keyCode, KeyEvent event) {
//                        switch (event.getAction()){
//                            case MotionEvent.ACTION_POINTER_UP:
//                                Log.d("Testinput","---------->长按了  ");
//                                adapter1.clearBg();
//                                break;
//                        }
//                        return false;
//                    }
//                });
//            }
//        });
    }

    class TextWatch implements TextWatcher {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    }

    private void addData() {
        numList = new ArrayList<>();
        for (int i=0;i<12;i++){
            if (i < 9)
                numList.add((i+1) + "");
            else if (i == 9){
                numList.add("清空");
            }else if (i == 10){
                numList.add("0");
            }else if (i == 11) numList.add("删除");

        }
    }

    @Override
    public void setOnUpItemClick(int position) {
        if (position == 9){
            edit.setText("");
        }else if (position == 11){
            String currentClear = edit.getText().toString().trim();
            Log.i("SecurityCodeView","----------抬起---->  " + currentClear.length());
            String currentContent = currentClear.substring(0,currentClear.length()-1);
            edit.setText(currentContent);
        }else {
            String con = numList.get(position);
            String currentC = edit.getText().toString().trim();
            edit.setText(currentC + con);
        }

    }

    @Override
    public void setOnDownItemClick(int position) {
        String currentC = edit.getText().toString().trim();
    }
}

运行即可看到,

你可能感兴趣的:(Android实现数字键盘效果)