Android自定义数字键盘和输入框

版权声明:本文为博主原创文章,未经博主允许不得转载。

前段时间开发过程中公司要求自己写一个自定义数字键盘和输入框的弹框,整理了下。

效果图

第一步:布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <!--设置页面密码弹框-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/drop_background"
        android:orientation="vertical"
        android:paddingLeft="30dp"
        android:paddingRight="30dp"
        android:paddingBottom="30dp">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="20dp">
            <TextView
                android:id="@+id/tv_guide_count"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="5dp"
                android:text="23s"
                android:layout_gravity="left"
                android:layout_centerVertical="true"
                android:textColor="@color/blue"
                android:textSize="25sp" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="5dp"
                android:text="@string/set_password_title"
                android:textColor="@color/black"
                android:textSize="25sp"
                android:layout_centerHorizontal="true"
                android:layout_centerInParent="true"
                />
            <ImageView
                android:id="@+id/iv_guide_count"
                android:layout_width="43dp"
                android:layout_height="40dp"
                android:src="@mipmap/ca"
                android:layout_centerVertical="true"
                android:layout_alignParentEnd="true"/>
        </RelativeLayout>

        <EditText
            android:id="@+id/et_bag_id"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:layout_marginBottom="30dp"
            android:background="@drawable/key_dialog_et_background"
            android:gravity="center"
            android:maxLength="6"
            android:hint="@string/set_password"
            android:inputType="numberPassword"
            android:textSize="40sp"
            android:textColor="@color/blue"
            android:letterSpacing="0.2"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/tv_1"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_margin="5dp"
                android:layout_weight="1"
                android:background="@drawable/orders_line_background"
                android:gravity="center"
                android:text="1"
                android:textColor="@color/blue"
                android:textSize="25dp" />

            <TextView
                android:id="@+id/tv_2"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_margin="5dp"
                android:layout_weight="1"
                android:background="@drawable/orders_line_background"
                android:gravity="center"
                android:text="2"
                android:textColor="@color/blue"
                android:textSize="25dp" />

            <TextView
                android:id="@+id/tv_3"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_margin="5dp"
                android:layout_weight="1"
                android:background="@drawable/orders_line_background"
                android:gravity="center"
                android:text="3"
                android:textColor="@color/blue"
                android:textSize="25dp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/tv_4"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_margin="5dp"
                android:layout_weight="1"
                android:background="@drawable/orders_line_background"
                android:gravity="center"
                android:text="4"
                android:textColor="@color/blue"
                android:textSize="25dp" />

            <TextView
                android:id="@+id/tv_5"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_margin="5dp"
                android:layout_weight="1"
                android:background="@drawable/orders_line_background"
                android:gravity="center"
                android:text="5"
                android:textColor="@color/blue"
                android:textSize="25dp" />

            <TextView
                android:id="@+id/tv_6"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_margin="5dp"
                android:layout_weight="1"
                android:background="@drawable/orders_line_background"
                android:gravity="center"
                android:text="6"
                android:textColor="@color/blue"
                android:textSize="25dp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/tv_7"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_margin="5dp"
                android:layout_weight="1"
                android:background="@drawable/orders_line_background"
                android:gravity="center"
                android:text="7"
                android:textColor="@color/blue"
                android:textSize="25dp" />

            <TextView
                android:id="@+id/tv_8"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_margin="5dp"
                android:layout_weight="1"
                android:background="@drawable/orders_line_background"
                android:gravity="center"
                android:text="8"
                android:textColor="@color/blue"
                android:textSize="25dp" />

            <TextView
                android:id="@+id/tv_9"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_margin="5dp"
                android:layout_weight="1"
                android:background="@drawable/orders_line_background"
                android:gravity="center"
                android:text="9"
                android:textColor="@color/blue"
                android:textSize="25dp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/tv_del"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_margin="5dp"
                android:layout_weight="1"
                android:background="@drawable/orders_line_background"
                android:gravity="center"
                android:text="后退"
                android:textColor="@color/blue"
                android:textSize="25dp" />

            <TextView
                android:id="@+id/tv_0"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_margin="5dp"
                android:layout_weight="1"
                android:background="@drawable/orders_line_background"
                android:gravity="center"
                android:text="0"
                android:textColor="@color/blue"
                android:textSize="25dp" />

            <TextView
                android:id="@+id/tv_confirm"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_margin="5dp"
                android:layout_weight="1"
                android:background="@drawable/btn_drop_orange"
                android:gravity="center"
                android:text="确认"
                android:textColor="@color/white"
                android:textSize="25dp" />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

