android自定义键盘弹窗

news2024/9/22 19:25:09

样式布局

要在Android中自定义键盘弹窗,先要创建一个新的XML布局文件,用于定义键盘弹窗的外观和布局。例如,创建一个名为key_alert_dialog.xml的文件,并在其中添加所需的按钮和其他UI元素。

<?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="wrap_content"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/shape_alert_dialog"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="88dp"
            android:orientation="horizontal">

            <TextView
                style="@style/key_board_title"
                android:text="请输入管理员密码" />

        </LinearLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="#BBBBBB" />

        <LinearLayout
            android:paddingTop="20dp"
            android:paddingStart="48dp"
            android:paddingEnd="48dp"
            android:layout_width="fill_parent"
            android:layout_height="84dp"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/tv_pay1"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:background="@drawable/key_password"
                android:maxLength="1"
                android:inputType="numberPassword"
                android:textSize="32sp" />
            <TextView
                android:layout_marginStart="24dp"
                android:id="@+id/tv_pay2"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:background="@drawable/key_password"
                android:layout_weight="1"
                android:gravity="center"
                android:maxLength="1"
                android:inputType="numberPassword"
                android:textSize="32sp" />

            <TextView
                android:layout_marginStart="24dp"
                android:id="@+id/tv_pay3"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:background="@drawable/key_password"
                android:layout_weight="1"
                android:gravity="center"
                android:maxLength="1"
                android:inputType="numberPassword"
                android:textSize="32sp" />

            <TextView
                android:id="@+id/tv_pay4"
                android:layout_marginStart="24dp"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:background="@drawable/key_password"
                android:layout_weight="1"
                android:gravity="center"
                android:maxLength="1"
                android:inputType="numberPassword"
                android:textSize="32sp" />

            <TextView
                android:id="@+id/tv_pay5"
                android:layout_marginStart="24dp"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:background="@drawable/key_password"
                android:layout_weight="1"
                android:gravity="center"
                android:maxLength="1"
                android:inputType="numberPassword"
                android:textSize="32sp" />

            <TextView
                android:id="@+id/tv_pay6"
                android:layout_marginStart="24dp"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:background="@drawable/key_password"
                android:layout_weight="1"
                android:gravity="center"
                android:maxLength="1"
                android:inputType="numberPassword"
                android:textSize="32sp" />

        </LinearLayout>


        <!--        以下为键盘-->

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="#FFFFFF"
                android:orientation="vertical">

                <LinearLayout
                    android:paddingStart="48dp"
                    android:paddingEnd="48dp"
                    android:layout_width="fill_parent"
                    android:layout_height="71dp"
                    android:orientation="horizontal">

                    <TextView
                        android:id="@+id/num1"
                        style="@style/key_board_text"
                        android:background="@drawable/shape_key_board_bg"
                        android:text="1" />

                    <TextView
                        android:id="@+id/num2"
                        style="@style/key_board_text"
                        android:layout_marginLeft="60dp"
                        android:background="@drawable/shape_key_board_bg"
                        android:text="2" />

                    <TextView
                        android:id="@+id/num3"
                        style="@style/key_board_text"
                        android:layout_marginLeft="60dp"
                        android:background="@drawable/shape_key_board_bg"
                        android:text="3" />
                </LinearLayout>

                <!--  -->

                <LinearLayout
                    android:paddingStart="48dp"
                    android:paddingEnd="48dp"
                    android:layout_width="fill_parent"
                    android:layout_height="71dp"
                    android:layout_marginTop="20dp"
                    android:orientation="horizontal">

                    <TextView
                        android:id="@+id/num4"
                        style="@style/key_board_text"
                        android:background="@drawable/shape_key_board_bg"
                        android:text="4" />

                    <TextView
                        android:id="@+id/num5"
                        style="@style/key_board_text"
                        android:layout_marginLeft="60dp"
                        android:background="@drawable/shape_key_board_bg"
                        android:text="5" />

                    <TextView
                        android:id="@+id/num6"
                        style="@style/key_board_text"
                        android:layout_marginLeft="60dp"
                        android:background="@drawable/shape_key_board_bg"
                        android:text="6" />
                </LinearLayout>

                <!--  -->

                <LinearLayout
                    android:paddingStart="48dp"
                    android:paddingEnd="48dp"
                    android:layout_width="fill_parent"
                    android:layout_height="71dp"
                    android:layout_marginTop="20dp"
                    android:orientation="horizontal">

                    <TextView
                        android:id="@+id/num7"
                        style="@style/key_board_text"
                        android:background="@drawable/shape_key_board_bg"
                        android:text="7" />

                    <TextView
                        android:id="@+id/num8"
                        style="@style/key_board_text"
                        android:layout_marginLeft="60dp"
                        android:background="@drawable/shape_key_board_bg"
                        android:text="8" />

                    <TextView
                        android:id="@+id/num9"
                        style="@style/key_board_text"
                        android:layout_marginLeft="60dp"
                        android:background="@drawable/shape_key_board_bg"
                        android:text="9" />
                </LinearLayout>
                <!--  -->

                <LinearLayout
                    android:paddingStart="48dp"
                    android:paddingEnd="48dp"
                    android:layout_width="fill_parent"
                    android:layout_height="71dp"
                    android:layout_marginTop="20dp"
                    android:orientation="horizontal">

                    <TextView
                        android:id="@+id/num0"
                        style="@style/key_board_text"
                        android:background="@drawable/shape_key_board_bg"
                        android:text="0" />

                    <ImageView
                        android:id="@+id/del"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:layout_width="0dp"
                        android:layout_marginLeft="60dp"
                        android:background="@drawable/shape_key_board_bg"
                        android:src="@drawable/md_backspace" />
                </LinearLayout>


                <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:layout_marginTop="20dp"
                    android:background="#BBBBBB" />

                <RelativeLayout
                    android:layout_width="fill_parent"
                    android:layout_height="88dp"
                    android:orientation="horizontal"
                    android:paddingStart="48dp"
                    android:paddingTop="14dp"
                    android:paddingEnd="48dp">


                    <TextView
                        android:id="@+id/cancel"
                        style="@style/key_board_text"
                        android:layout_width="match_parent"
                        android:layout_height="60dp"
                        android:background="@drawable/shape_key_board_bg"
                        android:text="取消" />

                </RelativeLayout>
            </LinearLayout>
        </LinearLayout>


    </LinearLayout>

