zoukankan      html  css  js  c++  java
  • Android中九种dialog对话框代码

    public class MainActivity extends Activity {
    
    	private static final int MAX_PROGRESS = 100;
    	private static final int PRO = 10;
    	private Handler handler;
    
    	private int progress=10;
    	private ProgressDialog progressDialog;
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		//显示视图
    		setContentView(R.layout.activity_main);
    
    	}
    
    	public void openDialog(View v) {
    		
    		//调用 方法
    		//test1();
    		tes8();
    		//myDialog();
    
    	}
    
    	public void test1() {
    		// 创建对话框对象
    		AlertDialog alertDialog = new AlertDialog.Builder(this).create();
    
    		// 设置对话框的标题
    		alertDialog.setTitle("XXXXX");
    		// 设置对话框中的内容
    		alertDialog.setMessage("消息");
    		// 显示对话框
    		alertDialog.show();
    	}
    
    	public void tes2() {
    		AlertDialog alertDialog = new AlertDialog.Builder(this)
    				.setTitle("xxxx").setMessage("xxxx").show();
    
    	}
    
    	// 对话框
    	public void tes3() {
    		new AlertDialog.Builder(this)
    				.setIcon(R.drawable.ic_launcher)
    				.setTitle("xxxx")
    				.setMessage("是否创建文件")
    				.setPositiveButton("确认", new DialogInterface.OnClickListener() {
    
    					@Override
    					public void onClick(DialogInterface dialog, int which) {
    						// 创建文件了
    						new AlertDialog.Builder(MainActivity.this).setMessage(
    								"文件已经被创建").show();
    					}
    				})
    				.setNegativeButton("取消", new DialogInterface.OnClickListener() {
    
    					@Override
    					public void onClick(DialogInterface dialog, int which) {
    						new AlertDialog.Builder(MainActivity.this)
    								.setMessage("您已经选择了取消的按钮,该文件不会被创建").create()
    								.show();
    
    					}
    				}).show();
    	}
    
    	public void tes4() {
    		final String items[] = { "Java", "PHP", "3G", ".NET" };
    
    		new AlertDialog.Builder(this).setTitle("简单列表对话框")
    				.setItems(items, new DialogInterface.OnClickListener() {
    
    					@Override
    					public void onClick(DialogInterface dialog, int which) {
    						// 第一个参数 dialog int which 那个条目
    						Toast.makeText(getApplicationContext(),
    								"xxxxx" + items[which], Toast.LENGTH_LONG)
    								.show();
    
    					}
    				}).show();
    	}
    
    	public void tes5() {
    		final String items[] = { "Java", "PHP", "3G", ".NET" };
    
    		new AlertDialog.Builder(this).setTitle("单选列表对话框")
    
    		// 数据集中的某一列会作为列表对话框的数据加载的列表框中,该参数表示该列的名称(字段名称)
    				.setSingleChoiceItems(items, 1,
    						new DialogInterface.OnClickListener() {
    
    							@Override
    							public void onClick(DialogInterface dialog,
    									int which) {
    								// 第一个参数 dialog int which 那个条目
    								Toast.makeText(getApplicationContext(),
    										"xxxxx" + items[which],
    										Toast.LENGTH_LONG).show();
    
    							}
    						}).show();
    
    	}
    
    	public void tes6() {
    
    		final String items[] = { "Java", "PHP", "3G", ".NET" };
    
    		new AlertDialog.Builder(this)
    				.setTitle("多选列表对话框")
    			
    				.setMultiChoiceItems(items,
    						new boolean[] { false, true, true, false },
    						new DialogInterface.OnMultiChoiceClickListener() {
    
    							@Override
    							public void onClick(DialogInterface dialog,
    									int which, boolean isChecked) {
    
    								if (isChecked) {
    									Toast.makeText(getApplicationContext(),
    											"xxx" + items[which],
    											Toast.LENGTH_LONG).show();
    								}
    
    							}
    						})
    				.setPositiveButton("确认", new DialogInterface.OnClickListener() {
    
    					@Override
    					public void onClick(DialogInterface dialog, int which) {
    						Toast.makeText(getApplicationContext(), "确认",
    								Toast.LENGTH_LONG).show();
    					}
    				}).show();
    
    	}
    	
    	
    	//自定义的对话框
    	public void myDialog(){
    		LayoutInflater  layoutInflater = getLayoutInflater();
    		View view = layoutInflater.inflate(R.layout.activity_main, null); //R.layout.activty_main自定义的布局文件
    		new AlertDialog.Builder(this).setView(view).setTitle("自定义的对话框").setPositiveButton("确认按钮", new DialogInterface.OnClickListener() {
    			
    			@Override
    			public void onClick(DialogInterface dialog, int which) {
    				//处理
    				
    			}
    		}).show();
    	}
    	
    	
    	//进度条对话框
    	public void tes8(){
    		handler = new Handler() {
    
    			@Override
    			public void handleMessage(Message msg) {
    
    				super.handleMessage(msg);
    				switch (msg.what) {
    				case PRO:
    
    					if (progress >= MAX_PROGRESS) {
    						// 重新设置
    						progress = 0;
    						progressDialog.dismiss();// 销毁对话框
    					} else {
    						progress++;
    						progressDialog.incrementProgressBy(1);
    
    						// 延迟发送消息
    						handler.sendEmptyMessageDelayed(PRO, 100);
    					}
    					break;
    				default:
    					break;
    				}
    
    			}
    		};
    
    		progressDialog = new ProgressDialog(this);
    		progressDialog.setIcon(R.drawable.ic_launcher);
    		progressDialog.setTitle("正在处理数据......");
    		progressDialog.setMessage("请稍后...");
    		progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);// 设置进度条对话框
    																			// 样式(水平,旋体)
    
    		// 进度最大值
    		progressDialog.setMax(MAX_PROGRESS);
    
    		progressDialog.setButton("暂停", new DialogInterface.OnClickListener() {
    
    			@Override
    			public void onClick(DialogInterface dialog, int which) {
    				//删除消息队列
    				handler.removeMessages(PRO);
    
    			}
    		});
    
    		progressDialog.setButton2("取消", new DialogInterface.OnClickListener() {
    
    			@Override
    			public void onClick(DialogInterface dialog, int which) {
    
    				//删除消息队列
    				handler.removeMessages(PRO);
    				//恢复进度初始值
    				progress=0;
    				progressDialog.setProgress(progress);
    			}
    		});
    
    		// 显示
    		progressDialog.show();
    		
    		//必须设置到show之后  show之前 可能bug
    		progress = (progress>0)?progress:0;
    		progressDialog.setProgress(progress);
    		
    		// 线程
    		handler.sendEmptyMessage(PRO);
    	}
    }
    


  • 相关阅读:
    Linux主要shell命令详解(下)
    mget命令, ftp命令详解
    VI 基本可视模式
    vim使用技巧
    cd及目录快速切换
    du命令解决linux磁盘空间满的问题(很不错的哦)
    Mysql删除数据后磁盘空间未释放的解决办法【转】
    MYSQL-innodb性能优化几个点
    Apache服务器出现Forbidden 403错误提示的解决方法总结
    MySQL 分区表原理及数据备份转移实战
  • 原文地址:https://www.cnblogs.com/dyllove98/p/3153157.html
Copyright © 2011-2022 走看看