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

相关文章

全面掌握 SQL 中的 DATEDIFF函数及用法最佳实践

《全面掌握SQL中的DATEDIFF函数及用法最佳实践》本文解析DATEDIFF在不同数据库中的差异,强调其边界计算原理,探讨应用场景及陷阱,推荐根据需求选择TIMESTAMPDIFF或inte... 目录1. 核心概念:DATEDIFF 究竟在计算什么?2. 主流数据库中的 DATEDIFF 实现2.1

MySQL中的LENGTH()函数用法详解与实例分析

《MySQL中的LENGTH()函数用法详解与实例分析》MySQLLENGTH()函数用于计算字符串的字节长度,区别于CHAR_LENGTH()的字符长度,适用于多字节字符集(如UTF-8)的数据验证... 目录1. LENGTH()函数的基本语法2. LENGTH()函数的返回值2.1 示例1:计算字符串

Java中的数组与集合基本用法详解

《Java中的数组与集合基本用法详解》本文介绍了Java数组和集合框架的基础知识,数组部分涵盖了一维、二维及多维数组的声明、初始化、访问与遍历方法,以及Arrays类的常用操作,对Java数组与集合相... 目录一、Java数组基础1.1 数组结构概述1.2 一维数组1.2.1 声明与初始化1.2.2 访问

MySQL 中的 CAST 函数详解及常见用法

《MySQL中的CAST函数详解及常见用法》CAST函数是MySQL中用于数据类型转换的重要函数,它允许你将一个值从一种数据类型转换为另一种数据类型,本文给大家介绍MySQL中的CAST... 目录mysql 中的 CAST 函数详解一、基本语法二、支持的数据类型三、常见用法示例1. 字符串转数字2. 数字

Spring WebFlux 与 WebClient 使用指南及最佳实践

《SpringWebFlux与WebClient使用指南及最佳实践》WebClient是SpringWebFlux模块提供的非阻塞、响应式HTTP客户端,基于ProjectReactor实现,... 目录Spring WebFlux 与 WebClient 使用指南1. WebClient 概述2. 核心依

MyBatis-Plus 中 nested() 与 and() 方法详解(最佳实践场景)

《MyBatis-Plus中nested()与and()方法详解(最佳实践场景)》在MyBatis-Plus的条件构造器中,nested()和and()都是用于构建复杂查询条件的关键方法,但... 目录MyBATis-Plus 中nested()与and()方法详解一、核心区别对比二、方法详解1.and()

Spring Boot @RestControllerAdvice全局异常处理最佳实践

《SpringBoot@RestControllerAdvice全局异常处理最佳实践》本文详解SpringBoot中通过@RestControllerAdvice实现全局异常处理,强调代码复用、统... 目录前言一、为什么要使用全局异常处理?二、核心注解解析1. @RestControllerAdvice2

Python中你不知道的gzip高级用法分享

《Python中你不知道的gzip高级用法分享》在当今大数据时代,数据存储和传输成本已成为每个开发者必须考虑的问题,Python内置的gzip模块提供了一种简单高效的解决方案,下面小编就来和大家详细讲... 目录前言:为什么数据压缩如此重要1. gzip 模块基础介绍2. 基本压缩与解压缩操作2.1 压缩文

Spring事务传播机制最佳实践

《Spring事务传播机制最佳实践》Spring的事务传播机制为我们提供了优雅的解决方案,本文将带您深入理解这一机制,掌握不同场景下的最佳实践,感兴趣的朋友一起看看吧... 目录1. 什么是事务传播行为2. Spring支持的七种事务传播行为2.1 REQUIRED(默认)2.2 SUPPORTS2

解读GC日志中的各项指标用法

《解读GC日志中的各项指标用法》:本文主要介绍GC日志中的各项指标用法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、基础 GC 日志格式(以 G1 为例)1. Minor GC 日志2. Full GC 日志二、关键指标解析1. GC 类型与触发原因2. 堆