Andriod 对话框

本文详细介绍如何在Android应用中使用各种类型的对话框,包括简单的对话框、列表对话框、单选和多选对话框、进度条对话框以及自定义对话框等,并提供了相应的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在Activity中可以调用showDialog()来显示一个对话框,覆盖Activity的onCreateDialog方法,在这个方法中创建对话框,返回一个Dialog对象。

1.最简单的对话框

AlertDialog.Builder b=new  AlertDialog.Builder(this);
b.setTitle("简单的");
			b.setMessage("this is a simple dialog");
			b.setPositiveButton("是", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					
				}
			});
			b.setNegativeButton("否", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					
				}
			});
			return b.create();


效果如下

2.列表对话框

b.setTitle("列表");
			//b.setMessage("message");这行代码不要有
			b.setItems(items, new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					Toast.makeText(AndroidDialogActivity.this, items[which], Toast.LENGTH_SHORT).show();
					
				}
			});
			return b.create();

items是一个String数组
效果图

3.单选对话框

b.setTitle("请选择颜色");
			b.setSingleChoiceItems(items, -1,  new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					Toast.makeText(AndroidDialogActivity.this, items[which], Toast.LENGTH_SHORT).show();
					
				}
			});
			b.setPositiveButton("是", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					
				}
			});
			b.setNegativeButton("否", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					
				}
			});
			return b.create();
		

效果图

4.多选对话框

 

boolean []ddd=new boolean[3];
			b.setTitle("请选择颜色");
			b.setMultiChoiceItems(items, ddd, new DialogInterface.OnMultiChoiceClickListener(){

				@Override
				public void onClick(DialogInterface dialog, int which,
						boolean isChecked) {
					// TODO Auto-generated method stub
					
				}
				
			});
			
			return b.create();


效果图


5.进度条对话框

Handler hand=new Handler(){

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			if(progressint>=100)
			{
				pd.dismiss();
			}
			else
			{
				progressint++;
				pd.setProgress(progressint);
				hand.sendEmptyMessageDelayed(0, 100);
			}
			
		}
		
	};
pd=new ProgressDialog(this);
			pd.setTitle("进度对话框");
			pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
			pd.setMax(100);
			AndroidDialogActivity.this.hand.sendEmptyMessage(0);
			pd.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener(){

				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					
					
				}});
			return pd;


效果图

6.代码自定义对话框

EditText et=new EditText(this);
			et.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD|InputType.TYPE_CLASS_TEXT);
			b.setTitle("请输入密码");
			b.setView(et);
			return b.create();


效果图

7.XML文件自定义对话框

xml文件

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="用户名" />
    <EditText
       android:id="@+id/EditText1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="TextView"
        
        />

    <TextView
        android:id="@+id/textView2"
         android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="密码" />

    <EditText
       android:id="@+id/EdiText2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:inputType="textPassword"
        
        />
    <Button
        android:id="@+id/buttonyes"
         android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="确定" />

</LinearLayout>


Java代码

b.setIcon(R.drawable.ic_launcher);
			b.setTitle("自定义对话框");
			LayoutInflater li=LayoutInflater.from(this);
			View v=li.inflate(R.layout.info, null);
			Button yes=(Button) v.findViewById(R.id.buttonyes);
			yes.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					Toast.makeText(AndroidDialogActivity.this, "Hello World", Toast.LENGTH_SHORT).show();
				}});
			b.setView(v);
			return b.create();


效果图

 下载工程:下载

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值