布局可以根据自己需求更改。

第二步:自定义dialog逻辑

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.fiveportmachine.R;
import com.example.fiveportmachine.timer.CountDownTimerSupport;
import com.example.fiveportmachine.timer.OnCountDownTimerListener;

public class KeyBoardDialog extends Dialog implements View.OnClickListener {

    private EditText et_bag_id;
    private TextView tv_guide_count;
    private KeyBoardDialog keyBoardDialog;
    private long countDownInterval = 1000;
    // 倒计时时间设置
    private static final int secondCountdownToClosing = 60;
    private CountDownTimerSupport mTimer;
    public KeyBoardDialog(@NonNull Context context) {
        super(context);
    }

    public KeyBoardDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    protected KeyBoardDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_key_dialog);
        //按空白处不能取消动画
        setCanceledOnTouchOutside(false);
        //设置dialog样式
        Window window =getWindow();
        window.getDecorView().setPadding(0, 0, 0, 0);
        window.setBackgroundDrawableResource(R.drawable.drop_background);
        intView();
        //setTimer();
    }
/**
    private void setTimer() {
        //总时长 间隔时间
        long millisInFuture = secondCountdownToClosing * 1000L;
        mTimer = new CountDownTimerSupport(millisInFuture, countDownInterval);
        // SimpleOnCountDownTimerListener
        mTimer.setOnCountDownTimerListener(new OnCountDownTimerListener() {
            @Override
            public void onTick(long millisUntilFinished) {
                // 倒计时间隔
                tv_guide_count.setText(millisUntilFinished / 1000 + "s");
            }

            @Override
            public void onFinish() {
                // 倒计时结束
                dismiss();
            }

            @Override
            public void onCancel() {
                // 倒计时手动停止
            }
        });
        mTimer.start();
    }
*/
    private void intView() {
        tv_guide_count = findViewById(R.id.tv_guide_count);
        et_bag_id = findViewById(R.id.et_bag_id);
        et_bag_id.setInputType(InputType.TYPE_NULL);
        findViewById(R.id.tv_0).setOnClickListener(this);
        findViewById(R.id.tv_0).setOnClickListener(this);
        findViewById(R.id.tv_1).setOnClickListener(this);
        findViewById(R.id.tv_2).setOnClickListener(this);
        findViewById(R.id.tv_3).setOnClickListener(this);
        findViewById(R.id.tv_4).setOnClickListener(this);
        findViewById(R.id.tv_5).setOnClickListener(this);
        findViewById(R.id.tv_6).setOnClickListener(this);
        findViewById(R.id.tv_7).setOnClickListener(this);
        findViewById(R.id.tv_8).setOnClickListener(this);
        findViewById(R.id.tv_9).setOnClickListener(this);
        findViewById(R.id.tv_del).setOnClickListener(this);
        findViewById(R.id.tv_confirm).setOnClickListener(this);
        findViewById(R.id.iv_guide_count).setOnClickListener(this);
    }

    /**
     * 初始化界面的确定和取消监听器
     */
    private void initEvent() {
        //设置确定按钮被点击后,向外界提供监听
    }
    /**
     * 设置确定取消按钮的回调
     */
    public KeyDialogOnClickListener keyDialogOnClickListener;
    public KeyBoardDialog setOnClickKeyDialogListener(KeyDialogOnClickListener keyDialogOnClickListener) {
        this.keyDialogOnClickListener = keyDialogOnClickListener;
        return this;
    }

    @Override
    public void onClick(View view) {
        if (view.getId()==R.id.tv_0){
            et_bag_id.getText().append("0");
            //将光标设置到尾部
            et_bag_id.setSelection(et_bag_id.getText().length());
        }else if (view.getId()==R.id.tv_1){
            et_bag_id.getText().append("1");
            //将光标设置到尾部
            et_bag_id.setSelection(et_bag_id.getText().length());
        }else if (view.getId()==R.id.tv_2){
            et_bag_id.getText().append("2");
            //将光标设置到尾部
            et_bag_id.setSelection(et_bag_id.getText().length());
        }else if (view.getId()==R.id.tv_3){
            et_bag_id.getText().append("3");
            //将光标设置到尾部
            et_bag_id.setSelection(et_bag_id.getText().length());
        }else if (view.getId()==R.id.tv_4){
            et_bag_id.getText().append("4");
            //将光标设置到尾部
            et_bag_id.setSelection(et_bag_id.getText().length());
        }else if (view.getId()==R.id.tv_5){
            et_bag_id.getText().append("5");
            //将光标设置到尾部
            et_bag_id.setSelection(et_bag_id.getText().length());
        }else if (view.getId()==R.id.tv_6){
            et_bag_id.getText().append("6");
            //将光标设置到尾部
            et_bag_id.setSelection(et_bag_id.getText().length());
        }else if (view.getId()==R.id.tv_7){
            et_bag_id.getText().append("7");
            //将光标设置到尾部
            et_bag_id.setSelection(et_bag_id.getText().length());
        }else if (view.getId()==R.id.tv_8){
            et_bag_id.getText().append("8");
            //将光标设置到尾部
            et_bag_id.setSelection(et_bag_id.getText().length());
        }else if (view.getId()==R.id.tv_9){
            et_bag_id.getText().append("9");
            //将光标设置到尾部
            et_bag_id.setSelection(et_bag_id.getText().length());
        }else if (view.getId()==R.id.tv_del){
            if (et_bag_id.getText().length()!=0){
                et_bag_id.getText().delete(et_bag_id.getText().length()-1,et_bag_id.getText().length());
            }
        }else if (view.getId()==R.id.tv_confirm){
            keyDialogOnClickListener.onPositiveClick(et_bag_id.getText().toString());
            et_bag_id.getText().delete(0,et_bag_id.getText().length());
        } else if (view.getId()==R.id.iv_guide_count) {
            keyDialogOnClickListener.onQuitClick();
            dismiss();
        }
    }
    public interface KeyDialogOnClickListener{
        /**
         * 点击确定按钮事件
         */
        public void onPositiveClick(String password);
        /**
         * 点击取消按钮事件
         */
        public void onQuitClick();
    }
}

第三步:activity中应用

//设置页面密码输入弹框
KeyBoardDialog keyBoardDialog=new KeyBoardDialog(MainActivity.this);
keyBoardDialog.setOnClickKeyDialogListener(new KeyBoardDialog.KeyDialogOnClickListener() {
    @Override
    public void onPositiveClick(String password) {
        //确认按钮回调
        if (password.equals("666666")){
            Toast.makeText(MainActivity.this,"登录成功",Toast.LENGTH_SHORT).show();
            keyBoardDialog.dismiss();
            startActivity(new Intent(MainActivity.this,SetUpActivity.class));
        }else{
            Toast.makeText(MainActivity.this,"密码有误,请重新输入",Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onQuitClick() {
        //取消按钮回调
        Toast.makeText(MainActivity.this,"ssss",Toast.LENGTH_SHORT).show();
    }
}).show();

注意:

dialog边框问题,根据自己需求调整。如果需要自己定义修改背景边框,dialog自带边框需要去除掉。

倒计时已去掉,需要请私聊。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值