andrid实践项目四-对话框Dialog的各种用法

2024-03-14 09:58

本文主要是介绍andrid实践项目四-对话框Dialog的各种用法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

先来个简单的列子熟悉dialog是如何创建使用的

老规矩 先看dailog的继承关系

java.lang.Objectandroid.app.Dialog

dialog是直接继承object 与view没有半毛钱关系。
一。先来看看最常见的dialog,也就是AlertDialog(警告框),alertdialog是非阻塞式的。

public class AlertDialogtest extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.dailog);Button btn1=(Button) findViewById(R.id.btn1);btn1.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubDialog();}});}public void Dialog(){AlertDialog.Builder bulider = new AlertDialog.Builder(this);bulider.setIcon(R.drawable.ic_launcher);bulider.setMessage("你什么时候回寝室");bulider.setTitle("标准对话框");bulider.setCancelable(false);// 设置是否可以取消对话框//bulider.setPositiveButton("取消", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stub}});// 设置及其按钮bulider.setNegativeButton("取消", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stub}});AlertDialog dialog = bulider.create();dialog.show();}}

运行截图
代码逻辑还是很清楚的,设置button的点击事件。然后再写个dialog方法构建一个alretdialog。构建dialog时一定要引用builder

AlertDialog.Builder bulider = new AlertDialog.Builder(this);
builder.set~***
builder.set~***//这几句话都是用来设置dialog的属性
builder.set~***
然后用builder.creat();和builder.show();将builder显示出来。

再来设置一个类似listview 风格的alertdialog

运行效果如下所示
代码如下

public class AlertDialogtest2 extends Activity {private TextView text1;private TextView text2;private String[] data = { "苹果1", "苹果2", "苹果3", };private String[] datadetails = { "富含维生素c1", "富含维生素c2", "富含维生素c3", };private int NUM = 0;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.dailog2);text1 = (TextView) findViewById(R.id.mych);text2 = (TextView) findViewById(R.id.mytext);Button btn1 = (Button) findViewById(R.id.btn1);btn1.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubDialog();}});}public void Dialog() {Dialog dialog = new AlertDialog.Builder(AlertDialogtest2.this).setIcon(R.drawable.ic_launcher).setTitle("请选择你喜欢吃的水果?").setPositiveButton("确定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {text1.setText(data[NUM]);}}).setNegativeButton("取消", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {}}).setSingleChoiceItems(AlertDialogtest2.this.data, 0, new DialogInterface.OnClickListener() {// setSingleChoiceItems()方法@Overridepublic void onClick(DialogInterface dialog, int which) {text2.setText(datadetails[which]);NUM = which; // 保存选项的索引}}).create();dialog.show();}}

相比之前的标准框多了一个setSingleChoiceItems()方法,其中which指定了当前选择的是那一项。

DIY登录框和LayoutInflater

先写布局文件xml 只需要一个按钮就行了
然后写要引入的弹出框的布局文件login.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context="com.example.demotest.MainActivity" ><LinearLayout
        android:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="horizontal" ><TextView
            android:layout_width="wrap_content"android:layout_height="wrap_content" android:text="账号:"/><EditText
            android:id="@+id/edtlogin"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1000"android:hint="输入账号" /></LinearLayout><LinearLayout
        android:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="horizontal" ><TextView
            android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="密码:" /><EditText
            android:id="@+id/edtpassword"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1000"android:hint="输入密码" /></LinearLayout></LinearLayout>``````

在builder alertdialog 的时候引入一句就可以导入登录布局了
.setView(R.layout.login)

然后验证登陆的代码如下

public class AlertDialogtest3 extends Activity {private EditText edt1;private EditText edt2;private String string_userString;private String string_passwdString;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.dailog3);Button btn1 = (Button) findViewById(R.id.btn1);btn1.setOnClickListener(new OnClickListenerImpl());}private class OnClickListenerImpl implements OnClickListener{  @Override  public void onClick(View v) {  LayoutInflater layoutInflater=LayoutInflater.from(AlertDialogtest3.this);//获得layoutInflater对象  View view=layoutInflater.from(AlertDialogtest3.this).inflate(R.layout.login, null);//获得view对象  EditText edit_user=(EditText)view.findViewById(R.id.edtlogin);//获取控件  EditText edit_passwd=(EditText)view.findViewById(R.id.edtpassword);  //拿到输入的账号密码      string_userString=edit_user.getText().toString();  string_passwdString=edit_passwd.getText().toString();  Dialog dialog=new AlertDialog.Builder(AlertDialogtest3.this).setTitle("用户登录").setView(view).setPositiveButton("登录", new DialogInterface.OnClickListener() {   public void onClick(DialogInterface dialog, int which) {  if(string_userString.equals("admin")&&string_passwdString.equals("4011")){  Toast.makeText(AlertDialogtest3.this, "登录成功", Toast.LENGTH_SHORT).show();  }else{  Toast.makeText(AlertDialogtest3.this, "登录失败", Toast.LENGTH_SHORT).show();  }        }  }).setNegativeButton("取消", new DialogInterface.OnClickListener() {      @Override  public void onClick(DialogInterface dialog, int which) {  // 取消按钮事件   }  }).create();  dialog.show();  }         }   

拿到输入的账号密码 ,那是个坑。有兴趣的自己把gettext()换个位置试试……
这里写图片描述

这篇关于andrid实践项目四-对话框Dialog的各种用法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/808041

相关文章

基于MySQL Binlog的Elasticsearch数据同步实践

一、为什么要做 随着马蜂窝的逐渐发展,我们的业务数据越来越多,单纯使用 MySQL 已经不能满足我们的数据查询需求,例如对于商品、订单等数据的多维度检索。 使用 Elasticsearch 存储业务数据可以很好的解决我们业务中的搜索需求。而数据进行异构存储后,随之而来的就是数据同步的问题。 二、现有方法及问题 对于数据同步,我们目前的解决方案是建立数据中间表。把需要检索的业务数据,统一放到一张M

这15个Vue指令,让你的项目开发爽到爆

1. V-Hotkey 仓库地址: github.com/Dafrok/v-ho… Demo: 戳这里 https://dafrok.github.io/v-hotkey 安装: npm install --save v-hotkey 这个指令可以给组件绑定一个或多个快捷键。你想要通过按下 Escape 键后隐藏某个组件,按住 Control 和回车键再显示它吗?小菜一碟: <template

如何用Docker运行Django项目

本章教程,介绍如何用Docker创建一个Django,并运行能够访问。 一、拉取镜像 这里我们使用python3.11版本的docker镜像 docker pull python:3.11 二、运行容器 这里我们将容器内部的8080端口,映射到宿主机的80端口上。 docker run -itd --name python311 -p

在cscode中通过maven创建java项目

在cscode中创建java项目 可以通过博客完成maven的导入 建立maven项目 使用快捷键 Ctrl + Shift + P 建立一个 Maven 项目 1 Ctrl + Shift + P 打开输入框2 输入 "> java create"3 选择 maven4 选择 No Archetype5 输入 域名6 输入项目名称7 建立一个文件目录存放项目,文件名一般为项目名8 确定

系统架构师考试学习笔记第三篇——架构设计高级知识(20)通信系统架构设计理论与实践

本章知识考点:         第20课时主要学习通信系统架构设计的理论和工作中的实践。根据新版考试大纲,本课时知识点会涉及案例分析题(25分),而在历年考试中,案例题对该部分内容的考查并不多,虽在综合知识选择题目中经常考查,但分值也不高。本课时内容侧重于对知识点的记忆和理解,按照以往的出题规律,通信系统架构设计基础知识点多来源于教材内的基础网络设备、网络架构和教材外最新时事热点技术。本课时知识

Vue3项目开发——新闻发布管理系统(六)

文章目录 八、首页设计开发1、页面设计2、登录访问拦截实现3、用户基本信息显示①封装用户基本信息获取接口②用户基本信息存储③用户基本信息调用④用户基本信息动态渲染 4、退出功能实现①注册点击事件②添加退出功能③数据清理 5、代码下载 八、首页设计开发 登录成功后,系统就进入了首页。接下来,也就进行首页的开发了。 1、页面设计 系统页面主要分为三部分,左侧为系统的菜单栏,右侧

SpringBoot项目是如何启动

启动步骤 概念 运行main方法,初始化SpringApplication 从spring.factories读取listener ApplicationContentInitializer运行run方法读取环境变量,配置信息创建SpringApplication上下文预初始化上下文,将启动类作为配置类进行读取调用 refresh 加载 IOC容器,加载所有的自动配置类,创建容器在这个过程

bytes.split的用法和注意事项

当然,我很乐意详细介绍 bytes.Split 的用法和注意事项。这个函数是 Go 标准库中 bytes 包的一个重要组成部分,用于分割字节切片。 基本用法 bytes.Split 的函数签名如下: func Split(s, sep []byte) [][]byte s 是要分割的字节切片sep 是用作分隔符的字节切片返回值是一个二维字节切片,包含分割后的结果 基本使用示例: pa

Maven创建项目中的groupId, artifactId, 和 version的意思

文章目录 groupIdartifactIdversionname groupId 定义:groupId 是 Maven 项目坐标的第一个部分,它通常表示项目的组织或公司的域名反转写法。例如,如果你为公司 example.com 开发软件,groupId 可能是 com.example。作用:groupId 被用来组织和分组相关的 Maven artifacts,这样可以避免

2. 下载rknn-toolkit2项目

官网链接: https://github.com/airockchip/rknn-toolkit2 安装好git:[[1. Git的安装]] 下载项目: git clone https://github.com/airockchip/rknn-toolkit2.git 或者直接去github下载压缩文件,解压即可。