免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1812 | 回复: 0
打印 上一主题 下一主题

自定义Dialog UI [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-11-19 15:46 |只看该作者 |倒序浏览
自定义Dialog UI  





本例中CustomDialog继承Dialog,使用custom_dialog.xml布局文件。




custom_dialog.xml,这里就随便布局了一下



Java代码
  1. 1.<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2. 2.    android:id="@+id/layout_root"  
  3. 3.    android:layout_width="fill_parent"  
  4. 4.    android:layout_height="fill_parent"  
  5. 5.    android:orientation="horizontal"  
  6. 6.    android:padding="10dp" >   
  7. 7.  
  8. 8.    <ImageView   
  9. 9.        android:id="@+id/image"  
  10. 10.        android:layout_width="wrap_content"  
  11. 11.        android:layout_height="fill_parent"  
  12. 12.        android:layout_marginRight="10dp" />   
  13. 13.  
  14. 14.    <LinearLayout   
  15. 15.        android:layout_width="fill_parent"  
  16. 16.        android:layout_height="wrap_content"  
  17. 17.        android:orientation="vertical"  
  18. 18.        android:padding="5px" >   
  19. 19.  
  20. 20.        <TextView   
  21. 21.            android:id="@+id/text"  
  22. 22.            android:layout_width="wrap_content"  
  23. 23.            android:layout_height="fill_parent"  
  24. 24.            android:textColor="#FFF" />   
  25. 25.  
  26. 26.        <LinearLayout   
  27. 27.            android:layout_width="fill_parent"  
  28. 28.            android:layout_height="wrap_content"  
  29. 29.            android:orientation="horizontal"  
  30. 30.            android:padding="5px" >   
  31. 31.  
  32. 32.            <Button   
  33. 33.                android:id="@+id/button_yes"  
  34. 34.                android:layout_width="wrap_content"  
  35. 35.                android:layout_height="wrap_content"  
  36. 36.                android:gravity="center"  
  37. 37.                android:text=" Yes " />   
  38. 38.  
  39. 39.            <Button   
  40. 40.                android:id="@+id/button_no"  
  41. 41.                android:layout_width="wrap_content"  
  42. 42.                android:layout_height="wrap_content"  
  43. 43.                android:gravity="center"  
  44. 44.                android:text=" No " />   
  45. 45.        </LinearLayout>   
  46. 46.    </LinearLayout>   
  47. 47.  
  48. 48.</LinearLayout>  
  49. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  50.     android:id="@+id/layout_root"
  51.     android:layout_width="fill_parent"
  52.     android:layout_height="fill_parent"
  53.     android:orientation="horizontal"
  54.     android:padding="10dp" >

  55.     <ImageView
  56.         android:id="@+id/image"
  57.         android:layout_width="wrap_content"
  58.         android:layout_height="fill_parent"
  59.         android:layout_marginRight="10dp" />

  60.     <LinearLayout
  61.         android:layout_width="fill_parent"
  62.         android:layout_height="wrap_content"
  63.         android:orientation="vertical"
  64.         android:padding="5px" >

  65.         <TextView
  66.             android:id="@+id/text"
  67.             android:layout_width="wrap_content"
  68.             android:layout_height="fill_parent"
  69.             android:textColor="#FFF" />

  70.         <LinearLayout
  71.             android:layout_width="fill_parent"
  72.             android:layout_height="wrap_content"
  73.             android:orientation="horizontal"
  74.             android:padding="5px" >

  75.             <Button
  76.                 android:id="@+id/button_yes"
  77.                 android:layout_width="wrap_content"
  78.                 android:layout_height="wrap_content"
  79.                 android:gravity="center"
  80.                 android:text=" Yes " />

  81.             <Button
  82.                 android:id="@+id/button_no"
  83.                 android:layout_width="wrap_content"
  84.                 android:layout_height="wrap_content"
  85.                 android:gravity="center"
  86.                 android:text=" No " />
  87.         </LinearLayout>
  88.     </LinearLayout>

  89. </LinearLayout>  
复制代码
main.xml



Java代码
  1. 1.<?xml version="1.0" encoding="utf-8"?>   
  2. 2.<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3. 3.    android:orientation="vertical"  
  4. 4.    android:layout_width="fill_parent"  
  5. 5.    android:layout_height="fill_parent"  
  6. 6.    >   
  7. 7.<Button   
  8. 8.    android:id="@+id/main_button"     
  9. 9.    android:layout_width="fill_parent"   
  10. 10.    android:layout_height="wrap_content"   
  11. 11.    android:text="click to start an Dialog"  
  12. 12.    />   
  13. 13.</LinearLayout>  
  14. <?xml version="1.0" encoding="utf-8"?>
  15. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  16.     android:orientation="vertical"
  17.     android:layout_width="fill_parent"
  18.     android:layout_height="fill_parent"
  19.     >
  20. <Button
  21.         android:id="@+id/main_button"  
  22.     android:layout_width="fill_parent"
  23.     android:layout_height="wrap_content"
  24.     android:text="click to start an Dialog"
  25.     />
  26. </LinearLayout>
复制代码
CustomDialog.java,CustomDialog继承Dialog,使用起来和Activity差不多,通过setContentView()指定使用的布局文件,剩下的就和Activity差不多了,就是一些findViewById()和setListener()。



Java代码
  1. 1.package com.android.CustomDialog;   
  2. 2.  
  3. 3.import android.app.Dialog;   
  4. 4.import android.content.Context;   
  5. 5.import android.os.Bundle;   
  6. 6.import android.util.Log;   
  7. 7.import android.view.View;   
  8. 8.import android.widget.Button;   
  9. 9.import android.widget.ImageView;   
  10. 10.import android.widget.TextView;   
  11. 11.  
  12. 12.public class CustomDialog extends Dialog {   
  13. 13.  
  14. 14.    public CustomDialog(Context context) {   
  15. 15.        super(context);   
  16. 16.        // TODO Auto-generated constructor stub   
  17. 17.    }   
  18. 18.      
  19. 19.     protected void onCreate(Bundle savedInstanceState){   
  20. 20.         super.onCreate(savedInstanceState);   
  21. 21.            
  22. 22.         setContentView(R.layout.custom_dialog);   
  23. 23.         setTitle("Custom Dialog");   
  24. 24.  
  25. 25.         TextView text = (TextView)findViewById(R.id.text);   
  26. 26.         text.setText("Hello, this is a custom dialog!");   
  27. 27.         ImageView image = (ImageView)findViewById(R.id.image);   
  28. 28.         image.setImageResource(R.drawable.sepurple);   
  29. 29.            
  30. 30.         Button buttonYes = (Button) findViewById(R.id.button_yes);   
  31. 31.         buttonYes.setHeight(5);   
  32. 32.         buttonYes.setOnClickListener(new Button.OnClickListener(){   
  33. 33.  
  34. 34.                public void onClick(View v) {   
  35. 35.                    // TODO Auto-generated method stub   
  36. 36.                    dismiss();   
  37. 37.                       
  38. 38.                }   
  39. 39.            });   
  40. 40.         Button buttonNo = (Button) findViewById(R.id.button_no);   
  41. 41.         buttonNo.setSingleLine(true);   
  42. 42.         buttonNo.setOnClickListener(new Button.OnClickListener(){   
  43. 43.  
  44. 44.                public void onClick(View v) {   
  45. 45.                    // TODO Auto-generated method stub   
  46. 46.                    dismiss();   
  47. 47.                       
  48. 48.                }   
  49. 49.            });   
  50. 50.     }   
  51. 51.        
  52. 52.     //called when this dialog is dismissed   
  53. 53.     protected void onStop() {   
  54. 54.         Log.d("TAG","+++++++++++++++++++++++++++");   
  55. 55.     }   
  56. 56.        
  57. 57.  
  58. 58.}  
  59. package com.android.CustomDialog;

  60. import android.app.Dialog;
  61. import android.content.Context;
  62. import android.os.Bundle;
  63. import android.util.Log;
  64. import android.view.View;
  65. import android.widget.Button;
  66. import android.widget.ImageView;
  67. import android.widget.TextView;

  68. public class CustomDialog extends Dialog {

  69.         public CustomDialog(Context context) {
  70.                 super(context);
  71.                 // TODO Auto-generated constructor stub
  72.         }
  73.        
  74.          protected void onCreate(Bundle savedInstanceState){
  75.                  super.onCreate(savedInstanceState);
  76.                  
  77.                  setContentView(R.layout.custom_dialog);
  78.                  setTitle("Custom Dialog");

  79.                  TextView text = (TextView)findViewById(R.id.text);
  80.                  text.setText("Hello, this is a custom dialog!");
  81.                  ImageView image = (ImageView)findViewById(R.id.image);
  82.                  image.setImageResource(R.drawable.sepurple);
  83.                  
  84.                  Button buttonYes = (Button) findViewById(R.id.button_yes);
  85.                  buttonYes.setHeight(5);
  86.                  buttonYes.setOnClickListener(new Button.OnClickListener(){

  87.                                 public void onClick(View v) {
  88.                                         // TODO Auto-generated method stub
  89.                                         dismiss();
  90.                                        
  91.                                 }
  92.                 });
  93.                  Button buttonNo = (Button) findViewById(R.id.button_no);
  94.                  buttonNo.setSingleLine(true);
  95.                  buttonNo.setOnClickListener(new Button.OnClickListener(){

  96.                                 public void onClick(View v) {
  97.                                         // TODO Auto-generated method stub
  98.                                         dismiss();
  99.                                        
  100.                                 }
  101.                 });
  102.          }
  103.          
  104.          //called when this dialog is dismissed
  105.          protected void onStop() {
  106.                  Log.d("TAG","+++++++++++++++++++++++++++");
  107.          }
  108.          

  109. }
复制代码
CustomDialogUsage.java,这里重写了Activity的onCreateDialog()方法创建Dialog(),并为Dialog设置了OnDismissListener,没什么特别的。



Java代码
  1. 1.package com.android.CustomDialog;   
  2. 2.  
  3. 3.import android.app.Activity;   
  4. 4.import android.app.Dialog;   
  5. 5.import android.content.DialogInterface;   
  6. 6.import android.content.DialogInterface.OnDismissListener;   
  7. 7.import android.os.Bundle;   
  8. 8.import android.view.View;   
  9. 9.import android.view.View.OnClickListener;   
  10. 10.import android.widget.Button;   
  11. 11.import android.widget.Toast;   
  12. 12.  
  13. 13.public class CustomDialogUsage extends Activity {   
  14. 14.  
  15. 15.    OnDismissListener lis;   
  16. 16.    /** Called when the activity is first created. */  
  17. 17.    @Override  
  18. 18.    public void onCreate(Bundle savedInstanceState) {   
  19. 19.        super.onCreate(savedInstanceState);   
  20. 20.        setContentView(R.layout.main);   
  21. 21.           
  22. 22.        lis  =  new OnDismissListener() {   
  23. 23.               
  24. 24.            @Override  
  25. 25.            public void onDismiss(DialogInterface dialog) {   
  26. 26.                // TODO Auto-generated method stub   
  27. 27.                Toast.makeText(getApplicationContext(),   
  28. 28.                        ((CustomDialog)dialog).toString(),   
  29. 29.                        Toast.LENGTH_SHORT)   
  30. 30.                .show();   
  31. 31.            }   
  32. 32.        };   
  33. 33.           
  34. 34.        Button buttonYes = (Button) findViewById(R.id.main_button);   
  35. 35.        buttonYes.setOnClickListener(new OnClickListener(){   
  36. 36.  
  37. 37.            public void onClick(View v) {   
  38. 38.                // TODO Auto-generated method stub   
  39. 39.                showDialog(0);   
  40. 40.            }   
  41. 41.        });   
  42. 42.  
  43. 43.    }   
  44. 44.      
  45. 45.    @Override  
  46. 46.    protected Dialog onCreateDialog(int id) {   
  47. 47.        // TODO Auto-generated method stub   
  48. 48.        Dialog d = new CustomDialog(this);   
  49. 49.        d.setOnDismissListener(lis);   
  50. 50.        return d;   
  51. 51.    }   
  52. 52.      
  53. 53.    @Override  
  54. 54.    protected void onPrepareDialog(int id, Dialog dialog) {   
  55. 55.        // TODO Auto-generated method stub   
  56. 56.        super.onPrepareDialog(id, dialog);   
  57. 57.    }   
  58. 58.      
  59. 59.}  
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP