android 自定义数字键盘

深渊向深渊呼唤

Recyclerview自定义数字键盘

提示:这里使用recyclerview进行自定义数字键盘


文章目录

Recyclerview自定义数字键盘 一、自定义数字键盘效果 二、自定义步骤 1.写adapter 2.activity使用 3.自定义的样式 总结 (需要源码的下面有)


提示:以下是本篇文章正文内容,下面案例可供参考

一、自定义数字键盘效果

自定义数字键盘

示例:pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的。

二、自定义步骤

1.写adapter

xml代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:andro
    android:layout_
    android:layout_
    android:orientation="vertical">

    <TextView
        android:
        android:layout_
        android:layout_
        android:layout_marginTop="5dp"
        android:gravity="center"
        android:textColor="@color/white"
        android:textSize="25sp"
        android:textStyle="bold"></TextView>

</LinearLayout>

adapter代码如下:

package com.example.demo1.adapter;

import android.content.Context;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.demo1.R;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : Code23
 * @time : 2021/1/13
 * @module : KeyBoardAdapter
 * @describe :
 */
public class KeyBoardAdapter extends RecyclerView.Adapter {
    private Context context;
    private List<String> stringList = new ArrayList<>();
    private int width;//屏幕宽度

    public KeyBoardAdapter(Context context, List<String> stringList, int width) {
        this.context = context;
        this.stringList = stringList;
        this.width = width;
    }

    class KeyBoardHolder extends RecyclerView.ViewHolder {
        private TextView keyboardKey;

        public KeyBoardHolder(@NonNull View itemView) {
            super(itemView);
            keyboardKey = (TextView) itemView.findViewById(R.id.keyboard_key);
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.adapter_keyboard, null, false);
        KeyBoardHolder keyBoardHolder = new KeyBoardHolder(view);
        return keyBoardHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        final KeyBoardHolder keyBoardHolder = (KeyBoardHolder) holder;
        keyBoardHolder.keyboardKey.setText(stringList.get(position));

        ViewGroup.LayoutParams layoutParams = keyBoardHolder.keyboardKey.getLayoutParams();
        if (position != 10) {//数字键盘宽度和背景设置
            keyBoardHolder.keyboardKey.setBackgroundResource(R.drawable.btn_gray_bg);
            layoutParams.width = ((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, width / 3, context.getResources().getDisplayMetrics()));
        } else {//确定键盘宽度和背景设置
            keyBoardHolder.keyboardKey.setBackgroundResource(R.drawable.btn_blue_bg);
            layoutParams.width = ((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, width - (width / 3), context.getResources().getDisplayMetrics()));
        }
        layoutParams.height = ((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, width / 3, context.getResources().getDisplayMetrics()));
        keyBoardHolder.keyboardKey.setLayoutParams(layoutParams);

        /**
         * 键盘item点击
         */
        keyBoardHolder.keyboardKey.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (position != 10) {//数字键盘监听
                    onClickKeyBoardListener.OnClickKeyBoard(stringList.get(position));
                } else {//确认键盘监听
                    onClickKeyBoardListener.OnClickSure();
                }
            }
        });

        /**
         * 键盘item触目监听
         */
        keyBoardHolder.keyboardKey.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                switch (motionEvent.getAction()) {
                    case MotionEvent.ACTION_DOWN://按下
                        if (position != 10) {
                            keyBoardHolder.keyboardKey.setBackgroundResource(R.drawable.btn_blue_bg);
                        }
                        break;
                    case MotionEvent.ACTION_UP://取消or离开
                    case MotionEvent.ACTION_CANCEL:
                        if (position != 10) {
                            keyBoardHolder.keyboardKey.setBackgroundResource(R.drawable.btn_gray_bg);
                        }
                        break;
                }
                return false;
            }
        });
    }

    @Override
    public int getItemCount() {
        return stringList != null ? stringList.size() : 0;
    }

    public void setOnClickKeyBoardListener(OnClickKeyBoardListener onClickKeyBoardListener) {
        this.onClickKeyBoardListener = onClickKeyBoardListener;
    }

    private OnClickKeyBoardListener onClickKeyBoardListener;

    public interface OnClickKeyBoardListener {
        void OnClickKeyBoard(String key);

        void OnClickSure();
    }
}

2.activity使用

