首页 > 代码库 > Android-Dialog对话框Demo
Android-Dialog对话框Demo
代码
package com.lxt008;import android.app.Activity;import android.app.AlertDialog;import android.app.Dialog;import android.app.ProgressDialog;import android.content.DialogInterface;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;public class Activity01 extends Activity { ProgressDialog m_Dialog; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Dialog dialog = new AlertDialog.Builder(Activity01.this) .setTitle("登陆提示")//设置标题 .setMessage("这里需要登录!")//设置内容 .setPositiveButton("确定",//设置确定按钮 new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { //点击“确定”转向登陆框 LayoutInflater factory = LayoutInflater.from(Activity01.this); //得到自定义对话框 final View DialogView = factory.inflate(R.layout.dialog, null); //创建对话框 AlertDialog dlg = new AlertDialog.Builder(Activity01.this) .setTitle("登录框") .setView(DialogView)//设置自定义对话框的样式 .setPositiveButton("确定", //设置"确定"按钮 new DialogInterface.OnClickListener() //设置事件监听 { public void onClick(DialogInterface dialog, int whichButton) { //输入完成后,点击“确定”开始登陆 m_Dialog = ProgressDialog.show ( Activity01.this, "请等待...", "正在为你登录...", true ); new Thread() { public void run() { try { sleep(3000); } catch (Exception e) { e.printStackTrace(); } finally { //登录结束,取消m_Dialog对话框 m_Dialog.dismiss(); } } }.start(); } }) .setNegativeButton("取消", //设置“取消”按钮 new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { //点击"取消"按钮之后退出程序 Activity01.this.finish(); } }) .create();//创建 dlg.show();//显示 } }).setNeutralButton("退出", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { //点击"退出"按钮之后推出程序 Activity01.this.finish(); } }).create();//创建按钮 // 显示对话框 dialog.show(); }}
布局文件1
<?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:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /></LinearLayout>
布局文件2
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/username" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_marginLeft="20dip" android:layout_marginRight="20dip" android:text="账号" android:gravity="left" android:textAppearance="?android:attr/textAppearanceMedium" /> <EditText android:id="@+id/username" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_marginLeft="20dip" android:layout_marginRight="20dip" android:scrollHorizontally="true" android:autoText="false" android:capitalize="none" android:gravity="fill_horizontal" android:textAppearance="?android:attr/textAppearanceMedium" /> <TextView android:id="@+id/password" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_marginLeft="20dip" android:layout_marginRight="20dip" android:text="密码" android:gravity="left" android:textAppearance="?android:attr/textAppearanceMedium" /> <EditText android:id="@+id/password" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_marginLeft="20dip" android:layout_marginRight="20dip" android:scrollHorizontally="true" android:autoText="false" android:capitalize="none" android:gravity="fill_horizontal" android:password="true" android:textAppearance="?android:attr/textAppearanceMedium" /> </LinearLayout>
Android-Dialog对话框Demo
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。