• android ui定义自己的dialog(项目框架搭建时就写好,之后事半功倍)


    自定义一个dialog:

    之前有很多博客都有过这方面的介绍,可是个人觉得通常不是很全面,通用性不是很强,一般会定义一个自己的dialog类,然后去使用,难道每一个dialog都要定义一个class吗??

    首先:dialog一般包含一个标题部分,内容部分,按钮部分,风格部分。progressdialog则多一个进度条

    那么我们就不妨写一个dialog类,在构造方法中,我们把标题,内容,按钮信息都给他,然后可以show出来

    然后,在构造方法中添加一个接口,接口中使用确定,取消等等的按钮的回调。

    那么开始咯:

    第一步定义一个自己的dialog类

    import android.app.Dialog;
    import android.content.Context;
    import android.content.DialogInterface;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    
    import com.easipass.R;
    
    public class CustormDialog extends Dialog implements DialogInterface {
    
        	private String title;
        	private String content;
        	private DialogCallBack callback;
        	private int index;
        	
        	/***
        	 * @param context
        	 * @param title 对话框标题
        	 * @param content 对话框内容
        	 * @param theme 对应的style 这里为R.style.CustomDialog_1 可自定义style
        	 * @param dialogcallback 确定取消按钮的回调 分别是 onCancle onOk
        	 * @param index 显示几个button 1 为只有一个确定键,其他为有确定取消两个按钮
        	 * 
        	 * 调用实例 
        	 * 	    dialog = new CustormDialog(SettingsActivity.this,"缓存清理",
        	 * 		"点击确定为您清理以下历史信息:
    系统通知,提箱小票,行业资讯,装箱单录入", R.style.CustomDialog_1,
            *		new DialogCallBack(){
           *			@Override
           *			public void OkDown() {   
             *           	dialog.dismiss();
             *           //这里放 确定按钮响应
            *			}
            *			@Override
            *			public void CancleDown() {
             *           	dialog.dismiss();	
             *            //这里放取消按钮响应				
            *			} },2);
             */
            public CustormDialog(Context context,String title,String content,int theme,DialogCallBack dialogcallback,int index) {
                super(context, theme);
                this.title = title;
                this.content = content;
                this.callback = dialogcallback;
                this.index = index;
            }
    
    @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.dg_custormdialog);
                TextView titl = (TextView) findViewById(R.id.title);
                TextView cont = (TextView) findViewById(R.id.tv_content);  
                
                titl.setText(title);
                cont.setText(content);
                
                Button cancel = (Button) findViewById(R.id.cancel);
                Button ok = (Button) findViewById(R.id.sure);
                if(index == 1){
                	cancel.setVisibility(View.GONE);
                }else{
                cancel.setOnClickListener(new android.view.View.OnClickListener() {
                    public void onClick(View v) {               	
                        CustormDialog.this.dismiss();
                        callback.CancleDown();
                    }
                });
                }
                ok.setOnClickListener(new android.view.View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        CustormDialog.this.dismiss();
                        callback.OkDown();
                    }
                });
            }
                
        }

    这个类中实现了接口 DialogCallBack  

    再定义下这个回调:

    public interface DialogCallBack {
        abstract void OkDown();
        abstract void CancleDown(); 
    }

    初步的框架就有了  

    接下来我们把布局写一下(当然框架在那,布局完全自己发挥就好了)

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/mian_container"
            android:layout_width="300dp"
            android:layout_height="wrap_content"
            android:background="@color/easipass_dailog_bg_blue"
            android:orientation="vertical"
            android:padding="10dip" >
    
            <TextView
                android:id="@+id/title"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="@string/notification_version_name"
                android:textSize="18sp" />
    
            <View
                android:layout_width="fill_parent"
                android:layout_height="1dp"
                android:layout_marginTop="10dip"
                android:background="@drawable/cmb_list_separator_line" />
    
            <TextView
                android:id="@+id/tv_content"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dip"
                android:gravity="center_horizontal"
                android:text="@string/version_isup"
                android:textSize="18sp" />
    
            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:gravity="center_horizontal"
                android:padding="10dip" >
    
                <Button
                    android:id="@+id/sure"
                    android:layout_width="120dp"
                    android:layout_height="wrap_content"
                    android:text="@string/ok"
                    android:textSize="@dimen/main_content_text_size" />
                <Button
                    android:id="@+id/cancel"
                    android:layout_width="120dp"
                    android:layout_height="wrap_content"
                    android:text="@string/cancel"
                    android:textSize="@dimen/main_content_text_size" />
            </LinearLayout>
    </LinearLayout>

    系统自带的style通常让我们受不鸟,它有一个白色边框,等等,那我们最好定义一个自己的style  

    <style name="CustomDialog_1" parent="@android:style/Theme.Dialog">
            <item name="android:windowFrame">@null</item>
            <item name="android:windowIsFloating">true</item>
            <item name="android:windowIsTranslucent">false</item>
            <item name="android:windowNoTitle">true</item>
            <item name="android:windowBackground">@null</item>
            <item name="android:backgroundDimEnabled">false</item>
        </style>

    一切顺利的话,我们尝试一下哦  

    dialog = new CustormDialog(SettingsActivity.this,"放标题","放内容", R.style.CustomDialog_1,
        		new DialogCallBack(){
    @Override
    public void OkDown() {		                   
                    	dialog.dismiss();
    
    }
    @Override
    public void CancleDown() {
                    	dialog.dismiss();					
    }
        	
        },2);

    这里的 2是显示 确定 取消 两个按钮,如果填写1,那就只有确定按钮  

    自己发挥咯

        

    个人布局的比较丑啦,相信大家比俺文艺很多

    有了这一个类,以后有确定取消按钮,以及只有确定按钮的对话框都搞定啦

    ps,如果还要一次性搞定progressdialog,那就用一个帧布局,就好了,给构造添加一个参数,废话不多说了,相信大家也没有这么笨的,举一反三啦

  • 相关阅读:
    Ubuntu 18.04 初始化(server版本 )
    named主从环境部署
    CentOS 源码安装svn
    端口状态
    进程状态
    top命令详解
    gitlab部署
    day16
    day15
    day14
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/3734765.html
Copyright © 2020-2023  润新知