android——dialog对话框总结

dialog对话框可以分为:

普通Dialog
列表Dialog
单选Dialog
多选Dialog
等待Dialog
进度条Dialog
编辑Dialog

效果图:

普通对话框:

activity_main.xml中先定义了八个按钮,对应的对话框按钮。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="${relativePackage}.${activityClass}" >

    <Button 
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="两个按钮"/>
    <Button 
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="三个按钮"/>
    <Button 
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="列表"/>
    <Button 
        android:id="@+id/button3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="单选"/>
    <Button 
        android:id="@+id/button4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="多选"/>
    <Button 
        android:id="@+id/button5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="等待"/>
    <Button 
        android:id="@+id/button6"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="进度条"/>
    <Button 
        android:id="@+id/button7"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="编辑"/>
    

</LinearLayout>

MainActivity.java中,这里先把按钮后获取过来了,后面的对话框样式代码分开写的(普通对话框)。

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {
	
	private Button button;
	private Button button1;
	private Button button2;
	private Button button3;
	private Button button4;
	private Button button5;
	private Button button6;
	private Button button7;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        button = (Button) findViewById(R.id.button);
        button1 = (Button) findViewById(R.id.button1);
        button2 = (Button) findViewById(R.id.button2);
        button3 = (Button) findViewById(R.id.button3);
        button4 = (Button) findViewById(R.id.button4);
        button5 = (Button) findViewById(R.id.button5);
        button6 = (Button) findViewById(R.id.button6);
        button7 = (Button) findViewById(R.id.button7);
        button.setOnClickListener(this);
        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);
        button4.setOnClickListener(this);
        button5.setOnClickListener(this);
        button6.setOnClickListener(this);
        button7.setOnClickListener(this);
    }

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.button:	//两个按钮
			showNorMalDialog();
			break;
		case R.id.button1:	//三个按钮
			showMultiBtnDialog();
			break;
		case R.id.button2:	//列表
			showListDialog();
			break;
		case R.id.button3:	//单选按钮
			showSingleChoiceDialog();
			break;
		case R.id.button4:	//多选按钮
			showMultiChoiceDialog();
			break;
		case R.id.button5:	//等待
			showWaitingDialog();
			break;
		case R.id.button6: 	//进度条
			showProgressDialog();
			break;
		case R.id.button7:	//编辑
			showInputDialog();
			break;
		default:
			break;
		}
	}

    //三个按钮
	private void showMultiBtnDialog() {
		AlertDialog.Builder normalDialog = new AlertDialog.Builder(MainActivity.this);
		//设置对话框图标
		normalDialog.setIcon(R.drawable.ic_launcher);
		//设置对话框标题
		normalDialog.setTitle("对话框标题");
		//设置对话框消息提示
		normalDialog.setMessage("对话框内容提示");
		//按钮一
		normalDialog.setPositiveButton("按钮1", 
			new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					Toast.makeText(MainActivity.this, "按钮1", Toast.LENGTH_LONG).show();
				}
			}
		);
		//按钮二
		normalDialog.setNeutralButton("按钮2", 
			new DialogInterface.OnClickListener() {
			
				@Override
				public void onClick(DialogInterface dialog, int which) {
					Toast.makeText(MainActivity.this, "按钮2", Toast.LENGTH_LONG).show();
				}
			}
		);
		//按钮三
		normalDialog.setNegativeButton("按钮3", 
			new DialogInterface.OnClickListener() {
			
				@Override
				public void onClick(DialogInterface dialog, int which) {
					Toast.makeText(MainActivity.this, "按钮3", Toast.LENGTH_LONG).show();
				}
			}
		);
		normalDialog.show();
	}

	//两个按钮
	private void showNorMalDialog() {
		AlertDialog.Builder normalDialog = new AlertDialog.Builder(MainActivity.this);
		//设置对话框图标
		normalDialog.setIcon(R.drawable.ic_launcher);
		//设置对话框标题
		normalDialog.setTitle("对话框标题");
		//设置对话框消息提示
		normalDialog.setMessage("对话框内容提示");
		//setPositiveButton确定按钮
		normalDialog.setPositiveButton("确定", 
			new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					Toast.makeText(MainActivity.this, "确定按钮", Toast.LENGTH_LONG).show();
				}
			}
		);
		//setNegativeButton取消按钮
		normalDialog.setNegativeButton("取消", 
			new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					Toast.makeText(MainActivity.this, "取消按钮", Toast.LENGTH_LONG).show();
				}
			}
		);
		//显示
		normalDialog.show();
	}
}

