Android如何自定义输入文本对话框?

news2024/9/21 12:41:46

文章目录

  • 0.引言
  • 1.创建示例工程
  • 2.输入文本对话框布局和功能设计
  • 3.主程序调用输入文本对话框

0.引言

  笔者研究的课题涉及到安卓软件开发,在开发过程中,发现普通的显示消息对话框一般可以调用android自带包实现,而要通过文本框输入交互,则无法轻易实现。在查阅网络资料后,实现了自定义输入文本对话框的功能,本文记录实现自定义输入文本对话框的过程。

1.创建示例工程

  (1)创建TestInputDialog工程
  在这里插入图片描述

  (2)生成主程序
  在这里插入图片描述

  (3)新建输入文本对话框布局(.xml)和功能(.java)文件
  在这里插入图片描述

2.输入文本对话框布局和功能设计

  (1)新建形状文件(myshape.xml)
  在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">  
    <!--设置圆角-->  
    <corners android:radius="0dp" />  
    <!--设置边框-->  
    <stroke  
        android:width="0.1dp"  
        android:color="#d7d7db" />  
    <!--设置填充色-->  
    <solid android:color="#f8f8f8" />  
</shape>

  (2)添加对话框样式(themes.xml)
  在这里插入图片描述

<style name="CustomInputStyle" parent="@android:style/Theme.Dialog">
    <!--这个说明提示框是否有边框-->  
    <item name="android:windowFrame">@null</item>  
    <!--这个说明提示框是否是浮动的-->  
    <item name="android:windowIsFloating">true</item>  
    <!--这个说明提示框是滞是透明的-->  
    <item name="android:windowIsTranslucent">false</item>  
    <!--这个说明提示框是否有标题-->  
    <item name="android:windowNoTitle">true</item>  
    <!--这个说明提示框的背景颜色是什么-->  
    <item name="android:windowBackground">@drawable/myshape</item>  
    <!--这个说明是否充许对话框的背景变暗。为true则充许变暗-->  
    <item name="android:backgroundDimEnabled">false</item>  
</style>

  (3)新建对话框布局文件(input_dialog.xml)
  在这里插入图片描述

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:orientation="vertical">  
  
    <LinearLayout  
        android:layout_width="270dp"  
        android:layout_height="wrap_content"  
        android:orientation="vertical">  
  
        <TextView  
            android:id="@+id/title"  
            android:layout_width="match_parent"  
            android:layout_height="wrap_content"  
            android:layout_marginLeft="15dp"  
            android:layout_marginTop="15dp"  
            android:layout_marginRight="15dp"  
            android:gravity="center_horizontal"  
            android:text="输入对话框"  
            android:textSize="19sp" />  
  
        <EditText  
            android:id="@+id/et_input"  
            android:layout_width="match_parent"  
            android:layout_height="wrap_content"  
            android:layout_marginLeft="15dp"  
            android:layout_marginTop="10dp"  
            android:layout_marginRight="15dp"  
            android:layout_marginBottom="10dp"  
            android:gravity="center_horizontal"  
            android:hint="输入内容"  
            android:lineSpacingExtra="5dp"  
            android:textSize="17sp" />  
  
        <View  
            android:layout_width="match_parent"  
            android:layout_height="1dp"  
            android:background="#DFDFDF" />  
  
        <LinearLayout  
            android:layout_width="match_parent"  
            android:layout_height="44dp"  
            android:orientation="horizontal">  
  
            <Button  
                android:id="@+id/btn_ok"  
                android:layout_width="wrap_content"  
                android:layout_height="match_parent"  
                android:layout_weight="1"  
                android:clickable="true"  
                android:gravity="center"  
                android:text="确认"  
                android:textSize="17sp" />  
  
            <View  
                android:layout_width="1dp"  
                android:layout_height="match_parent"  
                android:background="#DFDFDF" />  
  
            <Button  
                android:id="@+id/btn_cancel"  
                android:layout_width="wrap_content"  
                android:layout_height="match_parent"  
                android:layout_weight="1"  
                android:clickable="true"  
                android:gravity="center"  
                android:text="取消"  
                android:textSize="17sp" />  
  
        </LinearLayout>  
    </LinearLayout>  