</LinearLayout>

其中,数字键盘和背景的样式举例如下:

<style name="key_board_text">
        <item name="android:textSize">32sp</item>
        <item name="android:textColor">#333333</item>
        <item name="android:gravity">center</item>
        <item name="android:layout_weight">1</item>
        <item name="android:layout_width">0dp</item>
        <item name="android:textStyle">bold</item>
        <item name="android:layout_height">match_parent</item>
    </style>

    

shape_alert_dialog.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

    <solid android:color="#ffffff"/>

    <corners android:radius="16dp"/>

</shape>

    

密码输入框灰色:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="4dp" />
    <solid android:color="#F5F5F5" />
</shape>

建立一个LinearLayout类为自定义键盘上的每个按钮设置点击事件监听器,以便在用户点击时执行相应的操作。你可以在布局文件中为每个按钮设置android:onClick属性,或者在代码中动态地为它们设置监听器,并设置text的显示,最后定义一个接口(onInputChanged)以便在输入满6位时通知实现者:

public class KeyBoardView extends LinearLayout implements View.OnClickListener {


    View[] numberViews;

    View delView;

    private TextView tvFirst, tvSecond, tvThird, tvForth, tvFifth, tvSixth;


    private StringBuilder mPassword;

    public KeyBoardView(Context context) {
        this(context, null);
    }

