android中Dialog的使用
来源:互联网 发布:在淘宝买片怎么搜 编辑:程序博客网 时间:2024/05/28 16:14
各种Dialog的使用:
public class MainActivity extends Activity {
//单选项的数组
private String[] singleItems=new String[]{"上网","游戏","看电影"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void nomaldialog(View view){
// AlertDialog.Builder builder=new Builder(this);
// //设置对话框的相关参数
// builder.setTitle("普通对话框");
// builder.setIcon(R.drawable.ic_launcher);
// builder.setMessage("信息");
// //创建对话框对象
// AlertDialog dialog=builder.create();
AlertDialog dialog=new Builder(this)
.setTitle("普通对话框")
.setIcon(R.drawable.ic_launcher)
.setMessage("信息")
.create();
//显示
dialog.show();
}
public void buttondialog(View view){
AlertDialog dialog=new Builder(this)
.setIcon(R.drawable.ic_launcher)
.setTitle("带按钮的对话框")
.setMessage("消息")
.setPositiveButton("确认", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "确认按钮己被点击", Toast.LENGTH_LONG).show();
}
})
.setNegativeButton("取消", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "取消按钮己被点击", Toast.LENGTH_LONG).show();
}
})
.setNeutralButton("忽略", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "忽略按钮己被点击", Toast.LENGTH_LONG).show();
}
})
.create();
dialog.show();
}
public void singledialog(View view){
AlertDialog dialog=new Builder(this)
.setTitle("选择爱好")
.setIcon(R.drawable.ic_launcher)
.setSingleChoiceItems(singleItems, -1, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "选定的爱好:"+singleItems[which], Toast.LENGTH_LONG).show();
dialog.cancel();
}
})
.create();
dialog.show();
}
public void multidialog(View view){
AlertDialog dialog=new Builder(this)
.setTitle("选择爱好")
.setIcon(R.drawable.ic_launcher)
.setCancelable(false)
.setMultiChoiceItems(singleItems, new boolean[]{true,false,false}, new OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
//判断当前点击的选项是否被选定了
if (isChecked) {
Toast.makeText(MainActivity.this, singleItems[which]+"被选定了", Toast.LENGTH_SHORT).show();
}
}
})
.setPositiveButton("设置", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//关闭对话框
dialog.dismiss();
}
})
.create();
dialog.show();
}
}
布局文件代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.shen.MainActivity" >
<Button
android:onClick="nomaldialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="普通对话框" />
<Button
android:onClick="buttondialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="带按钮的对话框" />
<Button
android:onClick="singledialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="带单选项的对话框" />
<Button
android:onClick="multidialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="带多选项的对话框" />
</LinearLayout>
//通过点击按钮来显示不同的Dialog
- Android中Dialog的使用
- Android中Dialog的使用
- Android中Dialog的使用
- Android中Dialog的使用
- Android中Dialog的使用
- Android中Dialog的使用
- android中dialog的使用
- Android中Dialog的使用
- Android中Dialog的使用
- Android 中dialog的使用
- Android中Dialog的使用
- Android中dialog的使用
- Android中Dialog的使用
- android中Dialog的使用
- Android中Dialog的使用
- Android中对话框(dialog)的使用
- Android开发中Dialog对话框的使用
- Android开发中Dialog对话框的使用
- 菜鸟教程 Java 学习笔记 (基础教程)
- 这个单例足够单
- 小米手机无法安装apk INSTALL_CANCELED_BY_USER
- 新手引导
- Java BigDecimal和double
- android中Dialog的使用
- 【实战】使用Java结合Sendmail邮件服务器+Quartz开源框架定时发邮件
- analyse K9F2G08X0A nand flash
- 蓝桥杯 算法提高 9-2 文本加密
- android时间对话框TimePickerDialog介绍
- mac系统中使用tree 命令展示目录树结构
- Mongo 初识
- Spark入门实战系列--6.SparkSQL(上)--SparkSQL简介
- leetcode 51 N-Queens 回溯 万能结构