Android UI设计:AlertDialog
2015-08-27 19:46
661 查看
AlertDialog(对话框)
一、用法
首先通过AlertDialog.Builder创建出一个AlertDialog的实例然后可以为这个对话框设置标题、内容、可否取消等属性。
[code] //创建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,跟上面用法的介绍相同
[code] 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() {});(匿名内部类)
加上取消与确定按钮[code] 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()});
[code] 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() {});
[code] 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(); }
完整代码:
布局:四个按钮[code]<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:
[code]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(); } }
相关文章推荐
- iOS-UIViewController的UIView生命周期
- iOS:步进UIStepper、滑动块UISlider、开关UISwitch的基本使用
- UITaleViewCell
- uitextfield 当设置为selectedAll 时不让 复制、粘帖那几项自动出来的解决办法
- Building a Space Station(POJ--2031【最小生成树】
- UITableView的编辑
- 08-25 convertView、ViewHolder、MyfruitPractice、divider、Header
- JSF 2 hidden value example
- UIImageView添加圆角图片
- Ngui如何弹出提示消息,并淡出【支持多条】
- 8.8.1 Optimizing Queries with EXPLAIN
- amaze UI animation动画示例
- ValueError: setting an array element with a sequence.
- ValueError: setting an array element with a sequence.
- 如何访问UITextField内的值
- pcDUino 卡刷实现
- HBuilder快捷键
- 【UISlider滑动控件】
- UISearchBar修改输入框,取消按钮样式
- WM_QUIT,WM_CLOSE,WM_DESTROY