下面代码写了常见的几个对话框,包含:
创新互联专注于企业成都全网营销推广、网站重做改版、苍南网站定制设计、自适应品牌网站建设、H5响应式网站、商城开发、集团公司官网建设、外贸网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为苍南等各大城市提供网站开发制作服务。
1、确定取消对话框
2、单选对话框
3、多选对话框
4、进度对话框(加载时的)
5、下载进度对话框
package com.yuanlp.dialogs; import android.app.ProgressDialog; import android.content.DialogInterface; import android.os.Bundle; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Toast; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } /** * 创建一个确定取消对话框 * @param view */ public void click1(View view){ //工厂设计模式,获取对话框工厂 AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("警告"); builder.setMessage("欲练此功,必先自宫"); builder.setPositiveButton("是", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this,"点击了是",Toast.LENGTH_SHORT).show(); } }); builder.setNegativeButton("否", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this,"不自宫,不成功",Toast.LENGTH_SHORT).show(); } }); //一定要显示show,不然不会显示 builder.show(); } /** * 创建一个单选对话框 * @param view */ public void click2(View view){ //工厂设计模式,获取对话框工厂 final AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("请选择姓名"); final String[] items=new String[]{"男","女","未知"}; builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this,"选择的性别是:"+items[which],Toast.LENGTH_LONG).show(); //关闭对话框 dialog.dismiss(); } }); //一定要显示show,不然不会显示 builder.show(); } /** * 创建一个多选对话框 * @param view */ public void click3(View view){ //工厂设计模式,获取对话框工厂 AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("警告"); final String[] items={"苹果","香蕉","梨子","桃子"}; final boolean[] checkdes={false,true,true,true}; //参数解析:第一个是多选项的一个集合,第二个是每个选项默认的选择状态,第三个是监听 builder.setMultiChoiceItems(items, checkdes, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { Toast.makeText(MainActivity.this,items[which]+isChecked,Toast.LENGTH_SHORT).show(); } }); builder.setPositiveButton("是", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { StringBuffer sb=new StringBuffer(); for(int i=0;i
文章名称:Android常见对话框
转载来于:http://cdkjz.cn/article/jeioeo.html