用户界面View之Dialog 对话框

 终身学习,不断读书,以书为侣,是我们每个人不让自己的生命过早枯萎的唯一选择。为了让我们的生命之树常青,让我们今后的生活更加丰富多彩,朋友们,让我们一起来努力吧! 


本讲内容:Dialog 对话框


一、AlertDialog常用方法
使用AlertDialog.Builder中的create()方法创建一个AlertDialog

setTitle();给对话框设置标题
setIcon();给对话框设置图标
setMessage();设置对话框的提示信息
setView() 给对话框设置自定义样式
setItems();设置对话框要显示的一个list,一般用于显示几个命令时
setSingleChoiceItems();设置对话框显示一个单选的List
setMultiChoiceItems();设置对话框显示一系列的复选框
setNeutralButton();普通按钮
setPositiveButton();给对话框添加"确定"按钮
setNegativeButton();给对话框添加"取消"按钮


二、ProgressDialog的用法

ProgressDialog 继承自AlertDialog,AlertDialog继承自Dialog,实现DialogInterface接口。


示例一:

  

  

下面是res/layout/activity_main.xml 布局文件:

<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:orientation="vertical" >

    <Button
        android:id="@+id/dialog_btn1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"
        android:text="确认对话框" />

    <Button
        android:id="@+id/dialog_btn2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"
        android:text="单选按钮对话框" />

    <Button
        android:id="@+id/dialog_btn3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"
        android:text="多选按钮对话框" />

    <Button
        android:id="@+id/dialog_btn4"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"
        android:text="列表对话框" />

    <Button
        android:id="@+id/dialog_btn5"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"
        android:text="自定义对话框" />

</LinearLayout>


下面是res/layout/dialog_layout.xml 布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:hint="输入内容..." />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dip"
            android:text="提交" />
    </LinearLayout>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dip"
        android:src="@drawable/pic" />

</LinearLayout>

下面是MainActivity.java主界面文件:

public class MainActivity extends Activity implements OnClickListener{
	private Button b1;
	private Button b2;
	private Button b3;
	private Button b4;
	private Button b5;
	String [] single_list = {"男","女","女博士","程序员"};
	String [] multi_list = {"篮球","足球","男生","女生"};
	String [] item_list = {"项目经理","策划","测试","美工","程序猿"};

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initViews();
	}

	/**
	 * 初始化控件
	 */
	private void initViews() {
		b1=(Button) findViewById(R.id.dialog_btn1);
		b2=(Button) findViewById(R.id.dialog_btn2);
		b3=(Button) findViewById(R.id.dialog_btn3);
		b4=(Button) findViewById(R.id.dialog_btn4);
		b5=(Button) findViewById(R.id.dialog_btn5);
		b1.setOnClickListener(this);
		b2.setOnClickListener(this);
		b3.setOnClickListener(this);
		b4.setOnClickListener(this);
		b5.setOnClickListener(this);
	}

	/**
	 * 按钮点击事件
	 */
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.dialog_btn1:
			showDialog1();
			break;
		case R.id.dialog_btn2:
			showDialog2();
			break;
		case R.id.dialog_btn3:
			showDialog3();
			break;
		case R.id.dialog_btn4:
			showDialog4();
			break;
		case R.id.dialog_btn5:
			showDialog5();
			break;
		}
	}
	
	/**
	 * 显示确认对话框
	 */
	private void showDialog1(){
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle("提示");//设置标题
		builder.setIcon(R.drawable.ic_launcher);//设置图标
		builder.setMessage("确认退出吗?");//设置内容
		builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				Toast.makeText(MainActivity.this, "点击了确定按钮!",Toast.LENGTH_SHORT).show();
				//dialog.dismiss();// 关闭对话框  
			}
		});
		builder.setNegativeButton("取消",  new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				Toast.makeText(MainActivity.this, "点击了取消按钮!",Toast.LENGTH_SHORT).show();
			}
		});
		builder.create().show();// 创建并显示对话框  
	}
	
	/**
	 * 显示单选按钮对话框
	 */
	private void showDialog2(){
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle("选择性别");
		builder.setIcon(R.drawable.ic_launcher);//0:默认选中第一个
		builder.setSingleChoiceItems(single_list, 0, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				String str = single_list[which];
				Toast.makeText(MainActivity.this, "这个人是"+str+"!",Toast.LENGTH_SHORT).show();
				dialog.dismiss();
			}
		});
		builder.setNegativeButton("取消", null);
		builder.create().show();// 创建并显示对话框  
	}
	
	/**
	 * 显示多选按钮对话框
	 */
	private void showDialog3(){
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle("爱好");
		builder.setIcon(R.drawable.ic_launcher);//null:不默认选中
		builder.setMultiChoiceItems(multi_list, null, new DialogInterface.OnMultiChoiceClickListener() {
			public void onClick(DialogInterface dialog, int which, boolean isChecked) {
				if(isChecked){
					Toast.makeText(MainActivity.this, "我喜欢上了"+multi_list[which]+"!",Toast.LENGTH_SHORT).show();
				}else{
					Toast.makeText(MainActivity.this, "我不喜欢"+multi_list[which]+"了!",Toast.LENGTH_SHORT).show();
				}
			}
		});
		builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		builder.create().show();// 创建并显示对话框  
	}
	/**
	 * 显示列表对话框
	 */
	private void showDialog4(){
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle("部门列表");
		builder.setIcon(R.drawable.ic_launcher);
		builder.setItems(item_list, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				Toast.makeText(MainActivity.this, "我选择了"+item_list[which]+"!",Toast.LENGTH_SHORT).show();
			}
		});
		builder.create().show();// 创建并显示对话框  
	}
	
	/**
	 * 显示自定义对话框
	 */
	private void showDialog5(){
		LayoutInflater inflater=LayoutInflater.from(this);
		View view=inflater.inflate(R.layout.dialog_layout, null);
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle("自定义对话框");
		builder.setIcon(R.drawable.ic_launcher);
		builder.setView(view);
		builder.create().show();// 创建并显示对话框  
	}
}

