Android中实现对话框可以使用AlertDialog.Builder类,还可以自定义对话框.如果对话框设置了按钮就需要对其设置事件监听OnClickListener.
下面将列出一个示例:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/t_username" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="帐号"
android:layout_marginLeft="20dip" />
<EditText android:id="@+id/e_username" android:hint="输入帐号"
android:layout_marginLeft="20dip" android:layout_marginRight="20dip"
android:layout_width="fill_parent" android:layout_height="wrap_content"></EditText>
<TextView android:id="@+id/t_password" android:layout_width="wrap_content"
android:layout_marginLeft="20dip" android:layout_height="wrap_content"
android:text="密码"></TextView>
<EditText android:id="@+id/e_password" android:hint="输入密码"
android:password="true" android:layout_marginLeft="20dip"
android:layout_marginRight="20dip" android:layout_width="fill_parent"
android:layout_height="wrap_content"></EditText>
</LinearLayout>
package com.Aina.Android;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
public class Test_Dialog extends Activity implements OnClickListener {
/** Called when the activity is first created. */
String[] str = { "111", "222", "333", "444" };
TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView) this.findViewById(R.id.TextView);
Dialog dialog = new AlertDialog.Builder(this).setTitle("对话框标题")
.setIcon(R.drawable.icon).setMessage("登陆对话框")
// .setItems(str, Test_Dialog.this)// 设置对话框要显示的一个list
// .setSingleChoiceItems(str, 0, Test_Dialog.this)//
// 设置对话框显示一个单选的list
.setPositiveButton("确定", Test_Dialog.this)
// .setNegativeButton("取消", Test_Dialog.this)
.setNeutralButton("退出",Test_Dialog.this)
.create();
dialog.show();
}
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
if (which == Dialog.BUTTON_POSITIVE) {
// tv.setText("点击了确定按钮");
this.login();
} else if (which == Dialog.BUTTON_NEUTRAL) {
this.finish();
} else {
tv.setText("点击了" + str[which]);
}
}
public void login() {
LayoutInflater inflater = LayoutInflater.from(this);
View view = inflater.inflate(R.layout.dialog, null);
Dialog dialog = new AlertDialog.Builder(this).setTitle("登陆").setView(
view).setPositiveButton("登陆", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
final ProgressDialog pdialog = ProgressDialog.show(Test_Dialog.this, "Login", "登陆中...", true);
new Thread(){
public void run(){
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
pdialog.dismiss();
}
}
}.start();
}
}).setNegativeButton("退出", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Test_Dialog.this.finish();
}
}).create();
dialog.show();
}
}
分享到:
相关推荐
本文将详细介绍7种常见的Android Dialog使用实例,帮助开发者更好地理解和应用这些对话框。 1. 基本确认对话框 这种对话框通常用于确认用户的操作,例如退出应用。创建此类Dialog的基本代码如下: ```java ...
在本文中,我们将深入探讨如何在Android应用中有效地使用Dialog。 首先,理解Dialog的基本概念。Dialog通常包含一个标题(title)、内容(content)和一组按钮(如“确定”、“取消”)。它们可以是模态(阻塞用户...
在Android开发中 我们经常会需要在Android界面上弹出一些对话框 比如询问用户或者让用户选择 关于Android Dialog的使用方法 已经有很多blog做出了详细的介绍(比如博客:7种形式的Android Dialog使用举例) 但是只是...
在Android开发中,有时我们需要在Dialog中展示动态内容,如GIF动图,来提供更丰富的用户交互体验。本文将详细讲解如何在Android Dialog中利用Glide库加载并播放GIF。 首先,Glide是一个非常流行的Android图片加载库...
2. 使用第三方库:例如`androidx.core.widget.TintEditText`库,它提供了一种更灵活的方式来控制Dialog的位置。 在压缩包文件`MyDialog`中,可能包含了一个示例项目,展示了如何实现上述自定义Dialog样式和位置的...
在Android开发中,Dialog是一种非常常见的用户界面组件,它用于向用户展示临时信息或进行简单的交互操作。对话框通常会浮现在应用主界面之上,但它的显示位置可以根据开发者的需求进行定制。本文将深入探讨如何在...
在Android开发中,Dialog是一种常见的用户交互界面,用于显示临时信息或者进行简单的用户操作。而软键盘的管理和显示则是移动应用用户体验的关键因素之一。本文将深入探讨如何在Android中正确处理Dialog与软键盘的...
在Android开发中,自定义Dialog是一种常见的用户交互方式,它能提供更为丰富的界面和功能,以满足特定场景下的需求。本教程将详细讲解如何创建一个全屏显示且带有动画效果的自定义Dialog,并结合相机和图片选择的...
在Android开发中,Dialog是一种常见的用户交互组件,用于在主线程中显示临时信息或进行简单的操作选择。在设计用户界面时,有时我们可能希望Dialog具有透明背景或者可以自定义其显示位置,以达到更佳的视觉效果。本...
在Android开发中,Dialog是一种非常常见的用户界面组件,它用于在主界面之上显示临时的通知或交互窗口,以向用户展示信息、请求确认或者提供选项。本篇将详细讲解如何正确使用Dialog及其规范模板,以及创建Dialog的...
在Android开发中,Dialog...总的来说,Android Dialog是与用户交互的重要工具,理解并熟练使用各种Dialog可以帮助提升应用的用户体验。通过系统提供的Dialog和自定义Dialog,开发者可以根据应用场景选择最适合的方案。
在标题"Android dialog单选、多选弹窗"中,我们主要关注的是两种类型的Dialog:单选对话框(Radio Button Dialog)和多选对话框(Checkbox Dialog),以及可能涉及到的PopWindow窗口。 1. **单选对话框**: 单选...
总之,"android dialog向上弹效果"是通过Android的动画系统实现的,可以使用内置的动画类或者自定义XML动画资源。结合自定义Dialog和适当的布局设计,可以创建出具有专业水准和独特风格的Dialog动画效果。在实际开发...
3. **理解基本概念**:熟悉Android中Dialog的基本用法,了解LayoutInflater的作用及如何使用findViewById方法获取控件引用。 #### 三、实现步骤 接下来,我们将按照以下步骤来实现带有输入框的Dialog,并从中获取...
在Android开发中,有时我们需要创建一个对话框(Dialog)来展示一些信息或者提供用户交互,例如选择、设置等。在这种情况下,如果对话框中的内容需要包含列表数据,我们就会使用到`Dialog`嵌套`ListView`的技术。这...
在Android应用开发中,为了帮助用户更好地理解和使用新功能,开发者常常会采用引导页或新手引导来引导用户。本文将详细介绍如何使用自定义Dialog来实现这样的功能,特别针对初次使用APP的用户,通过半透明蒙版高亮...
总结,Android Dialog的基本用法涵盖了多种场景,开发者可以根据需求灵活选择或组合使用。通过学习和实践,我们可以创建出满足应用需求的对话框,提升用户体验。在实际项目中,还可以结合动画、过渡效果等进一步优化...
各种android弹出dialog效果以及各种dialog样式各种android弹出dialog效果以及各种dialog样式各种android弹出dialog效果以及各种dialog样式各种android弹出dialog效果以及各种dialog样式各种android弹出dialog效果...