当前位置: 首页 > news >正文

java android对话框_android 对话框Dialog和AlertDialog应用 | 学步园

1:第一种简单应用对话框。

先新建android  project

修改AndroidManifest.xml文件activity的属性代码:

android:name=".MainActivity"

android:label="@string/title_activity_main"

android:theme="@android:style/Theme.Dialog"

>

结果运行图如下

1347969264_4593.png

2:单击按钮时显示出一个对话框

布局Layout下的activity_mian.xml代码

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent" >

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerHorizontal="true"

android:layout_centerVertical="true"

android:text="@string/look"

tools:context=".MainActivity" />

string.xml代码

tds

欢迎来到对话框!

查看AlertDialog对话框!

Settings

MainActivity

这里是AlertDialog对话框!

编写人:某某

确定

取消

MainActivity.java代码

import android.os.Bundle;

import android.app.Activity;

import android.app.AlertDialog;

import android.content.DialogInterface;

import android.content.DialogInterface.OnClickListener;

import android.view.Menu;

import android.view.View;

import android.widget.Button;

import android.widget.Toast;

public class MainActivity extends Activity {

//定义组件

private Button button;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

//查找button组件button = (Button) this.findViewById(R.id.button);

//点击按钮触发显示Dialog事件

button.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {AlertDialog.Builder builder = new AlertDialog.Builder(

MainActivity.this);

//对话框的标题

builder.setTitle(R.string.s);

//显示的信息内容

builder.setMessage(R.string.message);

//设置android内中的图标库为对话框图标

builder.setIcon(android.R.drawable.ic_menu_help);

builder.setPositiveButton(R.string.ok, new OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

//点击确定时弹出文本域提示信息

Toast.makeText(MainActivity.this, "欢迎查看对话框",

Toast.LENGTH_LONG).show();

}

});

builder.setNegativeButton(R.string.cancal,

new OnClickListener() {

@Override

public void onClick(DialogInterface dialog,

int which) {

//点击取消时退出程序

finish();

}

});

builder.show();

}});

}

运行效果:

1347975505_3832.png

1347975513_2642.png

1347975517_8567.png

3:将布局文件设置到对话框里面

(1)实现xml文件变为dialog里面的内容

(2)ProgressDialog进程对话框的简单应用

activity_main.xml文件

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent" >

android:id="@+id/login"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerHorizontal="true"

android:layout_marginTop="100dp"

android:text="@string/login" />

新建xml登录布局文件用于弹出对话框用户输入用户名和密码login.xml

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical" >

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/inputname" />

android:id="@+id/user"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:inputType="text" >

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/inputpass" />

android:id="@+id/password"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:ems="10"

android:inputType="textPassword" >

string.xml代码;

DialogTest

进入登录页面

Settings

主界面

请输入你的用户名:

请输入你的密码:

Hello world!

登录成功页

Main_Activity.java代码

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

//查找登录按钮组件login = (Button) this.findViewById(R.id.login);

//注册监听,点击按钮打开登录对话框

login.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

//显示登录窗口showDialog(LOGIN);

}

});

}

创建对话框:

@Override