xml代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:andro
    android:layout_
    android:layout_
    android:background="@color/black"
    android:orientation="vertical"
    android:overScrollMode="never">

    <LinearLayout
        android:layout_
        android:layout_
        android:layout_margin="20dp"
        android:gravity="center_vertical"
        android:orientation="horizontal">

        <TextView
            android:
            android:layout_
            android:layout_
            android:layout_weight="1"
            android:gravity="center_vertical"
            android:hint="请输入数字"
            android:maxLength="8"
            android:textColor="@color/white"
            android:textColorHint="@color/white"
            android:textSize="25sp"></TextView>

        <ImageView
            android:
            android:layout_
            android:layout_
            android:scaleType="fitXY"
            android:src="@mipmap/icon_clear"></ImageView>
    </LinearLayout>

    <androidx.recyclerview.widget.RecyclerView
        android:
        android:layout_
        android:layout_
        android:layout_weight="1"
        android:overScrollMode="never"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        android:scrollbars="none"></androidx.recyclerview.widget.RecyclerView>
</LinearLayout>

activity代码如下:

package com.example.demo1.activity;

import android.content.res.Resources;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.demo1.R;
import com.example.demo1.adapter.KeyBoardAdapter;
import com.example.demo1.utils.Tools;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : Code23
 * @time : 2021/1/13
 * @module : NumericKeyboardActivity
 * @describe :数字键盘界面
 */
public class NumericKeyboardActivity extends AppCompatActivity {
    private TextView numerickeyboardInputtext;
    private ImageView numerickeyboardClear;
    private RecyclerView numerickeyboardKeyboardlist;

    private int width;//屏幕宽度
    private KeyBoardAdapter keyBoardAdapter;
    private List<String> stringList = new ArrayList<>();

    private String inputnumber = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_numerickeyboard);
        initView();
    }

    private void initView() {
        //获取屏幕宽度
        Resources resources = this.getResources();
        DisplayMetrics displayMetrics = resources.getDisplayMetrics();
        width = Tools.px2dip(this, displayMetrics.widthPixels) - 55;

        numerickeyboardInputtext = (TextView) findViewById(R.id.numerickeyboard_inputtext);
        numerickeyboardClear = (ImageView) findViewById(R.id.numerickeyboard_clear);
        numerickeyboardKeyboardlist = (RecyclerView) findViewById(R.id.numerickeyboard_keyboardlist);

        //获取资源文件数据
        String[] keyboards = getResources().getStringArray(R.array.keyboard);
        for (String key : keyboards) {
            stringList.add(key);
        }

        //数字键盘列表适配器实例化
        keyBoardAdapter = new KeyBoardAdapter(this,stringList, width);
        final GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 3);
        gridLayoutManager.setOrientation(RecyclerView.VERTICAL);
        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {//控制行数
            @Override
            public int getSpanSize(int position) {//最后一行占两位置,其余占一个位置
                return position != 10 ? 1 : 2;
            }
        });
        numerickeyboardKeyboardlist.setLayoutManager(gridLayoutManager);
        numerickeyboardKeyboardlist.setAdapter(keyBoardAdapter);

        /**
         * 数字键盘列表item时间监听
         */
        keyBoardAdapter.setOnClickKeyBoardListener(new KeyBoardAdapter.OnClickKeyBoardListener() {
            @Override
            public void OnClickKeyBoard(String key) {//数字按钮
                int lens = (inputnumber + key).length();
                if (lens < 9) {
                    inputnumber = inputnumber + key;
                    numerickeyboardInputtext.setText(inputnumber);
                }
            }

            @Override
            public void OnClickSure() {//确定按钮
                Toast.makeText(NumericKeyboardActivity.this, inputnumber, Toast.LENGTH_SHORT).show();
            }
        });

        /**
         * 清除
         */
        numerickeyboardClear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                inputnumber = "";
                numerickeyboardInputtext.setText(inputnumber);
            }
        });
    }
}

3.自定义的样式

1、btn_blue_bg

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:andro>
    <corners android:radius="15dp"></corners>
    <solid android:color="#169BD5"></solid>
</shape>

2、btn_gray_bg

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:andro>
    <corners android:radius="15dp"></corners>
    <solid android:color="#999999"></solid>
</shape>

3、keyboard

<string-array name="keyboard">
        <item>0</item>
        <item>1</item>
        <item>2</item>
        <item>3</item>
        <item>4</item>
        <item>5</item>
        <item>6</item>
        <item>7</item>
        <item>8</item>
        <item>9</item>
        <item>确定</item>
    </string-array>

总结 (需要源码的下面有)

提示:源码自定义数字键盘

栏目