Android 弹出对话框Dialog

 

Android 弹出对话框Dialog

分类: Android 小知识   676人阅读  评论(2)  收藏  举报

Dialog01Activity.java

[java]  view plain copy print ?
  1. package Rw.Dialog;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.AlertDialog;  
  5. import android.app.ProgressDialog;  
  6. import android.content.DialogInterface;  
  7. import android.content.Intent;  
  8. import android.os.Bundle;  
  9. import android.view.LayoutInflater;  
  10. import android.view.View;  
  11. import android.view.View.OnClickListener;  
  12. import android.widget.Button;  
  13. import android.widget.Toast;  
  14.   
  15. public class Dialog01Activity extends Activity {  
  16.    
  17.     private Button button1,button2,button3,button4,button6,button7;  
  18.     ProgressDialog progressDialog=null;  
  19.     public void onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.         setContentView(R.layout.main);  
  22.         button1=(Button)findViewById(R.id.button1);  
  23.         button2=(Button)findViewById(R.id.button2);  
  24.         button3=(Button)findViewById(R.id.button3);  
  25.         button4=(Button)findViewById(R.id.button4);  
  26.         button6=(Button)findViewById(R.id.button6);  
  27.         button7=(Button)findViewById(R.id.button7);  
  28.     
  29.         button1.setOnClickListener(new ButtonListener());  
  30.         button2.setOnClickListener(new ButtonListener());  
  31.         button3.setOnClickListener(new ButtonListener());  
  32.         button4.setOnClickListener(new ButtonListener());  
  33.         button6.setOnClickListener(new ButtonListener());  
  34.         button7.setOnClickListener(new ButtonListener());  
  35.     }  
  36.      
  37.     class ButtonListener implements OnClickListener{  
  38.   
  39.         @Override  
  40.         public void onClick(View v) {  
  41.              final String[] itemStrings={"AA","BB","CC","DD"};  
  42.             // TODO Auto-generated method stub  
  43.             switch (v.getId()) {  
  44.             case R.id.button1:  
  45.                  AlertDialog.Builder dialog=new AlertDialog.Builder(Dialog01Activity.this);  
  46.                    dialog.setTitle("Dialog").setIcon(android.R.drawable.ic_dialog_info).setMessage("弹出框").setPositiveButton("确定"new DialogInterface.OnClickListener() {  
  47.                       
  48.                     @Override  
  49.                     public void onClick(DialogInterface dialog, int which) {  
  50.                         //转跳到另外一个Activity  
  51.                         // TODO Auto-generated method stub  
  52.                         Intent intent=new Intent();  
  53.                         intent.setClass(getApplicationContext(), list.class);  
  54.                         startActivity(intent);  
  55.                     }  
  56.                 }).setNegativeButton("取消"new DialogInterface.OnClickListener() {  
  57.                       
  58.                       
  59.                     public void onClick(DialogInterface dialog, int which) {  
  60.                         // TODO Auto-generated method stub  
  61.                         dialog.cancel();//取消弹出框  
  62.                     }  
  63.                 }).create().show();  
  64.                 break;  
  65.             case R.id.button2:  
  66.                   
  67.                 AlertDialog.Builder builder=new AlertDialog.Builder(Dialog01Activity.this);  
  68.                 builder.setTitle("LIST").setIcon(android.R.drawable.ic_lock_lock).setItems(itemStrings, new DialogInterface.OnClickListener() {  
  69.                       
  70.                     @Override  
  71.                     public void onClick(DialogInterface dialog, int which) {  
  72.                         // TODO Auto-generated method stub  
  73.                     Toast.makeText(getApplicationContext(), "你点击的是"+itemStrings[which], Toast.LENGTH_LONG).show();    
  74.                     }  
  75.                 }).create().show();  
  76.                 break;  
  77.             case R.id.button3:  
  78.                   
  79.                 AlertDialog.Builder builder1=new AlertDialog.Builder(Dialog01Activity.this);  
  80.                 builder1.setTitle("LIST").setIcon(android.R.drawable.ic_lock_lock).setSingleChoiceItems(itemStrings,-1new DialogInterface.OnClickListener() {  
  81.                       
  82.                     @Override  
  83.                     public void onClick(DialogInterface dialog, int which) {  
  84.                         // TODO Auto-generated method stub  
  85.                     Toast.makeText(getApplicationContext(), "你点击的是"+itemStrings[which], Toast.LENGTH_LONG).show();    
  86.                       
  87.                     }  
  88.                 }).create().show();  
  89.                 builder1.setCancelable(true);  
  90.                 break;  
  91.                 case R.id.button4:  
  92.                     progressDialog=ProgressDialog.show(Dialog01Activity.this"下载""下载中.....",true);  
  93.                     progressDialog.setCancelable(true);//当点击按钮返回的时候Dialog消失  
  94.                     //progressDialog.dismiss();  
  95.                     break;  
  96.                       
  97.                 case R.id.button6:  
  98.                     LayoutInflater inflater=(LayoutInflater)getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);  
  99.                     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布局

[html]  view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:weightSum="1">  
  7. <Button android:id="@+id/button1" android:layout_height="wrap_content" android:layout_width="match_parent" android:text="简单弹出框"></Button>  
  8. <Button android:id="@+id/button2" android:layout_height="wrap_content" android:layout_width="match_parent" android:text="带有列表风格"></Button>  
  9. <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

[html]  view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.   xmlns:android="http://schemas.android.com/apk/res/android"  
  4.   android:orientation="vertical"  
  5.   android:layout_width="match_parent"  
  6.   android:layout_height="match_parent" android:weightSum="1">  
  7.     <ImageView android:layout_height="wrap_content" android:src="@drawable/logo" android:id="@+id/imageView1" android:layout_width="fill_parent"></ImageView>  
  8. <LinearLayout   
  9. 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_第1张图片Android 弹出对话框Dialog_第2张图片

Android 弹出对话框Dialog_第3张图片 Android 弹出对话框Dialog_第4张图片Android 弹出对话框Dialog_第5张图片

Android 弹出对话框Dialog_第6张图片

你可能感兴趣的:(html,android,android,layout,dialog,button,encoding,小知识)