</LinearLayout>

  (4)新建对话框功能代码文件(CustomInputDialog.java)
  在这里插入图片描述

package com.example.testinputdialog;
  
import android.annotation.SuppressLint;  
import android.app.Dialog;  
import android.content.Context;  
import android.view.LayoutInflater;  
import android.view.View;  
import android.widget.EditText;  
import android.widget.TextView;  
  
import androidx.annotation.NonNull;  
  
public class CustomInputDialog extends Dialog{  
    Context mContext;  
    private TextView btn_ok;  
    private TextView btn_cancel;  
    private TextView title;  
    private EditText editText;  
    public CustomInputDialog(@NonNull Context context) {  
        super(context, R.style.CustomInputStyle);  
        this.mContext = context;  
        initView();  
    }  
    @SuppressLint("MissingInflatedId")  
    public void initView() {  
        View view = LayoutInflater.from(mContext).inflate(R.layout.input_dialog, null);  
        title = (TextView) view.findViewById(R.id.title);  
        editText = (EditText) view.findViewById(R.id.et_input);  
        btn_ok = (TextView) view.findViewById(R.id.btn_ok);  
        btn_cancel = (TextView) view.findViewById(R.id.btn_cancel);  
        super.setContentView(view);  
    }  
    public CustomInputDialog setTile(String s) {  
        title.setText(s);  
        return this;  
    }  
    //获取当前输入框对象  
    public View getEditText() {  
        return editText;  
    }  
    //传递数据给输入框对象  
    public CustomInputDialog setEditText(String s) {  
        title.setText(s);  
        return this;  
    }  
    //确定键监听器  
    public void setOnSureListener(View.OnClickListener listener) {  
        btn_ok.setOnClickListener(listener);  
    }  
    //取消键监听器  
    public void setOnCanlceListener(View.OnClickListener listener) {  
        btn_cancel.setOnClickListener(listener);  
    }  
}

3.主程序调用输入文本对话框

  (1)主程序布局实现
  在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:app="http://schemas.android.com/apk/res-auto"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    tools:context=".MainActivity">  
  
    <TextView  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:text="点击,显示输入对话框,可显示输入值"  
        android:onClick="showInput"  
        app:layout_constraintBottom_toBottomOf="parent"  
        app:layout_constraintEnd_toEndOf="parent"  
        app:layout_constraintStart_toStartOf="parent"  
        app:layout_constraintTop_toTopOf="parent" />  
  
</androidx.constraintlayout.widget.ConstraintLayout>

  (2)主程序功能实现
  在这里插入图片描述

package com.example.testinputdialog;
  
import androidx.appcompat.app.AppCompatActivity;  
  
import android.os.Bundle;  
import android.view.View;  
import android.widget.EditText;  
import android.widget.Toast;  
  
public class MainActivity extends AppCompatActivity {  
  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
    }  
  
    public void showInput(View view) {  
        final CustomInputDialog customDialog = new CustomInputDialog(this);  
        final EditText editText = (EditText) customDialog.getEditText();//方法在CustomDialog中实现  
        customDialog.setOnSureListener(new View.OnClickListener() {  
            @Override  
            public void onClick(View v) {  
                Toast.makeText(MainActivity.this, "你点击了确定,输入的值为:"+editText.getText().toString(), Toast.LENGTH_SHORT).show();  
                customDialog.dismiss();  
            }  
        });  
        customDialog.setOnCanlceListener(new View.OnClickListener() {  
            @Override  
            public void onClick(View v) {  
                Toast.makeText(MainActivity.this, "你点击了取消", Toast.LENGTH_SHORT).show();  
                customDialog.dismiss();  
            }  
        });  
        customDialog.setTile("请输入内容");  
        customDialog.show();  
    }  
}

  (3)结果展示
  在这里插入图片描述

参考资料:
[1] yuan_fang_yan. android自定义普通对话框,输入框对话框; 2017-09-27 [accessed 2023-05-20].
[2] 晓艳考研. android点击按钮弹出输入框,android 弹出框(输入框和选择框); 2021-05-26 [accessed 2023-05-20].
[3] 李易-_-. Android 密码输入框; 2017-11-06 [accessed 2023-05-20].
[4] Android_xiong_st. (原创)安卓自定义shape方法; 2023-03-21 [accessed 2023-05-20].

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

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