    public KeyBoardView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        InitView(context);
    }

    private void InitView(Context context) {
        //绑定0-9
        numberViews = new View[10];
        View view = LayoutInflater.from(context).inflate(R.layout.key_alert_dialog, this);
        Context viewContext = view.getContext();
        for (int i = 0; i < numberViews.length; i++) {
            int id = viewContext.getResources().getIdentifier("num" + i, "id", context.getPackageName());
            numberViews[i] = findViewById(id);
            numberViews[i].setOnClickListener(this);
        }
        //绑定删除
        delView = findViewById(R.id.del);
        delView.setOnClickListener(this);
        initPayEditText(view);

    }

    private void initPayEditText(View view) {
        mPassword = new StringBuilder();
        tvFirst = view.findViewById(R.id.tv_pay1);
        tvSecond = view.findViewById(R.id.tv_pay2);
        tvThird = view.findViewById(R.id.tv_pay3);
        tvForth = view.findViewById(R.id.tv_pay4);
        tvFifth = view.findViewById(R.id.tv_pay5);
        tvSixth = view.findViewById(R.id.tv_pay6);
    }

    @Override
    public void onClick(View view) {
        if (getNumberClicked(view) != -1) {
            add(getNumberClicked(view));
        }

        if (view.getId() == R.id.del) {
            remove();
        }
    }

    /**
     * 删除密码
     */
    public void remove() {
        if (mPassword != null && mPassword.length() > 0) {
            if (mPassword.length() == 1) {
                tvFirst.setText("");
            } else if (mPassword.length() == 2) {
                tvSecond.setText("");
            } else if (mPassword.length() == 3) {
                tvThird.setText("");
            } else if (mPassword.length() == 4) {
                tvForth.setText("");
            } else if (mPassword.length() == 5) {
                tvFifth.setText("");
            } else if (mPassword.length() == 6) {
                tvSixth.setText("");
            }
            mPassword.deleteCharAt(mPassword.length() - 1);
        }
    }


    /**
     * 输入密码
     *
     * @param valueInt
     */
    public void add(int valueInt) {
        String value = Integer.toString(valueInt);
        if (mPassword != null && mPassword.length() < 6) {
            mPassword.append(value);
            if (mPassword.length() == 1) {
                tvFirst.setText(value);
            } else if (mPassword.length() == 2) {
                tvSecond.setText(value);
            } else if (mPassword.length() == 3) {
                tvThird.setText(value);
            } else if (mPassword.length() == 4) {
                tvForth.setText(value);
            } else if (mPassword.length() == 5) {
                tvFifth.setText(value);
            } else if (mPassword.length() == 6) {
                tvSixth.setText(value);
                //业务
                inputCallback.onInputChanged(mPassword.toString());
            }

        }
    }


    public int getNumberClicked(View view) {
        int result = -1;
        for (int i = 0; i < numberViews.length; i++) {
            if (view.equals(numberViews[i])) {
                result = i;
                break;
            }
        }
        return result;
    }



    private InputCallback inputCallback;

    public void setInputListener(InputCallback inputCallback) {
        this.inputCallback = inputCallback;
    }

    public interface InputCallback {

        void onInputChanged(String text);

    }
}

自定义的弹窗

自定义PasswordDialog 继承 Dialog,将自定义布局加载到当前弹窗,并且将我们的取消按钮增加点击事件。最后,我们实现视图KeyBoardView的InputCallback接口完成数据接收:

public class PasswordDialog extends Dialog implements View.OnClickListener{

    private Activity context;

    TextView cancel;
    
    public PasswordDialog(@NonNull Activity context) {
        super(context);
        this.context = context;
        InitView();
    }

    private void InitView() {
        // 设置自定义布局
        KeyBoardView keyBoardView = new KeyBoardView(context);
        setContentView(keyBoardView);
        keyBoardView.setInputListener(text -> {
            if (text.length() == 6) {
                String dPwd = "666666";
                if (!TextUtils.equals(text, dPwd)) {
                    Toast.makeText(context, "密码错误", Toast.LENGTH_SHORT).show();
                } else {
                //校验通过
                    dismiss();
                }
            }
        });

 		cancel = findViewById(R.id.cancel);
        cancel.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
 		if (view.getId() == R.id.cancel) {
            dismiss();
        }
    }
}

调用弹窗中的视图

在你的Activity或Fragment中,使用AlertDialog或其他适当的方法来显示自定义键盘弹窗。首先,加载刚才创建的布局文件,并将其设置为弹窗的内容视图。
在Activity中调用方法,不如在点击事件中调用:

  private PasswordDialog passwordDialog;
      @Override
public void onClick(View view) {
	if (view.getId() == R.id.button) {
            showPasswordDialog(this);
    }
}
       

    /**
     * @param context 上下文对象
     */
    private void showPasswordDialog(Activity context) {
        passwordDialog = new PasswordDialog(context);
        passwordDialog.show();
    }

