Android UI设计:AlertDialog
来源:互联网 发布:豌豆公主app数据 编辑:程序博客网 时间:2024/05/21 08:52
AlertDialog(对话框)
一、用法
首先通过AlertDialog.Builder创建出一个AlertDialog的实例
然后可以为这个对话框设置标题、内容、可否取消等属性。
//创建builder AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this); //设置图标、标题、内容、取消、确定等按钮 builder.setIcon(R.mipmap.ic_launcher); builder.setTitle("我是一个标题"); builder.setMessage("我是内容"); builder.setNegativeButton("NegativeButton", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(getApplicationContext(),"NegativeButton",Toast.LENGTH_SHORT).show(); } }); builder..setNeutralButton("NeutralButton", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(getApplicationContext(),"NeutralButton",Toast.LENGTH_SHORT).show(); } }); builder.setPositiveButton("PositiveButton", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(getApplicationContext(),"PositiveButton",Toast.LENGTH_SHORT).show(); } }); //AlertDialog对象 AlertDialog ad=builder.create(); //显示 ad.show();
二、实例
实例一:普通用法,弹出普通alerdialog,跟上面用法的介绍相同
public void show(){ AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this); builder.setIcon(R.mipmap.ic_launcher).setTitle("我是一个标题").setMessage("我是内容").setNegativeButton("NegativeButton", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(getApplicationContext(),"NegativeButton",Toast.LENGTH_SHORT).show(); } }).setNeutralButton("NeutralButton", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(getApplicationContext(),"NeutralButton",Toast.LENGTH_SHORT).show(); } }).setPositiveButton("PositiveButton", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(getApplicationContext(),"PositiveButton",Toast.LENGTH_SHORT).show(); } }); AlertDialog ad=builder.create(); ad.show(); }
实例二:弹出单选框的Dialog,调用builder.setSingleChoiceItems(msexs, 0, new DialogInterface.OnClickListener() {});(匿名内部类)
加上取消与确定按钮
public void singleselect(){ AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this); builder.setSingleChoiceItems(msexs, 0, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { sex=msexs[which]; } }); builder.setNeutralButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(getApplicationContext(), "您的性别是" + sex, Toast.LENGTH_SHORT).show(); mBtn2.setText("您的性别是"+sex); } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { sex="您没有进行性别选择"; Toast.makeText(getApplicationContext(), sex, Toast.LENGTH_SHORT).show(); } }); AlertDialog ad=builder.create(); ad.show(); }
实例三:多选框Dialog,调用 builder.setMultiChoiceItems(hobbys, mIscheckedHobby, new DialogInterface.OnMultiChoiceClickListener()});
public void multiselect(){ AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this); builder.setMultiChoiceItems(hobbys, mIscheckedHobby, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { //which代表选中的是哪个 mIscheckedHobby[which]=isChecked; } }); builder.setNegativeButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { for (int i = 0; i <hobbys.length ; i++) { if(mIscheckedHobby[i]){ hobby.append(hobbys[i]); } } Toast.makeText(getApplicationContext(),"您的爱好是"+hobby,Toast.LENGTH_SHORT).show(); } }); builder.setNeutralButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); AlertDialog ad=builder.create(); ad.show(); }
实例四:可点击Dialog,调用builder.setItems(mData,new DialogInterface.OnClickListener() {});
private void enableselect() { AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this); builder.setItems(mData,new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //which是从0开始的 which=which+1; Toast.makeText(getApplicationContext(),"选中了第"+which+"个",Toast.LENGTH_SHORT).show(); } }); AlertDialog ad=builder.create(); ad.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:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity" android:orientation="vertical"><Button android:id="@+id/button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="弹出一个Dialog"/> <Button android:id="@+id/button2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="弹出一个单选Dialog"/> <Button android:id="@+id/button3" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="弹出一个多选Dialog"/> <Button android:id="@+id/button4" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="弹出一个可选择的Dialog"/></LinearLayout>
MainActivity:
public class MainActivity extends Activity implements View.OnClickListener{ private Button mBtn; private Button mBtn2; private Button mBtn3; private Button mBtn4; private String[] msexs={"男","女","其他"}; private String sex; private String[] hobbys={"篮球","看书","跑步","羽毛球","画画"}; private boolean[] mIscheckedHobby; private StringBuffer hobby=new StringBuffer(); private String[] mData={"第一个可点击","第二个可点击","第三个可点击","第四个可点击"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mIscheckedHobby=new boolean[hobbys.length]; mBtn= (Button) findViewById(R.id.button); mBtn2=(Button) findViewById(R.id.button2); mBtn3=(Button) findViewById(R.id.button3); mBtn4=(Button) findViewById(R.id.button4); mBtn.setOnClickListener(this); mBtn2.setOnClickListener(this); mBtn3.setOnClickListener(this); mBtn4.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.button:{ show(); break; } case R.id.button2:{ singleselect(); break; } case R.id.button3:{ multiselect(); break; } case R.id.button4:{ enableselect(); break; } default:break; } } private void enableselect() { AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this); builder.setItems(mData,new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { which=which+1; Toast.makeText(getApplicationContext(),"选中了第"+which+"个",Toast.LENGTH_SHORT).show(); } }); AlertDialog ad=builder.create(); ad.show(); } public void show(){ AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this); builder.setIcon(R.mipmap.ic_launcher).setTitle("我是一个标题").setMessage("我是内容").setNegativeButton("NegativeButton", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(getApplicationContext(),"NegativeButton",Toast.LENGTH_SHORT).show(); } }).setNeutralButton("NeutralButton", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(getApplicationContext(),"NeutralButton",Toast.LENGTH_SHORT).show(); } }).setPositiveButton("PositiveButton", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(getApplicationContext(),"PositiveButton",Toast.LENGTH_SHORT).show(); } }); AlertDialog ad=builder.create(); ad.show(); } public void singleselect(){ AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this); builder.setSingleChoiceItems(msexs, 0, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { sex=msexs[which]; } }); builder.setNeutralButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(getApplicationContext(), "您的性别是" + sex, Toast.LENGTH_SHORT).show(); mBtn2.setText("您的性别是"+sex); } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { sex="您没有进行性别选择"; Toast.makeText(getApplicationContext(), sex, Toast.LENGTH_SHORT).show(); } }); AlertDialog ad=builder.create(); ad.show(); } public void multiselect(){ AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this); builder.setMultiChoiceItems(hobbys, mIscheckedHobby, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { mIscheckedHobby[which]=isChecked; } }); builder.setNegativeButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { for (int i = 0; i <hobbys.length ; i++) { if(mIscheckedHobby[i]){ hobby.append(hobbys[i]); } } Toast.makeText(getApplicationContext(),"您的爱好是"+hobby,Toast.LENGTH_SHORT).show(); } }); builder.setNeutralButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); AlertDialog ad=builder.create(); ad.show(); }}
0 0
- Android UI设计:AlertDialog
- Android UI 详解之AlertDialog
- Android常用UI之AlertDialog
- Android UI控件之AlertDialog
- Android UI学习 - 对话框 (AlertDialog & ProgressDialog)
- Android UI学习 - 对话框 (AlertDialog & ProgressDialog)
- Android UI学习 - 对话框 (AlertDialog & ProgressDialog)
- Android UI学习 - 对话框 (AlertDialog & ProgressDialog)
- Android学习:用户界面UI之AlertDialog
- Android UI学习 - 对话框 (AlertDialog & ProgressDialog)
- Android UI学习 - 对话框 (AlertDialog & ProgressDialog)
- Android UI学习 - 对话框 (AlertDialog & ProgressDialog)
- Android UI学习 - 对话框 (AlertDialog & ProgressDialog)
- Android UI 之 ProgressBar、ProgressDialog、AlertDialog
- Android笔记:Android UI学习 - 对话框 (AlertDialog & ProgressDialog)
- Android UI中的style和theme以及AlertDialog
- Android UI中的自定义style和theme以及AlertDialog
- [Android新手学习笔记16]-UI控件之AlertDialog
- HDU 1005 Number Sequence(矩阵快速幂)
- Hibernate3使用(二)Spring整合Hibernate,使用xml配置
- 富文本编辑器UMeditor在backbone中的使用
- Toast
- ActiveMQ简介
- Android UI设计:AlertDialog
- IOS学习之单例的实现
- hdu 1106 排序
- 小猪二手车 v4.1 安卓版
- 解决CSS中float:left后需要clear:both清空的繁琐步骤
- WINDOWS API学习笔记之创建线程
- C语言基础知识之(九):函数
- Linux内核工程导论——CGroup子系统
- 杭电 HDU ACM 1015 Safecracker (dfs 枚举排列)