版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
【移动应用开发技术】怎么在Android中实现一个对话框
怎么在Android中实现一个对话框?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。2个按钮public
class
MainActivity
extends
Activity
{
@Override
protected
void
onCreate(Bundle
savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button
buttonNormal
=
(Button)
findViewById(R.id.button_normal);
buttonNormal.setOnClickListener(new
View.OnClickListener()
{
@Override
public
void
onClick(View
v)
{
showNormalDialog();
}
});
}
private
void
showNormalDialog(){
/*
@setIcon
设置对话框图标
*
@setTitle
设置对话框标题
*
@setMessage
设置对话框消息提示
*
setXXX方法返回Dialog对象,因此可以链式设置属性
*/
final
AlertDialog.Builder
normalDialog
=
new
AlertDialog.Builder(MainActivity.this);
normalDialog.setIcon(R.drawable.icon_dialog);
normalDialog.setTitle("我是一个普通Dialog")
normalDialog.setMessage("你要点击哪一个按钮呢?");
normalDialog.setPositiveButton("确定",
new
DialogInterface.OnClickListener()
{
@Override
public
void
onClick(DialogInterface
dialog,
int
which)
{
//...To-do
}
});
normalDialog.setNegativeButton("关闭",
new
DialogInterface.OnClickListener()
{
@Override
public
void
onClick(DialogInterface
dialog,
int
which)
{
//...To-do
}
});
//
显示
normalDialog.show();
}
}3个按钮/*
@setNeutralButton
设置中间的按钮
*
若只需一个按钮,仅设置
setPositiveButton
即可
*/
private
void
showMultiBtnDialog(){
AlertDialog.Builder
normalDialog
=
new
AlertDialog.Builder(MainActivity.this);
normalDialog.setIcon(R.drawable.icon_dialog);
normalDialog.setTitle("我是一个普通Dialog").setMessage("你要点击哪一个按钮呢?");
normalDialog.setPositiveButton("按钮1",
new
DialogInterface.OnClickListener()
{
@Override
public
void
onClick(DialogInterface
dialog,
int
which)
{
//
...To-do
}
});
normalDialog.setNeutralButton("按钮2",
new
DialogInterface.OnClickListener()
{
@Override
public
void
onClick(DialogInterface
dialog,
int
which)
{
//
...To-do
}
});
normalDialog.setNegativeButton("按钮3",
new
DialogInterface.OnClickListener()
{
@Override
public
void
onClick(DialogInterface
dialog,
int
which)
{
//
...To-do
}
});
//
创建实例并显示
normalDialog.show();
}2.2列表Dialog(图3)private
void
showListDialog()
{
final
String[]
items
=
{
"我是1","我是2","我是3","我是4"
};
AlertDialog.Builder
listDialog
=
new
AlertDialog.Builder(MainActivity.this);
listDialog.setTitle("我是一个列表Dialog");
listDialog.setItems(items,
new
DialogInterface.OnClickListener()
{
@Override
public
void
onClick(DialogInterface
dialog,
int
which)
{
//
which
下标从0开始
//
...To-do
Toast.makeText(MainActivity.this,
"你点击了"
+
items[which],
Toast.LENGTH_SHORT).show();
}
});
listDialog.show();
}2.3单选Dialog(图4)int
yourChoice;
private
void
showSingleChoiceDialog(){
final
String[]
items
=
{
"我是1","我是2","我是3","我是4"
};
yourChoice
=
-1;
AlertDialog.Builder
singleChoiceDialog
=
new
AlertDialog.Builder(MainActivity.this);
singleChoiceDialog.setTitle("我是一个单选Dialog");
//
第二个参数是默认选项,此处设置为0
singleChoiceDialog.setSingleChoiceItems(items,
0,
new
DialogInterface.OnClickListener()
{
@Override
public
void
onClick(DialogInterface
dialog,
int
which)
{
yourChoice
=
which;
}
});
singleChoiceDialog.setPositiveButton("确定",
new
DialogInterface.OnClickListener()
{
@Override
public
void
onClick(DialogInterface
dialog,
int
which)
{
if
(yourChoice
!=
-1)
{
Toast.makeText(MainActivity.this,
"你选择了"
+
items[yourChoice],
Toast.LENGTH_SHORT).show();
}
}
});
singleChoiceDialog.show();
}2.4多选Dialog(图5)ArrayList<Integer>
yourChoices
=
new
ArrayList<>();
private
void
showMultiChoiceDialog()
{
final
String[]
items
=
{
"我是1","我是2","我是3","我是4"
};
//
设置默认选中的选项,全为false默认均未选中
final
boolean
initChoiceSets[]={false,false,false,false};
yourChoices.clear();
AlertDialog.Builder
multiChoiceDialog
=
new
AlertDialog.Builder(MainActivity.this);
multiChoiceDialog.setTitle("我是一个多选Dialog");
multiChoiceDialog.setMultiChoiceItems(items,
initChoiceSets,
new
DialogInterface.OnMultiChoiceClickListener()
{
@Override
public
void
onClick(DialogInterface
dialog,
int
which,
boolean
isChecked)
{
if
(isChecked)
{
yourChoices.add(which);
}
else
{
yourChoices.remove(which);
}
}
});
multiChoiceDialog.setPositiveButton("确定",
new
DialogInterface.OnClickListener()
{
@Override
public
void
onClick(DialogInterface
dialog,
int
which)
{
int
size
=
yourChoices.size();
String
str
=
"";
for
(int
i
=
0;
i
<
size;
i++)
{
str
+=
items[yourChoices.get(i)]
+
"
";
}
Toast.makeText(MainActivity.this,
"你选中了"
+
str,
Toast.LENGTH_SHORT).show();
}
});
multiChoiceDialog.show();
}2.5等待Dialog(图6)private
void
showWaitingDialog()
{
/*
等待Dialog具有屏蔽其他控件的交互能力
*
@setCancelable
为使屏幕不可点击,设置为不可取消(false)
*
下载等事件完成后,主动调用函数关闭该Dialog
*/
ProgressDialog
waitingDialog=
new
ProgressDialog(MainActivity.this);
waitingDialog.setTitle("我是一个等待Dialog");
waitingDialog.setMessage("等待中...");
waitingDialog.setIndeterminate(true);
waitingDialog.setCancelable(false);
waitingDialog.show();
}2.6进度条Dialog(图7)private
void
showProgressDialog()
{
/*
@setProgress
设置初始进度
*
@setProgressStyle
设置样式(水平进度条)
*
@setMax
设置进度最大值
*/
final
int
MAX_PROGRESS
=
100;
final
ProgressDialog
progressDialog
=
new
ProgressDialog(MainActivity.this);
progressDialog.setProgress(0);
progressDialog.setTitle("我是一个进度条Dialog");
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMax(MAX_PROGRESS);
progressDialog.show();
/*
模拟进度增加的过程
*
新开一个线程,每个100ms,进度增加1
*/
new
Thread(new
Runnable()
{
@Override
public
void
run()
{
int
progress=
0;
while
(progress
<
MAX_PROGRESS){
try
{
Thread.sleep(100);
progress++;
progressDialog.setProgress(progress);
}
catch
(InterruptedException
e){
e.printStackTrace();
}
}
//
进度达到最大值后,窗口消失
progressDialog.cancel();
}
}).start();
}2.7编辑Dialog(图8)private
void
showInputDialog()
{
/*@setView
装入一个EditView
*/
final
EditText
editText
=
new
EditText(MainActivity.this);
AlertDialog.Builder
inputDialog
=
new
AlertDialog.Builder(MainActivity.this);
inputDialog.setTitle("我是一个输入Dialog").setView(editText);
inputDialog.setPositiveButton("确定",
new
DialogInterface.OnClickListener()
{
@Override
public
void
onClick(DialogInterface
dialog,
int
which)
{
Toast.makeText(MainActivity.this,
editText.getText().toString(),
Toast.LENGTH_SHORT).show();
}
}).show();
}2.8自定义Dialog(图9)<!--
res/layout/dialog_customize.xml-->
<!--
自定义View
-->
<LinearLayout
xmlns:android="/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>private
void
showCustomizeDialog()
{
/*
@setView
装入自定义View
==>
R.layout.dialog_customize
*
由于dialog_customize.xml只放置了一个EditView,因此和图8一样
*
dialog_customize.xml可自定义更复杂的View
*/
AlertDialog.Builder
customizeDialog
=
new
AlertDialog.Builder(MainActivity.this);
final
View
dialogView
=
LayoutInflater.from(MainActivity.this)
.inflate(R.layout.dialog_customize,null);
customizeDialog.setTitle("我是一个自定义Dialog");
customizeDialog.setView(dialogView);
customizeDialog.setPositiveButton("确定",
new
DialogInterface.OnClickListener()
{
@Override
public
void
onClick(DialogInterface
dialog,
int
which)
{
//
获取EditView中的输入内容
EditText
edit_text
=
(EditText)
dialogView.findViewById(R.id.edit_text);
Toast.makeText(MainActivity.this,
edit_text.getText().toString(),
Toast.LENGTH_SHORT).show();
}
});
customizeDialog.show();
}3.复写回调函数/*
复写Builder的create和show函数,可以在Dialog显示前实现必要设置
*
例如初始化列表、默认选项等
*
@create
第一次创建时调用
*
@show
每次显示时调用
*/
private
void
showListDialog()
{
final
S
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 福建省龙岩市武平一中、长汀一中、漳平一中等六校2024年物理高一第二学期期末统考模拟试题含解析
- 2024年《高三》观后感高三的观后感
- 英文词、汉字词、早期文字和图画的认知加工比较
- 2021年秋季自考电子商务专业本科电子商务网站设计原理试题含解析
- 山东省微山县2024届中考三模数学试题含解析
- 山东省威海市环翠区2024年中考物理猜题卷含解析
- 全国2021年上半年高等教育自学考试电子商务网站设计原理考题含解析
- 山东省日照市东港区市级名校2023-2024学年中考化学四模试卷含解析
- 山东省临沂市沂水县2024届中考二模化学试题含解析
- 山东省临沂市兰山区市级名校2023-2024学年中考化学仿真试卷含解析
- 相变蓄热球体堆积床传热模型及热性能分析
- 胃积病(胃癌)中医诊疗方案分析、总结、评估报告
- 国家级文明旅游示范单位申请评定报告书(自评报告)
- 电磁场与电磁波完整版课件全套PPT教程最全电子教案
- 香港与内地报表转换
- 民营整形医院与公立整形医院的区别
- 临时板房施工组织设计
- 最新北师大版六年级数学下册期中测试卷含答案
- 国家开放大学《数据结构(本)》单元测试参考答案
- 国家开放大学《土木工程力学(本)》形考作业1-5参考答案
- GB 13544-2011 烧结多孔砖和多孔砌块.pdf
评论
0/150
提交评论