效果

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1911000.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

7月9日学习打卡-回文链表,交叉链表

大家好呀&#xff0c;本博客目的在于记录暑假学习打卡&#xff0c;后续会整理成一个专栏&#xff0c;主要打算在暑假学习完数据结构&#xff0c;因此会发一些相关的数据结构实现的博客和一些刷的题&#xff0c;个人学习使用&#xff0c;也希望大家多多支持&#xff0c;有不足之…

海外多语言盲盒APP系统开发

随着盲盒的全球化发展&#xff0c;盲盒已经成为了一个热门行业&#xff0c;不仅深受我国消费者的青睐&#xff0c;更是深受海外消费者的喜爱。目前&#xff0c;盲盒出海已经成为了企业拓展市场的新机会。 在数字化时代&#xff0c;海外盲盒APP为企业提供了一个快速打开海外盲盒…

57、基于概率神经网络(PNN)的分类(matlab)

1、基于概率神经网络(PNN)的分类简介 PNN&#xff08;Probabilistic Neural Network&#xff0c;概率神经网络&#xff09;是一种基于概率论的神经网络模型&#xff0c;主要用于解决分类问题。PNN最早由马科夫斯基和马西金在1993年提出&#xff0c;是一种非常有效的分类算法。…

MyBatis框架学习笔记(一):MyBatis入门

1 MyBatis 介绍 1.1 官方文档 MyBatis 中文手册&#xff1a; &#xff08;1&#xff09;https://mybatis.org/mybatis-3/zh/index.html &#xff08;2&#xff09;https://mybatis.net.cn/ Maven 仓库&#xff1a; https://mvnrepository.com/ 仓库作用&#xff1a;需要…

android Dialog全屏沉浸式状态栏实现

在Android中&#xff0c;创建沉浸式状态栏通常意味着让状态栏背景与应用的主题颜色一致&#xff0c;并且让对话框在状态栏下面显示&#xff0c;而不是浮动。为了实现这一点&#xff0c;你可以使用以下代码片段&#xff1a; 1、实际效果图&#xff1a; 2、代码实现&#xff1a;…

第一次坐火车/高铁,如何坐?全流程讲解

第一次坐动车注意事项 第一次乘动车流程&#xff1a;进站→安检→候车厅→找检票口→过闸机→站台候车→找车厢→上车找座→下车→出站 乘车流程 一、进火车站/高铁站&#xff1a;刷购票证件原件进站 1、自助闸机刷证&#xff1a;身份证 2、人工通道&#xff1a;护照、临时…

Oracle通过procedure调用webservice接口

文章目录 准备工作方法体如下Oracle语句详情重要参数说明Web Service的URL地址构造SOAP请求包体构造SOAP请求包体方法依次如下操作即可&#xff1a; 如果需要把上面的功能变成function或者procedure&#xff0c;请自行搜索相关的方法实现即可。 准备工作 定义测试的webservice…

怎么将图片旋转30度?旋转图片的几种方法推荐

怎么将图片旋转30度&#xff1f;在创作过程中&#xff0c;我们常常需要处理图片的镜像效果&#xff0c;确保其视觉效果和构图都达到最佳状态。镜像效果的合理运用不仅可以解决视觉单调的问题&#xff0c;还能在艺术作品中吸引观者的注意力。此外&#xff0c;镜像可以有效地调整…

MoRA: High-Rank Updating for Parameter-Efficient Fine-Tuning

文章汇总 LoRA的问题 与FFT相比&#xff0c;低秩更新难以记忆新知识。虽然不断提高LoRA的秩可以缓解这一问题&#xff0c;但差距仍然存在。 动机 尽可能地利用相同的可训练参数来获得更高的 Δ W \Delta W ΔW秩。 考虑到预训练权值 W 0 ∈ R d k W_0\in R^{d\times k} W0​…

Mac电脑上有什么好玩的肉鸽游戏推荐 苹果电脑怎么玩以撒的结合

Mac电脑尽管在游戏兼容性上可能不及Windows。但是&#xff0c;对于喜欢在Mac上游玩的玩家来说&#xff0c;依然有不少优秀的游戏可以选择&#xff0c;尤其是那些富有挑战性和策略性的肉鸽游戏。此外&#xff0c;对于经典游戏《以撒的结合》&#xff0c;Mac平台也提供了良好的游…

