Android 弹出对话框Dialog

Dialog01Activity.java
001 package Rw.Dialog;
002  
003 import android.app.Activity;
004 import android.app.AlertDialog;
005 import android.app.ProgressDialog;
006 import android.content.DialogInterface;
007 import android.content.Intent;
008 import android.os.Bundle;
009 import android.view.LayoutInflater;
010 import android.view.View;
011 import android.view.View.OnClickListener;
012 import android.widget.Button;
013 import android.widget.Toast;
014  
015 public class Dialog01Activity extends Activity {
016   
017     private Button button1,button2,button3,button4,button6,button7;
018     ProgressDialog progressDialog=null;
019     public void onCreate(Bundle savedInstanceState) {
020         super.onCreate(savedInstanceState);
021         setContentView(R.layout.main);
022         button1=(Button)findViewById(R.id.button1);
023         button2=(Button)findViewById(R.id.button2);
024         button3=(Button)findViewById(R.id.button3);
025         button4=(Button)findViewById(R.id.button4);
026         button6=(Button)findViewById(R.id.button6);
027         button7=(Button)findViewById(R.id.button7);
028    
029         button1.setOnClickListener(new ButtonListener());
030         button2.setOnClickListener(new ButtonListener());
031         button3.setOnClickListener(new ButtonListener());
032         button4.setOnClickListener(new ButtonListener());
033         button6.setOnClickListener(new ButtonListener());
034         button7.setOnClickListener(new ButtonListener());
035     }
036     
037     class ButtonListener implements OnClickListener{
038  
039         @Override
040         public void onClick(View v) {
041              final String[] itemStrings={"AA","BB","CC","DD"};
042             // TODO Auto-generated method stub
043             switch (v.getId()) {
044             case R.id.button1:
045                  AlertDialog.Builder dialog=new AlertDialog.Builder(Dialog01Activity.this);
046                    dialog.setTitle("Dialog").setIcon(android.R.drawable.ic_dialog_info).setMessage("弹出框").setPositiveButton("确定", new DialogInterface.OnClickListener() {
047                      
048                     @Override
049                     public void onClick(DialogInterface dialog, int which) {
050                         //转跳到另外一个Activity
051                         // TODO Auto-generated method stub
052                         Intent intent=new Intent();
053                         intent.setClass(getApplicationContext(), list.class);
054                         startActivity(intent);
055                     }
056                 }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
057                      
058                      
059                     public void onClick(DialogInterface dialog, int which) {
060                         // TODO Auto-generated method stub
061                         dialog.cancel();//取消弹出框
062                     }
063                 }).create().show();
064                 break;
065             case R.id.button2:
066                  
067                 AlertDialog.Builder builder=new AlertDialog.Builder(Dialog01Activity.this);
068                 builder.setTitle("LIST").setIcon(android.R.drawable.ic_lock_lock).setItems(itemStrings, new DialogInterface.OnClickListener() {
069                      
070                     @Override
071                     public void onClick(DialogInterface dialog, int which) {
072                         // TODO Auto-generated method stub
073                     Toast.makeText(getApplicationContext(), "你点击的是"+itemStrings[which], Toast.LENGTH_LONG).show(); 
074                     }
075                 }).create().show();
076                 break;
077             case R.id.button3:
078                  
079                 AlertDialog.Builder builder1=new AlertDialog.Builder(Dialog01Activity.this);
080                 builder1.setTitle("LIST").setIcon(android.R.drawable.ic_lock_lock).setSingleChoiceItems(itemStrings,-1, new DialogInterface.OnClickListener() {
081                      
082                     @Override
083                     public void onClick(DialogInterface dialog, int which) {
084                         // TODO Auto-generated method stub
085                     Toast.makeText(getApplicationContext(), "你点击的是"+itemStrings[which], Toast.LENGTH_LONG).show(); 
086                      
087                     }
088                 }).create().show();
089                 builder1.setCancelable(true);
090                 break;
091                 case R.id.button4:
092                     progressDialog=ProgressDialog.show(Dialog01Activity.this, "下载", "下载中.....",true);
093                     progressDialog.setCancelable(true);//当点击按钮返回的时候Dialog消失
094                     //progressDialog.dismiss();
095                     break;
096                      
097                 case R.id.button6:
098                     LayoutInflater inflater=(LayoutInflater)getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
099                     View view=inflater.inflate(R.layout.style, null);
100                     AlertDialog.Builder builder2=new AlertDialog.Builder(Dialog01Activity.this);
101                     builder2.setView(view);
102                     builder2.setTitle("QQ2011").setPositiveButton("确定", new DialogInterface.OnClickListener() {
103                          
104                         @Override
105                         public void onClick(DialogInterface dialog, int which) {
106                             // TODO Auto-generated method stub
107                             dialog.cancel();
108                         }
109                     }).create().show();
110                     
111                     break;
112                 case R.id.button7:
113                     Dialog01Activity.this.finish();
114                         break;
115             default:
116                 break;
117             }
118         }
119          
120     }
121 }
main.xml布局
01 <?xml version="1.0" encoding="utf-8"?>
02 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
03     android:orientation="vertical"
04     android:layout_width="fill_parent"
05     android:layout_height="fill_parent"
06     android:weightSum="1">
07 <Button android:id="@+id/button1" android:layout_height="wrap_content" android:layout_width="match_parent" android:text="简单弹出框"></Button>
08 <Button android:id="@+id/button2" android:layout_height="wrap_content" android:layout_width="match_parent" android:text="带有列表风格"></Button>
09 <Button android:id="@+id/button3" android:layout_height="wrap_content" android:layout_width="match_parent" android:text="带有Radio"></Button>
10 <Button android:id="@+id/button4" android:layout_height="wrap_content" android:layout_width="match_parent" android:text="进度条"></Button>
11 <Button android:id="@+id/button6" android:layout_height="wrap_content" android:layout_width="match_parent" android:text="自定义的"></Button>
12 <Button android:id="@+id/button7" android:layout_height="wrap_content" android:layout_width="match_parent" android:text="退出"></Button>
13 </LinearLayout>
自定义的Dialog风格 style.xml
01 <?xml version="1.0" encoding="utf-8"?>
02 <LinearLayout
03   xmlns:android="http://schemas.android.com/apk/res/android"
04   android:orientation="vertical"
05   android:layout_width="match_parent"
06   android:layout_height="match_parent" android:weightSum="1">
07     <ImageView android:layout_height="wrap_content" android:src="@drawable/logo" android:id="@+id/imageView1" android:layout_width="fill_parent"></ImageView>
08 <LinearLayout
09 android:orientation="horizontal"
10 android:layout_width="fill_parent"
11 android:layout_height="wrap_content"
12 >
13     <TextView android:text="账号:" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
14     <EditText android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="wrap_content" android:id="@+id/editText1">
15         <requestFocus></requestFocus>
16     </EditText>
17  
18 </LinearLayout>
19 <LinearLayout
20 android:orientation="horizontal"
21 android:layout_width="fill_parent"
22 android:layout_height="wrap_content"
23 >
24     <TextView android:text="密码:" android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
25     <EditText android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="wrap_content" android:id="@+id/editText2" android:inputType="textPassword"></EditText>
26  
27 </LinearLayout>
28     </LinearLayout>

主页面

 Android 弹出对话框Dialog Android 弹出对话框Dialog

Android 弹出对话框Dialog  Android 弹出对话框Dialog Android 弹出对话框Dialog

你可能感兴趣的:(android)