广告
返回顶部
首页 > 资讯 > 移动开发 >Android如何自定义输入文本对话框?
  • 625
分享到

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

androidjavaandroidstudio 2023-10-26 16:10:51 625人浏览 独家记忆
摘要

文章目录 0.引言1.创建示例工程2.输入文本对话框布局和功能设计3.主程序调用输入文本对话框 0.引言   笔者研究的课题涉及到安卓软件开发,在开发过程中,发现普通的显示消息对话框一般可

文章目录

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) {          editText.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].

来源地址:https://blog.csdn.net/qq_40640910/article/details/130781847

--结束END--

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

本文链接: https://www.lsjlt.com/news/464942.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Android如何自定义输入文本对话框?
    文章目录 0.引言1.创建示例工程2.输入文本对话框布局和功能设计3.主程序调用输入文本对话框 0.引言   笔者研究的课题涉及到安卓软件开发,在开发过程中,发现普通的显示消息对话框一般可...
    99+
    2023-10-26
    android java android studio
  • android如何自定义对话框
    这篇文章给大家分享的是有关android如何自定义对话框的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。实现效果  定义dialog.xml (res/layout/dialog.xml)<...
    99+
    2023-06-22
  • Android中如何自定义对话框
    本文小编为大家详细介绍“Android中如何自定义对话框”,内容详细,步骤清晰,细节处理妥当,希望这篇“Android中如何自定义对话框”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。本文测试的harbor的版本是...
    99+
    2023-06-29
  • android自定义AlertDialog对话框
    前面一篇文章//www.jb51.net/article/103036.htm介绍了alertDialog的四种简单使用,但是有些时候为了让整个app的风格统一,或者说前面的四...
    99+
    2022-06-06
    alertdialog Android
  • Android自定义对话框Dialog
    本文简单介绍自定义对话框Dialog的使用,代码和结构都非常简单,目的是能够快速使用自定义对话框,在本文中不具体讲解对话框的高级使用。 实现步骤 首先需要自己在我们的.xml文...
    99+
    2022-06-06
    dialog Android
  • Android 自定义对话框 showSetPwdDialog
    样式如下所示: 布局: layout   dialog_set_pwd.xml <?xml version="." encoding="utf-"&...
    99+
    2022-06-06
    自定义 Android
  • Android自定义等待对话框
    最近,看了好多的APP的等待对话框,发现自己的太lower,于是就研究了一番,最后经过苦心努力,实现一个。 自定义一个LoadingIndicatorView(extend...
    99+
    2022-06-06
    Android
  • Android常用的AlertDialog对话框及自定义对话框
    常用的Dialog有确认对话框,单选按钮对话框,多选按钮对话框,复选按钮对话框另外还有自定义的对话框 AlertDialog的常用方法 setTitle:为对话框设置标题 se...
    99+
    2022-06-06
    自定义 alertdialog Android
  • android自定义带箭头对话框
    本文实例为大家分享了android自定义带箭头对话框的具体代码,供大家参考,具体内容如下 import android.content.Context; import andr...
    99+
    2022-11-11
  • Android studio自定义对话框效果
    本文实例为大家分享了Android studio自定义对话框效果的具体代码,供大家参考,具体内容如下 实现步骤: 第一步:自定义.xml布局文件 <?xml ver...
    99+
    2022-11-12
  • android自定义对话框实例代码
    1.实现效果    2.定义dialog.xml (res/layout/dialog.xml) <?xml version="1.0" encoding="utf...
    99+
    2022-11-12
  • Android Compose自定义TextField如何实现自定义的输入框
    这篇文章主要介绍Android Compose自定义TextField如何实现自定义的输入框,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!简单自定义BasicTextField示例代码 var&n...
    99+
    2023-06-29
  • Android基础篇-对话框总结(普通对话框,单选对话框,多选对话框,自定义对话框)
    一:AlterDialog对话框 2:对话框圆角显示(在drawable下创建radiu...
    99+
    2022-06-06
    自定义 Android
  • Android自定义九宫格输入框
    本文实例为大家分享了Android自定义九宫格输入框的具体代码,供大家参考,具体内容如下 效果 实现 绘制宫格分割线 这里我们用一个RectF类型的数组来装载数据。在onSizeC...
    99+
    2022-11-13
  • Android对话框自定义标题 对话框标题美化操作
    Android自带的对话框标题不好看,如果我们需要给弹出的对话框设置一个自己定义的标题,可以使用AlertDialog.Builder的setCustomTitle()方法。&...
    99+
    2022-06-06
    自定义 Android
  • Android自定义样式圆角dialog对话框
    本文实例为大家分享了Android创建自定义样式圆角dialog对话框的具体代码,供大家参考,具体内容如下 效果如上,圆角对话框,标题和正文都可以自己设定 做法: 1.在res文件...
    99+
    2022-11-12
  • Android自定义对话框的简单实现
    本文实例为大家分享了Android自定义对话框的具体实现代码,供大家参考,具体内容如下 1、定义对话框的布局 <xml version="1.0" encoding="utf-...
    99+
    2022-11-13
  • Android Studio使用自定义对话框效果
    Android Studio基础使用自定义对话框,供大家参考,具体内容如下 兼容低版本的APP运行 第一步:新建新的空白activity,布局XML设置如下 该APP的启动界面ac...
    99+
    2022-11-13
  • Android如何自定义升级对话框示例详解
    前言本文主要给大家介绍了关于Android自定义升级对话框的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。实现的效果如下所示其实这也只是一个DialogFragment 而已,重点只是在于界面的设计想要使用做出这...
    99+
    2023-05-30
    android 自定义 升级对话框
  • Android自定义View验证码输入框
    本文实例为大家分享了Android自定义View验证码输入框的具体代码,供大家参考,具体内容如下 验证码输入框 1.先看下样式 2.直接上代码 public class M...
    99+
    2022-06-06
    view 输入 输入框 Android
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作