相关文章

代码随想录算法训练营第十一天|20. 有效的括号、1047. 删除字符串中的所有相邻重复项、150. 逆波兰表达式求值

今天的题目都是对栈的经典应用。 有效的括号 题目链接&#xff1a;力扣 解题思路&#xff1a;基于栈结构的特殊性&#xff0c;其非常适合做对称匹配类问题&#xff0c;其实如果知道了要用栈解这道题&#xff0c;在脑中模拟一遍&#xff0c;接下来的思路就是自然而然能够想到…

STM32 多路ADC同时扫描采样

背景 在项目实际应用中&#xff0c;刚好有需求需要使用多路ADC同时采样&#xff0c;这里就选择STM32 ADC多路ADC同时采样&#xff0c;这里简单说明下配置过程&#xff0c;以及使用步骤 原理图 如下图所示&#xff0c;使用四路ADC输入 ADC_Voltage -> 电压信号的采样&…

如何查看linux分区挂载在哪个目录?

一&#xff0c;简介 在linux系统中&#xff0c;如何查看磁盘分区是挂载在哪个目录呢&#xff1f;今天介绍一种方法&#xff0c;供参考。 二&#xff0c;图形化界面查看分区挂载方法 2.1 打开disk工具 2.2 点击查看对应的分区 看一个分区&#xff1a; 如上图所示&#xff0…

百度地图网页设计

一、百度地图api 1、百度搜索——百度地图API 进入——开放平台——开发文档——JavaScript API JavaScript API 首先是GL版本&#xff0c;是三维的效果&#xff0c;我们一般使用二维&#xff0c;选择下面v3.0版本 2、开发指南——注册账号 跟着提示来申请密钥就可。 二、…

Linux基本指令【下】

目录 一、时间相关指令 date显示 时间戳 二、cal指令 三、find指令 &#xff08;重要&#xff09;-name 四、grep指令 五、zip\unzip指令 六、tar指令&#xff1a;打包/解压&#xff0c;不打开它&#xff0c;直接看内容 七、bc指令 八、uname -r指令 九、几个重要…

ConstrainLayout(约束布局)属性详解

layout_constraintLeft_toLeftOf layout_constraintRight_toRightOf 这两个基本上用不上因为 layout_constraintStart_toStartOf就相当于layout_constraintLeft_toLeftOf layout_constraintEnd_toEndOf就相当于layout_constraintRight_toRightOf app:layout_constraintBottom_t…

【VMware】Ubantu 22.04配置静态IP

文章目录 一、VMware 虚拟网络配置VMnet8 网络设置注意 关于取消勾选使用本地DHCP服务将IP地址分配给虚拟机VMnet8 NAT设置 网关IP 二、虚拟机 网络适配器三、启动虚拟机 配置网络查看网卡名设置静态IP Reference 一、VMware 虚拟网络配置 VMnet8 网络设置 子网IP子网掩码 编…

22 外部排序

外部排序 外部排序的基本内容 前面介绍过的排序方法都是在内存中进行的&#xff08;称为内部排序&#xff09;。外部排序是一种无法全部装入内存的大规模数据集的排序算法。与在内存中处理数据的内部排序相比&#xff0c;外部排序要复杂的多&#xff0c;主要因为是其需要解决…

【c语言】二进制文件的读写操作

创作不易&#xff0c;本篇文章如果帮助到了你&#xff0c;还请点赞 关注支持一下♡>&#x16966;<)!! 主页专栏有更多知识&#xff0c;如有疑问欢迎大家指正讨论&#xff0c;共同进步&#xff01; &#x1f525;c语言系列专栏&#xff1a;c语言之路重点知识整合 &#x…

1.1 IAR新建工作空间 及 新建工程

目录 新建工作空间 新建工程 新建工作空间 &#xff08;1&#xff09;创建一个名字为Workspace的文件夹&#xff0c;如图所示。 &#xff08;2&#xff09;运行IAR EW for 8051 10.10.1&#xff0c;如图所示。 &#xff08;3&#xff09;依次选择File和New Workspace&#xf…