原创作品—数据可视化大屏

设计数据可视化大屏时&#xff0c;用户体验方面需注重以下几点&#xff1a;首先&#xff0c;确保大屏信息层次分明&#xff0c;主要数据突出显示&#xff0c;次要信息适当弱化&#xff0c;帮助用户快速捕捉关键信息。其次&#xff0c;设计应直观易懂&#xff0c;避免复杂难懂的…

论文 | Chain-of-Thought Prompting Elicits Reasoningin Large Language Models 思维链

这篇论文研究了如何通过生成一系列中间推理步骤&#xff08;即思维链&#xff09;来显著提高大型语言模型进行复杂推理的能力。论文展示了一种简单的方法&#xff0c;称为思维链提示&#xff0c;通过在提示中提供几个思维链示例来自然地激发这种推理能力。 主要发现&#xff1…

飞睿智能会议室静止雷达人体检测传感器,实时监测使用状态,有人、无人智能感应节能减

在这个科技日新月异的时代&#xff0c;每一个细微的创新都可能成为推动行业创新的关键力量。今天&#xff0c;让我们聚焦于一项看似不起眼却实则潜力无限的技术——飞睿智能静止雷达人体检测传感器&#xff0c;以及它在会议室这一商务交流核心区域中的巧妙应用。想象一下&#…

嵌入式c语言——指针加修饰符

指针变量可以用修饰符来修饰

图吧工具箱:DIY爱好者必备工具合集

名人说&#xff1a;莫道谗言如浪深&#xff0c;莫言迁客似沙沉。 ——刘禹锡《浪淘沙》 创作者&#xff1a;Code_流苏(CSDN)&#xff08;一个喜欢古诗词和编程的Coder&#x1f60a;&#xff09; 目录 一、概述二、主要功能1、硬件检测2、测试与故障诊断 三、使用方法四、总结 很…

我是如何渗透电信网络的

TLDR&#xff1b;作为一个电信公司的红队&#xff0c;从该公司的网络监控系统&#xff08;NMS&#xff09;获得一个立足点。通过基于HTTP协议的SSH隧道解决了Shell反连问题。通过HTTP获得SSH连接时&#xff0c;使用了Ninja。搭建代理以进行内部网络扫描。使用SS7应用程序访问CD…

斯坦福大学博士在GitHub发布的漫画机器学习小抄,竟斩获129k标星

斯坦福大学数据科学博士Chris Albon在GitHub上发布了一份超火的机器学习漫画小抄&#xff0c;发布仅仅一天就斩获GitHub榜首标星暴涨120k&#xff0c;小编有幸获得了一份并把它翻译成中文版本&#xff0c;今天给大家分享出来&#xff01; 轻松的画风配上让人更容易理解的文字讲…

瑞萨RH850 RTC计时进位异常

RH850 MCU的RTC&#xff08;实时时钟&#xff09;采用BCD&#xff08;二进制编码的十进制&#xff09;编码格式&#xff0c;支持闰年自动识别&#xff0c;并具有秒、分、时、日、周、月、年的进位功能。其中&#xff0c;秒和分为60进位&#xff0c;时为12或24进位&#xff0c;周…

救生拉网的使用方法及注意事项_鼎跃安全

水域救援在夏季尤为重要&#xff0c;随着气温的升高&#xff0c;人们更倾向于参与水上活动&#xff0c;如游泳、划船、垂钓等&#xff0c;这些活动虽然带来了乐趣和清凉&#xff0c;但同时也增加了水域安全事故的风险。救生拉网作为水域安全的重要工具之一&#xff0c;其重要性…

Vue3 pdf.js将二进制文件流转成pdf预览

好久没写东西&#xff0c;19年之前写过一篇Vue2将pdf二进制文件流转换成pdf文件&#xff0c;如果Vue2换成Vue3了&#xff0c;顺带来一篇文章&#xff0c;pdf.js这个东西用来解决内网pdf预览&#xff0c;是个不错的选择。 首先去pdfjs官网&#xff0c;下载需要的文件 然后将下载…