示例二:


public class MainActivity extends ActionBarActivity {

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		Button b1 = (Button) findViewById(R.id.id_b1);
		b1.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				showDialog();
			}
		});
	}

	private void showDialog() {
		final ProgressDialog dialog = new ProgressDialog(this);
		dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);// 设置进度条的形式为圆形转动的进度条
		dialog.setCancelable(true);// 设置是否可以通过点击Back键取消
		dialog.setCanceledOnTouchOutside(false);// 设置在点击Dialog外是否取消Dialog进度条
		dialog.setIcon(R.drawable.ic_launcher);//
		// 设置提示的title的图标,默认是没有的,如果没有设置title的话只设置Icon是不会显示图标的
		dialog.setTitle("提示");
		dialog.setMessage("这是一个圆形进度条");

		// 设置可点击的按钮,最多有三个(默认情况下)
		dialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定",
				new DialogInterface.OnClickListener() {

					public void onClick(DialogInterface dialog, int which) {

					}
				});
		dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消",
				new DialogInterface.OnClickListener() {

					public void onClick(DialogInterface dialog, int which) {

					}
				});
		dialog.setButton(DialogInterface.BUTTON_NEUTRAL, "中立",
				new DialogInterface.OnClickListener() {

					public void onClick(DialogInterface dialog, int which) {

					}
				});

		dialog.show();
	}

}


示例三:自定义

下面是res/layout/loading_dialog.xml 布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/dialog_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:minHeight="60dp"
    android:minWidth="180dp"
    android:orientation="vertical"
    android:padding="10dp" >

    <ImageView
        android:id="@+id/id_dialog_img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/loading_bg" />

    <TextView
        android:id="@+id/id_textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:text="数据加载中……" />

</LinearLayout>

定义一个loadingDialog中imageView转动的动画:loading_animation.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false" >

    <rotate
        android:duration="1500"
        android:fromDegrees="0"
        android:interpolator="@android:anim/linear_interpolator"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatCount="-1"
        android:repeatMode="restart"
        android:startOffset="-1"
        android:toDegrees="+360" />

</set>

定义dialog的style.

<!-- 自定义loading dialog -->  
    <style name="loading_dialog" parent="android:style/Theme.Dialog">  
        <item name="android:windowFrame">@null</item>  
        <item name="android:windowNoTitle">true</item>   
        <item name="android:windowBackground">@color/white</item>  
        <item name="android:windowIsFloating">true</item>  
        <item name="android:windowContentOverlay">@null</item>  
    </style>  


下面是MainActivity.java主界面文件:

public class MainActivity extends ActionBarActivity {

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		Button b1 = (Button) findViewById(R.id.id_b1);
		b1.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				Dialog dialog=createLoadingDialog(MainActivity.this, "正在登陆...");
				dialog.show();
			}
		});
	}

	/**
	 * 自定义的progressDialog 
	 * @param context
	 * @param msg
	 * @return
	 */
	public static Dialog createLoadingDialog(Context context, String msg) {  
		  
        LayoutInflater inflater = LayoutInflater.from(context);  
        View v = inflater.inflate(R.layout.loading_dialog, null);// 得到加载view  
        LinearLayout layout = (LinearLayout) v.findViewById(R.id.dialog_view);// 加载布局  
        
        ImageView spaceshipImage = (ImageView) v.findViewById(R.id.id_dialog_img);  
        TextView tipTextView = (TextView) v.findViewById(R.id.id_textview);
        // 加载动画  
        Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(  
                context, R.anim.loading_animation);  
        // 使用ImageView显示动画  
        spaceshipImage.startAnimation(hyperspaceJumpAnimation);  
        tipTextView.setText(msg);// 设置加载信息  
  
        Dialog loadingDialog = new Dialog(context, R.style.loading_dialog);// 创建自定义样式dialog  
  
        loadingDialog.setCancelable(false);// 不可以用“返回键”取消  
        loadingDialog.setContentView(layout, new LinearLayout.LayoutParams(  
                LinearLayout.LayoutParams.MATCH_PARENT,  
                LinearLayout.LayoutParams.MATCH_PARENT));// 设置布局  
        return loadingDialog;  
  
    }  

}



Take your time and enjoy it



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值