protected Dialog onCreateDialog(int id) {

//LayoutInflater将xml的布局文件变成一个View实例

LayoutInflater inflater=LayoutInflater.from(this);

//布局为logindialog实例

View logindialog=inflater.inflate(R.layout.login, null);

//NEW出alertdialog

AlertDialog.Builder builder=new AlertDialog.Builder(this);

builder.setIcon(android.R.drawable.ic_menu_help).setTitle("请输入").setView(logindialog);

//查找已布局到对话框内View--logindialog的用户名和密码组件

name=(EditText) logindialog.findViewById(R.id.user);

pass=(EditText) logindialog.findViewById(R.id.password);

builder.setPositiveButton("登录", new DialogInterface.OnClickListener() {

//点击登录时事件处理

@Override

public void onClick(DialogInterface dialog, int which) {

System.out.println(name.getText());

System.out.println(pass.getText());

//转化为String类型,否则出现空指针异常

if(name.getText().toString().equals("admin")&&pass.getText().toString().equals("123")){

//创建进程对话框

ProgressDialog pro=ProgressDialog.show(MainActivity.this, "请等待","正在为你登录……", true);

try {

//进程休眠三秒钟

Thread.sleep(3000);

} catch (InterruptedException e) {

e.printStackTrace();

}

Intent intent=new Intent();

intent.setClass(MainActivity.this,SuccessActivity.class);

//将登录的用户名传到另一个activity——SuccessActivity

intent.putExtra("username", name.getText().toString());

//必须转化为String类型,否则出现空指针

startActivity(intent);

}

else

Toast.makeText(MainActivity.this, "登录失败!!!\n用户名或密码不正确", Toast.LENGTH_LONG).show();

}

});

builder.setNegativeButton("取消", new DialogInterface.OnClickListener(){

//取消登录时退出登录

@Override

public void onClick(DialogInterface dialog, int which) {

removeDialog(LOGIN);//关闭对话框

}

});

return builder.create();

}

activity_success.xml布局文件代码:

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent" >

android:id="@+id/showuser"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerHorizontal="true"

android:padding="@dimen/padding_medium"

tools:context=".SuccessActivity" />

Success_Activity.java代码

public class SuccessActivity extends Activity {

private TextView showinfo;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_success);

showinfo=(TextView) this.findViewById(R.id.showuser);

//获取定义的意图,得到传的参数值

Intent i=this.getIntent();

showinfo.setText("登录成功!!!\n欢迎"+i.getStringExtra("username")+"登录!");

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

getMenuInflater().inflate(R.menu.activity_success, menu);

return true;

}

}

运行效果:

1349419072_8481.png

1349419081_1125.png

用户名及密码不正确或是有空值时登录失败,不跳转其他页面

1349419088_5294.png

输入正确时显示进程对话框,并进行登录跳转成功页面

1349419095_8652.png

1349419106_2410.png

http://www.lryc.cn/news/570361.html

相关文章:

  • 船舶燃料电池电力推进系统设计报告:300kW 系统方案
  • [WIFI]破解工具-BT4+unetbootin+spoonwep/wpa
  • 使用NMEA Tools生成GPS轨迹图
  • c++ algorithm常用算法汇总
  • android 各版本占比,谷歌公布最新Android各版本占比数据:9.0份额超过10%
  • 华尔街英语学习软件_哪些比较好的英语学习软件?
  • Eclipse 【3.4】 版本安装【插件】时的【dropins】 目录
  • Discuz!教程之图片友情链接横排的美化方法
  • 生活小窍门[转]
  • DataWorks 离线同步数据至 Kafka 实操
  • Vue.js知识——Promise、vuex
  • 尚硅谷Java零基础全套视频教程(宋红康2023版,java入门自学必备)
  • 基于Verilog的简易咖啡机设计
  • 洛谷 排队接水 贪心
  • 2005年度世界500强公司名单[转]
  • 【从零学视觉】(一) 认识工业相机
  • leetcode25-K个一组翻转链表
  • 赣网杯2021 CTF---MiscWebCrypto部分Writeup
  • 组合模式Composite Pattern
  • python爬取美女图片
  • Python开发串口通讯上位机程序三部曲 第二部:保存数据
  • UI前端大数据可视化:从设计到实现的完整流程
  • 牙科医疗设备EMC电磁兼容技术讨论
  • 《Linux驱动:nand flash驱动看这一篇就够了》
  • OmniMeetProTrack 全维会议链智能追录系统——山东大学软件学院创新实训项目博客(六)
  • CS研究生学习阶段必读书籍
  • Python高效操作MySQL数据库
  • c++ 完美转发
  • 零基础学前端-传统前端开发(第四期-JS基础-语法,语句)
  • 安卓玩java模拟器_安卓系统智能手机玩JAVA游戏!JAVA模拟器让你痛快地玩!