版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
【移动应用开发技术】Android对话框的几种形式
相关参考:/question/54100_32486?sort=default&p=1#answers工程结构图:activity_main.xml内容:<LinearLayout
xmlns:android="/apk/res/android"
xmlns:tools="/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="普通对话框"
/>
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="3个按钮对话框"
/>
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="输入框"
/>
<Button
android:id="@+id/button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="单选框"
/>
<Button
android:id="@+id/button5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="多选框"
/>
<Button
android:id="@+id/button6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="自定义对话框"
/>
<Button
android:id="@+id/button7"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="时间对话框"
/>
<Button
android:id="@+id/button8"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="进度条"
/>
</LinearLayout>MainActivity.javapackage
cn.lebo.testdialog;
import
android.support.v7.app.ActionBarActivity;
import
android.app.AlertDialog;
import
android.app.AlertDialog.Builder;
import
android.app.DatePickerDialog;
import
android.app.Dialog;
import
android.app.ProgressDialog;
import
android.content.DialogInterface;
import
android.os.Bundle;
import
android.view.*;
import
android.view.View.*;
import
android.widget.*;
public
class
MainActivity
extends
ActionBarActivity
implements
OnClickListener
{
private
Button
btn1
=
null;
private
Button
btn2
=
null;
private
Button
btn3
=
null;
private
Button
btn4
=
null;
private
Button
btn5
=
null;
private
Button
btn6
=
null;
private
Button
btn7
=
null;
private
Button
btn8
=
null;
@Override
protected
void
onCreate(Bundle
savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1
=
(Button)
findViewById(R.id.button1);
btn2
=
(Button)
findViewById(R.id.button2);
btn3
=
(Button)
findViewById(R.id.button3);
btn4
=
(Button)
findViewById(R.id.button4);
btn5
=
(Button)
findViewById(R.id.button5);
btn6
=
(Button)
findViewById(R.id.button6);
btn7
=
(Button)
findViewById(R.id.button7);
btn8
=
(Button)
findViewById(R.id.button8);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
btn4.setOnClickListener(this);
btn5.setOnClickListener(this);
btn6.setOnClickListener(this);
btn7.setOnClickListener(this);
btn8.setOnClickListener(this);
}
@Override
public
boolean
onCreateOptionsMenu(Menu
menu)
{
//
Inflate
the
menu;
this
adds
items
to
the
action
bar
if
it
is
present.
getMenuInflater().inflate(R.menu.main,
menu);
return
true;
}
@Override
public
boolean
onOptionsItemSelected(MenuItem
item)
{
//
Handle
action
bar
item
clicks
here.
The
action
bar
will
//
automatically
handle
clicks
on
the
Home/Up
button,
so
long
//
as
you
specify
a
parent
activity
in
AndroidManifest.xml.
int
id
=
item.getItemId();
if
(id
==
R.id.action_settings)
{
return
true;
}
return
super.onOptionsItemSelected(item);
}
public
void
onClick(View
v)
{
switch(v.getId()){
case
R.id.button1:
dialog1();
break;
case
R.id.button2:
dialog2();
break;
case
R.id.button3:
dialog3();
break;
case
R.id.button4:
dialog4();
break;
case
R.id.button5:
dialog5();
break;
case
R.id.button6:
dialog6();
break;
case
R.id.button7:
dialog7();
break;
case
R.id.button8:
dialog8();
break;
default:
break;
}
}
protected
void
dialog1(){
AlertDialog.Builder
builder
=
new
Builder(MainActivity.this);
builder.setMessage("确认退出吗");
builder.setTitle("提示");
builder.setPositiveButton("确认",
new
DialogInterface.OnClickListener()
{
@Override
public
void
onClick(DialogInterface
dialog,
int
which)
{
dialog.dismiss();
MainActivity.this.finish();
}
});
builder.setNegativeButton("取消",
new
DialogInterface.OnClickListener()
{
@Override
public
void
onClick(DialogInterface
dialog,
int
which)
{
dialog.dismiss();
}
});
builder.create().show();
}
protected
void
dialog2(){
AlertDialog.Builder
builder2
=
new
Builder(MainActivity.this);
builder2.setTitle("明星调查");
builder2.setMessage("您喜欢范冰冰吗?");
builder2.setIcon(R.drawable.cat);
builder2.setPositiveButton("很喜欢",
new
DialogInterface.OnClickListener(){
@Override
public
void
onClick(DialogInterface
dialog,
int
which)
{
Toast.makeText(MainActivity.this,
"我很喜欢范冰冰哦",
Toast.LENGTH_LONG).show();
}
});
builder2.setNegativeButton("不喜欢",
new
DialogInterface.OnClickListener(){
@Override
public
void
onClick(DialogInterface
dialog,
int
which)
{
Toast.makeText(MainActivity.this,
"我不太喜欢她",
Toast.LENGTH_LONG).show();
}
});
builder2.setNeutralButton("一般",
new
DialogInterface.OnClickListener(){
@Override
public
void
onClick(DialogInterface
dialog,
int
which)
{
Toast.makeText(MainActivity.this,
"对他不是很感冒",
Toast.LENGTH_LONG).show();
}
});
builder2.create().show();
}
protected
void
dialog3(){
AlertDialog.Builder
builder3
=
new
Builder(MainActivity.this);
builder3.setTitle("请输入信息");
builder3.setIcon(R.drawable.warn);
builder3.setView(new
EditText(this));
builder3.setPositiveButton("确定",
null);
builder3.setNegativeButton("取消",
null);
builder3.create().show();
}
protected
void
dialog4(){
AlertDialog.Builder
builder4
=
new
Builder(MainActivity.this);
builder4.setTitle("单选框");
builder4.setIcon(R.drawable.warn);
builder4.setSingleChoiceItems(new
String[]{"宋茜","古力娜扎"},
0,
new
DialogInterface.OnClickListener(){
@Override
public
void
onClick(DialogInterface
dialog,
int
which)
{
dialog.dismiss();
}});
builder4.setNegativeButton("取消",
null);
builder4.create().show();
}
protected
void
dialog5(){
AlertDialog.Builder
builder5
=
new
Builder(MainActivity.this);
builder5.setTitle("您喜欢哪些明星?");
builder5.setMultiChoiceItems(new
String[]
{"范冰冰","高圆圆","Angelababy","章子怡","李冰冰","周迅"},
null,
null);
builder5.setPositiveButton("确定",
null);
builder5.setNegativeButton("取消",
null);
builder5.create().show();
}
protected
void
dialog6(){
LayoutInflater
layoutinflater
=
getLayoutInflater();
View
view
=
layoutinflater.inflate(R.layout.login,
null);
EditText
edit_user
=
(EditText)
view.findViewById(R.id.edit_user);
EditText
edit_passwd
=
(EditText)
view.findViewById(R.id.edit_passwd);
final
String
user_name
=
edit_user.getText().toString();
final
String
pass_wd
=
edit_passwd.getText().toString();
Builder
dialog=new
AlertDialog.Builder(MainActivity.this);
dialog.setTitle("用户登陆");
dialog.setMessage("登陆");
dialog.setView(view);
dialog.setPositiveButton("登陆",
new
DialogInterface.OnClickListener(){
@Override
public
void
onClick(DialogInterface
dialog,
int
which)
{
if(user_name.equals("water")&&
pass_wd.equals("123abc")){
Toast.makeText(MainActivity.this,
"登陆成功",
Toast.LENGTH_LONG).show();
}else{
Toast.makeText(MainActivity.this,
"登陆失败",
Toast.LENGTH_LONG).show();
}
}
});
dialog.setNegativeButton("取消",
new
DialogInterface.OnClickListener(){
@Override
public
void
onClick(DialogInterface
dialog,
int
which)
{
//
TODO
Auto-generated
method
stub
}
});
dialog.create();
dialog.show();
}
protected
void
dialog7(){
Dialog
dialog
=
new
DatePickerDialog(MainActivity.this,
new
DatePickerDialog.OnDateSetListener(){
@Override
public
void
onDateSet(DatePicker
view,
int
year,
int
monthOfYear,
int
dayOfMonth)
{
Toast.makeText(MainActivity.this,
"您选择的时间是:
"
+
year
+"年"+
monthOfYear
+
"月"
+
dayOfMonth
+
"日"
,
Toast.LENGTH_LONG).show();
}
},
2015,
8,
1);
dialog.show();
}
protected
void
dialog8(){
final
ProgressDialog
dialog
=
ProgressDialog.show(MainActivity.this,
"正在搜索网络",
"请稍候");
new
Thread(){
public
void
run(){
try{
Thread.sleep(3000);
}catch(InterruptedException
e){
e.printStackTrace();
}
finally{
dialog.dismiss();
}
}
}.start();
}
}其中自定义对话框布局文件login.xml代码:<?xml
version="1.0"
encoding="utf-8"?>
<LinearLayout
xmlns:android="/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 高中历史(高二)自主导学课堂教学改革实践:《历史核心素养导向的知识建构与思维培育》教学设计
- 护理学科 高职大二《急性结石性胆囊炎患者护理》教学设计
- 立足单元整合促进语用生成-人教版(一年级起点)三年级英语上册 Revision 1 Lesson 3 综合复习课教学设计
- 高中生物《血红蛋白的提取与分离》教学设计
- 2026广东广州市农业科学院水稻研究所招聘科研辅助人员1人备考题库参考答案详解
- 2025江西南昌市劳动保障事务代理中心招聘2人备考题库及1套完整答案详解
- 2025贵州铜仁市德江县消防救援大队冬季招聘政府专职消防员30人备考题库及完整答案详解
- 2026山东鲁南发展投资控股(枣庄)集团有限公司招聘急需紧缺人才1人备考题库带答案详解
- 2026云南昆明理工大学附属高级中学地理物理学科教师招聘2人备考题库含答案详解
- 2026上海中医药大学国际教育学院英语教师招聘1人备考题库及完整答案详解一套
- T∕ZZB 0623-2018 有机溶剂型指甲油
- 2025体彩知识考试题及答案
- 机械企业安全生产风险评估报告
- 马匹性能智能评估-洞察及研究
- 中职班会课主题课件
- 政务服务大厅安全隐患排查
- 土建资料管理课件
- 钣金检验作业指导书
- 公司安全大讲堂活动方案
- 2025年江苏省无锡市梁溪区八下英语期末统考模拟试题含答案
- GB/T 42186-2022医学检验生物样本冷链物流运作规范
评论
0/150
提交评论