【实战】android中几个常用的对话框

几个常用对话框实现的源码:

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"
    tools:context=".MainActivity" >

	<Button 
	    android:onClick="dialog01"
	    android:layout_width="fill_parent"
	    android:layout_height="wrap_content"
	    android:text="创建确认取消对话框"
	    />
	<Button 
	    android:onClick="dialog02"
	    android:layout_width="fill_parent"
	    android:layout_height="wrap_content"
	    android:text="创建单选对话框"
	    />
	<Button 
	    android:onClick="dialog03"
	    android:layout_width="fill_parent"
	    android:layout_height="wrap_content"
	    android:text="创建多选对话框"
	    />
	<Button 
	    android:onClick="dialog04"
	    android:layout_width="fill_parent"
	    android:layout_height="wrap_content"
	    android:text="创建进度话框"
	    />
	<Button 
	    android:onClick="dialog05"
	    android:layout_width="fill_parent"
	    android:layout_height="wrap_content"
	    android:text="创建进度话框-带进度条的"
	    />
</LinearLayout>

MainActivity.java

package com.itheima.dialogs;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
/*
 *  先掌握前 三种 
 * 
 */
public class MainActivity extends Activity {

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

	// 创建确认取消对话框
	public void dialog01(View v){
		
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		
		builder.setTitle("约会把...");
		builder.setMessage("告别单身, 你愿意吗 ?");
		
		builder.setPositiveButton("愿意,gogogo", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				Toast.makeText(MainActivity.this, "我也单身, 说不定 可以 来找我...", 0).show();
			}
		});
		
		builder.setNegativeButton("不愿意", null );
		builder.show();
	}
	
	//创建单选对话框
	public void dialog02(View v){
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle("单选对话框 ");
		final String[] items = {"小丽","小红","小芳"};
		builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				
				Toast.makeText(MainActivity.this, " 被点击了 : " + items[which] +",位置: " +which, 0	).show();
				
			}
		});
		builder.show();
	}
	
	//创建多选对话框
	public void dialog03(View v){
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle("多选");
		// 这里不要设置 message 
//		builder.setMessage("哈哈,你可以 选 多个 有兴趣去学习的 了 ???");
		
		final String[] items ={"android","ios","javaee","php","C++"};
		boolean[] checkedItems = {true,true,false,false,false};
		
		builder.setMultiChoiceItems(items, checkedItems, new DialogInterface.OnMultiChoiceClickListener(){

			@Override
			public void onClick(DialogInterface dialog, int which,
					boolean isChecked) {
				Toast.makeText(MainActivity.this, " 被点击了 : " + items[which] +",位置: " +which+", 值是: "+ isChecked, 0	).show();
			}
		});
		
		builder.show();
		
	}
	
	//创建进度话框
	public void dialog04(View v){
		
		// 很多 地方 , 干了 比较耗时的事儿 时 会使用到的 
		ProgressDialog dialog = new ProgressDialog(this);
		dialog.setMessage("正在拼命加载中....");
		dialog.show();
//		
//		try {
//			Thread.sleep(3000);
//			dialog.dismiss();
//		} catch (InterruptedException e) {
//			e.printStackTrace();
//		}
		
		
		// 这行 代码 可以 让其 显示, 查看源代码 , 实际上是调用了  上面的这些代码 
		//ProgressDialog.show(this, "消息", "正在拼命加载中....").show();
		
		
	}
	
	//创建进度话框-带进度条的
	public void dialog05(View v){
		
		ProgressDialog dialog = new ProgressDialog(this);
		dialog.setMax(100);
		
//		dialog.set
		
		for (int i = 0; i < 100; i++) {
			dialog.setProgress(i);
		}
		dialog.show();
		
		// 隔 几秒中 去 消失 
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

mind_programmonkey

你的鼓励是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值