UI控件--AlertDialog
2016-04-02 20:44
507 查看
最简单的Alertdialog
效果图代码如下:
AlertDialog.Builder builder=new AlertDialog.Builder(this); builder.setTitle("请注意!!!"); builder.setIcon(R.mipmap.ic_launcher); builder.setMessage("你确定要删除吗?"); builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "OK", Toast.LENGTH_SHORT).show(); } }); builder.setNegativeButton("cancal", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "cancal", Toast.LENGTH_SHORT).show(); } }); builder.show();
带单选框的AlertDialog
注意无论是带单选框还是带多选框的弹窗,都不可设置setMessage方法,若调用此方法,会导致message内容覆盖选项
效果图如下代码如下
final String[] strings={"女","男"}; AlertDialog.Builder builder=new AlertDialog.Builder(this); builder.setTitle("请注意!!!"); builder.setIcon(R.mipmap.ic_launcher); builder.setSingleChoiceItems(strings, 0, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, strings[which], Toast.LENGTH_SHORT).show(); } }); builder.show();
带多选框的Alertdialog
效果图如下代码如下:
final String[] strings={"斗地主","LOL","打麻将","下象棋"}; final boolean[] isOn=new boolean[strings.length]; AlertDialog.Builder builder=new AlertDialog.Builder(this); builder.setTitle("请注意!!!"); builder.setIcon(R.mipmap.ic_launcher); builder.setMultiChoiceItems(strings, isOn, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { isOn[which]=isChecked; } }); builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String str=""; for (int i=0;i<strings.length;i++){ if (isOn[i]){ str+=strings[i]; } } Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT).show(); } }); builder.show();
自定义Dialog
效果图如下:自定义布局如下
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New Text" android:id="@+id/textView1111" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New Text" android:id="@+id/textView2222" /> </LinearLayout>
代码如下
Dialog dialog1=new Dialog(this); View view1=LayoutInflater.from(this).inflate(R.layout.my_layout,null); TextView textView= (TextView) view1.findViewById(R.id.textView1111); textView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "我是第一个TextView", Toast.LENGTH_SHORT).show(); } }); TextView textView1= (TextView) view1.findViewById(R.id.textView2222); textView1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "我是第2个TextView", Toast.LENGTH_SHORT).show(); } }); dialog1.setTitle("自定义的弹窗"); dialog1.setContentView(view1); dialog1.show();
相关文章推荐
- Android引导动画库:TourGuide
- POJ 1679 The Unique MST 次小生成树
- POJ 1679 The Unique MST 次小生成树
- 第62课:SparkSQL下的Parquet使用最佳实践和代码实践学习笔记
- ubuntu――How to Install MySQL GUI
- 95. Unique Binary Search Trees II
- LC60 Permutation Sequence
- UITableView的介绍及使用
- ubuntu12.04+ros fuerte 跑ORB_SLAM代码
- DuiLib(2)——几种控件的常规属性介绍
- 4 Java基础语法(switch语句,循环语句(for,while),控制跳转语句(break,return,continue))
- HDU - 5288-OO’s Sequence-数学+分类再二分+枚举+contribution costing
- [LeetCode]Implement Stack using Queues
- JS/html/form/XMLHttpRequest Ajax 批量上传文件/图片的3种方式
- POJ1679 The Unique MST
- <%@ include file=” ”%> 和<jsp:include page=” ” flush=”true”/> 区别
- Unique Binary Search Trees
- UITableView的优化
- leetcode 300. Longest Increasing Subsequence 最长上升序列数
- LeetCode334. Increasing Triplet Subsequence