zoukankan      html  css  js  c++  java
  • Android开发(19)---常见dialog对话框的运用

    Dialog是android开发过程中最常用到的组件之一,Dialog的创立办法有两种:
    一是直接new一个Dialog目标,然后调用Dialog目标的show和dismiss办法来操控对话框的显现和躲藏。
    二是在Activity的onCreateDialog(int id)办法中创立Dialog目标并回来,然后调用Activty的showDialog(int id)和dismissDialog(int id)来显现和躲藏对话框。

    差异在于经过第二种办法创立的对话框会承继Activity的特点,比方取得Activity的menu事情等。下面案例主要介绍第一种。

    1.最简单的对话框

    布局文件:

    就一个Button按钮

    <Button
            android:id="@+id/button1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="79dp"
             android:onClick="openDialog"
            android:text="打开对话框" />

    MainActivity.java

    package com.example.lesson16_dialog;
    
    import android.app.Activity;
    import android.app.AlertDialog;
    import android.app.Dialog;
    import android.app.ProgressDialog;
    import android.content.DialogInterface;
    import android.content.DialogInterface.OnClickListener;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Message;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.widget.Toast;
    
    public class MainActivity extends Activity {
    	private Handler handler;
    	private int progress;
    	private static final int MAX_PROGRESS=100;
    	private static final int PRO=10;
    	private ProgressDialog progressDialog;
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    
    	}
    
    	@SuppressWarnings("deprecation")
    	public void openDialog(View v) {
    		test1();
    		/*tes2();
    		tes3();
    		tes4();
    		tes5();
    		tes6();
    		test7();
    		myDialog();*/
    	}
    
    	
    
    	public void test1() {
    		// 创建对话框对象
    		AlertDialog alertDialog = new AlertDialog.Builder(this).create();
    
    		// 设置对话框的标题
    		alertDialog.setTitle("这是一个消息对话框");
    		// 设置对话框中的内容
    		alertDialog.setMessage("消息");
    		// 显示对话框
    		alertDialog.show();
    	}
    
    	public void tes2() {
    		AlertDialog alertDialog = new AlertDialog.Builder(this)
    				.setTitle("哈哈").setMessage("什么都没有").show();
    
    	}
    
    	public void tes3() {
    		new AlertDialog.Builder(this)
    				.setIcon(R.drawable.ic_launcher)
    				.setTitle("对话框")
    				.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, checkedItem, listener)
    		// .setSingleChoiceItems(itemsId, checkedItem, listener)
    		// .setSingleChoiceItems(adapter, checkedItem, listener)
    		// .setSingleChoiceItems(cursor, checkedItem, labelColumn, listener)
    		// labelColumn如果数据源是数据集
    		// 数据集中的某一列会作为列表对话框的数据加载的列表框中,该参数表示该列的名称(字段名称)
    				.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(itemsId, checkedItems, listener)
    				// .setMultiChoiceItems(cursor, isCheckedColumn, labelColumn,
    				// listener)
    				.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 test7(){
    		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("请稍后...");
    		// 设置进度条对话框  旋转STYLE_SPINNER,水平 STYLE_HORIZONTAL
    		progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    																			// 样式(水平,旋体)
    
    		// 进度最大值
    		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);
    	}
    	//自定义的对话框
    		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();
    		}
    		
    }
    




  • 相关阅读:
    可执行程序的装载
    stdafx.h的作用
    AI调色板
    3ds max输出图片
    3ds max移除几何体的线段
    3ds max删除了对象后,还是将原来所有对象输出的原因
    vs win32 & MFC 指针默认位置
    3ds max 分离对象
    PDF
    endnote设置文献第二行悬挂缩进办法
  • 原文地址:https://www.cnblogs.com/jiangu66/p/3155495.html
Copyright © 2011-2022 走看看