【IDEA使用指南】如何将 IDEA 开发工具作为可视化工具来操作数据库?

如何将 IDEA 开发工具作为可视化工具来操作数据库&#xff1f; 步骤1&#xff1a;找到数据源配置的工具栏。 如下图所示&#xff0c;找到 “View -> Tool Windows -> Database”&#xff0c;点击“Database”。 步骤2&#xff1a;选择数据源类型。 在开发工具右侧会…

CSS背景色渐变

从上到下渐变&#xff1a;background: linear-gradient(red, pink); 从左到右渐变&#xff1a;background: linear-gradient(to right, red , pink); 对角&#xff08;从左上角到右下角&#xff09;渐变&#xff1a;background: linear-gradient(to bottom right, red , pink);…

【Linux】进程信号详解(三)

文章目录 一、可重入函数二、volatile三、SIGCHLD信号 一、可重入函数 假设有一个不带头的单链表&#xff0c;要进行头插操作&#xff0c;在我们数据结构阶段都已经学习过&#xff0c;我们可以有以下的步骤&#xff1a; 要将node1头插到单链表中&#xff0c;调用insert函数&…

什么是数据仓库

数据仓库的概念可以追溯到20世纪80年代&#xff0c;当时IBM的研究人员开发出了“商业数据仓库”。本质上&#xff0c;数据仓库试图提供一种从操作型系统到决策支持环境的数据流架构模型。数据仓库概念的提出&#xff0c;是为了解决与这个数据流相关的各种问题&#xff0c;主要是…

flutter 在动图上添加文字

前言 有这样一个场景&#xff0c;在一个展示很多文字的App中背景图片可以自定义&#xff0c;当然也可以是动态的&#xff0c;但是这个主页是可以分享出去的&#xff0c;也就是我需要在一个动态的背景上写上文字并保存为一张新的图片并分享出去。 实现 前置准备 需要导入一个…

【计算机视觉 | 目标检测】Objects365 :最新大规模高质量目标检测数据集

文章目录 一、前言二、数据集的规模三、数据集的质量四、泛化能力五、结语 一、前言 2019 年 4 月&#xff0c;在北京举行的智源学者计划启动暨联合实验室发布会上&#xff0c;北京旷视科技有限公司与北京智源人工智能研究院共同发布了全球最大的目标检测数据集 &#xff1a; …

ChatGPT国内免费访问

背景 ChatGPT作为一种基于人工智能技术的自然语言处理工具&#xff0c;近期的热度直接沸腾&#x1f30b;。 作为一个程序员&#xff0c;我也忍不住做了一个基于ChatGPT的网站&#xff0c;免费&#xff01;免梯子&#xff01;&#xff01;国内可直接对话ChatGPT&#xff0c;也…

使用Python和Scrapy实现抓取网站数据

Scrapy是一个功能强大的网络爬虫框架&#xff0c;允许开发者轻松地抓取和解析网站内容&#xff0c;这篇文章主要为大家介绍了如何使用Python的Scrapy库进行网站数据抓取&#xff0c;需要的可以参考一下 在本文中&#xff0c;我们将介绍如何使用Python的Scrapy库进行网站数据抓…

00后才是内卷界的扛把子,被卷的头皮发麻....

人们都说00后躺平了&#xff0c;但是有一说一&#xff0c;该卷的还是卷。这不&#xff0c;前一周时间我们公司来了个00年的&#xff0c;工作没两年&#xff0c;跳槽到我们公司起薪20K&#xff0c;都快接近我了。后来听同事说才知道人家是个卷王&#xff0c;从早干到晚就差搬张床…

哈希表应用——布隆过滤器

注&#xff1a;布隆过滤是用来处理海量数据且允许存在误判 目录 布隆过滤器提出 布隆过滤器概念 布隆过滤器的理论知识 布隆过滤器的实现 布隆过滤器的删除 布隆过滤器优点 布隆过滤器缺陷 布隆过滤器的应用场景 哈希切分 布隆过滤器/哈希切分面试题 布隆过滤器提出 …