zoukankan      html  css  js  c++  java
  • Android自定义AlertDialog

    常见的一种方法:

    [html] view plaincopyprint?在CODE上查看代码片派生到我的代码片

    1. AlertDialog.Builder builder;  

    2.                 AlertDialog alertDialog;  

    3.   

    4.                 LayoutInflater inflater = getLayoutInflater();  

    5.                 // 添加自定义的布局文件  

    6.                 View layout = LayoutInflater.from(TestOne.this).inflate(  

    7.                         R.layout.dialog, null);  

    8.                 final TextView text = (TextView) layout.findViewById(R.id.tv1);  

    9.                 // 添加点击事件  

    10.                 text.setOnClickListener(new OnClickListener() {  

    11.   

    12.                     @Override  

    13.                     public void onClick(View v) {  

    14.                         // TODO Auto-generated method stub  

    15.                         text.setText("call");  

    16.                     }  

    17.                 });  

    18.   

    19.                 builder = new AlertDialog.Builder(TestOne.this);  

    20.                 alertDialog = builder.create();  

    21.                 // 去掉边框的黑色,因为设置的与四周的间距为0  

    22.                 alertDialog.setView(layout, 0, 0, 0, 0);  

    23.                 alertDialog.show();  

    24.                 // 修改大小  

    25.                 WindowManager.LayoutParams params = alertDialog.getWindow()  

    26.                         .getAttributes();  

    27.                 params.width = 350;  

    28.                 params.height = 200;  

    29.                 alertDialog.getWindow().setAttributes(params);  


    这样 ,重新给它填充自定义的布局视图,但缺乏可扩展性,而且每次使用还得重新定义。

    重写AlertDialog类,定义方法:

    [html] view plaincopyprint?在CODE上查看代码片派生到我的代码片

    1. /**  

    2.  * 自定义的对话框  

    3.  */  

    4. public abstract class MyAlerDialog extends AlertDialog implements  

    5.         android.view.View.OnClickListener {  

    6.   

    7.     protected MyAlerDialog(Context context) {  

    8.         super(context);  

    9.         // TODO Auto-generated constructor stub  

    10.     }  

    11.   

    12.     /**  

    13.      * 布局中的其中一个组件  

    14.      */  

    15.     private TextView txt;  

    16.   

    17.     @Override  

    18.     protected void onCreate(Bundle savedInstanceState) {  

    19.         // TODO Auto-generated method stub  

    20.         super.onCreate(savedInstanceState);  

    21.         // 加载自定义布局  

    22.         setContentView(R.layout.dialog);  

    23.         // setDialogSize(300, 200);  

    24.         txt = (TextView) findViewById(R.id.tv1);  

    25.         txt.setOnClickListener(this);  

    26.     }  

    27.   

    28.     /**  

    29.      * 修改 框体大小  

    30.      *   

    31.      * @param width  

    32.      * @param height  

    33.      */  

    34.     public void setDialogSize(int width, int height) {  

    35.         WindowManager.LayoutParams params = getWindow().getAttributes();  

    36.         params.width = 350;  

    37.         params.height = 200;  

    38.         this.getWindow().setAttributes(params);  

    39.     }  

    40.   

    41.     public abstract void clickCallBack();  

    42.       

    43.     /**  

    44.      * 点击事件  

    45.      */  

    46.     @Override  

    47.     public void onClick(View v) {  

    48.         // TODO Auto-generated method stub  

    49.         if (v == txt) {  

    50.             clickCallBack();  

    51.         }  

    52.     }  

    53.   

    54. }  

    在活动中使用:

    [html] view plaincopyprint?在CODE上查看代码片派生到我的代码片

    1. MyAlerDialog mydialog = new MyAlerDialog(this) {  

    2.             // 重写callback方法  

    3.             @Override  

    4.             public void clickCallBack() {  

    5.                 // TODO Auto-generated method stub  

    6.                 btn.setText("call");  

    7.             }  

    8.         };  

    9.         mydialog.show();  

    自己写的功能就封装了两个,有需要的童鞋可以很容易的扩展。这种方法,显然相对于上一种要有优势得多啦。

  • 相关阅读:
    团队冲刺第二天
    电梯演讲的准备——冰淇淋第一个项目NABCD分析
    团队冲刺第四天
    团队冲刺第六天
    团队冲刺第三天
    团队冲刺第一天
    XmlDocument类的WriteContentTo和WriteTo方法
    从一场DOTA对战中发现的哲理,也做为对2012年的展望
    String.Trim()真相大揭秘
    SQL Server 2008数据库维护计划
  • 原文地址:https://www.cnblogs.com/lzjsky/p/4924584.html
Copyright © 2011-2022 走看看