Andriod 对话框 .

2012-09-21 13:28 95人阅读 评论(0) 收藏 举报

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

1.最简单的对话框

[java] view plain copy print ?
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();


效果如下

Andriod 对话框 ._第1张图片

2.列表对话框

[java] view plain copy print ?

 

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数组
效果图

Andriod 对话框 ._第2张图片

3.单选对话框

[java] view plain copy print ?
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();
		

效果图

Andriod 对话框 ._第3张图片

4.多选对话框

 

[java] view plain copy print ?

 

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();


效果图

Andriod 对话框 ._第4张图片


5.进度条对话框

[java] view plain copy print ?
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;


效果图

Andriod 对话框 ._第5张图片

6.代码自定义对话框

[html] view plain copy print ?
EditText et=new EditText(this);
			et.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD|InputType.TYPE_CLASS_TEXT);
			b.setTitle("请输入密码");
			b.setView(et);
			return b.create();


效果图

Andriod 对话框 ._第6张图片

7.XML文件自定义对话框

xml文件

[html] view plain copy print ?
<?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代码

[java] view plain copy print ?

 

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();


效果图

Andriod 对话框 ._第7张图片

你可能感兴趣的:(android,dialog,andriod,对话框)