列表:

private void showListDialog() {
	final String items[] = {"列表1", "列表2", "列表3", "列表4"};
	AlertDialog.Builder normalDialog = new AlertDialog.Builder(MainActivity.this);
	//设置对话框图标
	normalDialog.setIcon(R.drawable.ic_launcher);
	//设置对话框标题
	normalDialog.setTitle("列表");
	normalDialog.setItems(items, new DialogInterface.OnClickListener() {
			
	    @Override
		public void onClick(DialogInterface dialog, int which) {
			// which 下标从0开始
			Toast.makeText(MainActivity.this, "你点击了" + items[which], Toast.LENGTH_SHORT).show();
		}
	});
	normalDialog.show();
}

单选按钮:

int a;
private void showSingleChoiceDialog() {
	final String items[] = {"单选1", "单选2", "单选3", "单选4"};
	a = -1;
	AlertDialog.Builder dia = new AlertDialog.Builder(MainActivity.this);
	//设置对话框图标
	dia.setIcon(R.drawable.ic_launcher);
	//设置对话框标题
	dia.setTitle("单选按钮");
	//第二个参数默认选项,此处设置为1,默认选中第二个
	dia.setSingleChoiceItems(items, 1, 
		new DialogInterface.OnClickListener() {
			//a为选中项的下标
			@Override
			public void onClick(DialogInterface dialog, int which) {
				a = which;
			}
		}
	);
	dia.setPositiveButton("确定", 
		new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				if(a != -1){
					Toast.makeText(MainActivity.this, "你选择了" + items[a], Toast.LENGTH_SHORT).show();
				}
			}
		}
	);
	dia.show();
}

多选:

ArrayList<Integer> list = new ArrayList<Integer>();
private void showMultiChoiceDialog() {
	final String items[] = {"多选1", "多选2", "多选3", "多选4"};
	AlertDialog.Builder dia = new AlertDialog.Builder(MainActivity.this);
	//设置默认选中的选项,全为false默认均未选中
	final boolean init[] = {false, false, false, false};
	list.clear();
	//设置对话框图标
	dia.setIcon(R.drawable.ic_launcher);
	//设置对话框标题
	dia.setTitle("多选按钮");
	dia.setMultiChoiceItems(items, init, 
		new DialogInterface.OnMultiChoiceClickListener() {
			
			//判断有哪些为选中状态
			@Override
			public void onClick(DialogInterface dialog, int which, boolean isChecked) {
				if(isChecked){
					list.add(which);
				}else{
					list.remove(which);
				}
			}
		}
	);
	dia.setPositiveButton("确定", 
		new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				int size = list.size();
				String str = "";
				for(int i = 0; i < size; i++){
					str += items[list.get(i)]+", ";
				}
				Toast.makeText(MainActivity.this, "你选中了" + str, Toast.LENGTH_SHORT).show();
			}
		}
	);
	dia.show();
}

等待:

private void showWaitingDialog() {
	ProgressDialog  dia = new ProgressDialog (MainActivity.this);
	dia.setTitle("等待");
	dia.setMessage("等待中");
	dia.setIndeterminate(true);
	//@setCancelable 为使屏幕不可点击,设置为不可取消false
	dia.setCancelable(false);
	dia.show();
}

进度条

private void showProgressDialog() {
	final int max = 100;
	final ProgressDialog  dia = new ProgressDialog (MainActivity.this);
	//设置初始进度条
	dia.setProgress(0);
	dia.setTitle("进度条");
	//设置样式(水平进度条)
	dia.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
	//设置进度最大值
	dia.setMax(max);
	dia.show();
	//模拟进度条增加的过程
	//开启一个新线程,每个100ms,进程+1
	new Thread(new Runnable(){

		@Override
		public void run() {
			int p = 0;
			while(p < max){
				try {
					Thread.sleep(100);
					p++;
					dia.setProgress(p);
				} catch (Exception e) {
					Log.i("Dialog", "线程异常");
				}
			}
		}
		
	}).start();
}

编辑:

private void showInputDialog() {
	//定义输入框
	final EditText edit = new EditText(MainActivity.this);
	AlertDialog.Builder dia = new AlertDialog.Builder(MainActivity.this);
	dia.setTitle("输入框").setView(edit);
	dia.setPositiveButton("确定", 
		new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				Toast.makeText(MainActivity.this, edit.getText().toString(), Toast.LENGTH_SHORT).show();
			}
		}
	).show();
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值