自定义一个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,"缓存清理",
* "点击确定为您清理以下历史信息:\n系统通知,提箱小票,行业资讯,装箱单录入", 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,那就用一个帧布局,就好了,给构造添加一个参数,废话不多说了,相信大家也没有这么笨的,举一反三啦

android ui定义自己的dialog(项目框架搭建时就写好,之后事半功倍)的更多相关文章

  1. 权限管理系统之项目框架搭建并集成日志、mybatis和分页

    前一篇博客中使用LayUI实现了列表页面和编辑页面的显示交互,但列表页面table渲染的数据是固定数据,本篇博客主要是将固定数据变成数据库数据. 一.项目框架 首先要解决的是项目框架问题,搭建什么样的 ...

  2. go语言实战教程:实战项目资源导入和项目框架搭建

    从本节内容开始,我们将利用我们所学习的Iris框架的相关知识,进行实战项目开发. 实战项目框架搭建 我们的实战项目是使用Iris框架开发一个关于本地服务平台的后台管理平台.平台中可以管理用户.商品.商 ...

  3. .Net Core3.0 WebApi 项目框架搭建 一:实现简单的Resful Api

    .Net Core3.0 WebApi 项目框架搭建:目录 开发环境 Visual Studio 2019.net core 3.1 创建项目 新建.net core web项目,如果没有安装.net ...

  4. .Net Core3.0 WebApi 项目框架搭建 二:API 文档神器 Swagger

    .Net Core3.0 WebApi 项目框架搭建:目录 为什么使用Swagger 随着互联网技术的发展,现在的网站架构基本都由原来的后端渲染,变成了:前端渲染.后端分离的形态,而且前端技术和后端技 ...

  5. .Net Core3.0 WebApi 项目框架搭建 四:JWT权限验证

    .Net Core3.0 WebApi 项目框架搭建:目录 什么是JWT 根据维基百科定义,JWT(读作 [/dʒɒt/]),即JSON Web Tokens,是一种基于JSON的.用于在网络上声明某 ...

  6. .Net Core3.0 WebApi 项目框架搭建 五:仓储模式

    .Net Core3.0 WebApi 项目框架搭建:目录 理论介绍 仓储(Respository)是存在于工作单元和数据库之间单独分离出来的一层,是对数据访问的封装.其优点: 1)业务层不需要知道它 ...

  7. ssm项目框架搭建(增删改查案例实现)——(SpringMVC+Spring+mybatis项目整合)

    Spring 常用注解 内容 一.基本概念 1. Spring 2. SpringMVC 3. MyBatis 二.开发环境搭建 1. 创建 maven 项目 2. SSM整合 2.1 项目结构图 2 ...

  8. Angular企业级开发(5)-项目框架搭建

    1.AngularJS Seed项目目录结构 AngularJS官方网站提供了一个angular-phonecat项目,另外一个就是Angular-Seed项目.所以大多数团队会基于Angular-S ...

  9. (三) Angular2项目框架搭建心得

    前言: 在哪看到过angular程序员被React程序员鄙视,略显尴尬,确实Angular挺值得被调侃的,在1.*版本存在的几个性能问题,性能优化的"潜规则"贼多,以及从1.*到2 ...

随机推荐

  1. ERP系统开发平台 (C#语言,支持多数据库)

    C/S系统开发框架-企业版 V4.0 (Enterprise Edition) 简介: http://www.csframework.com/cs-framework-4.0.htm 适用软件:适合开 ...

  2. 软件测试software testing summarize

    软件测试(英语:software testing),描述一种用来促进鉴定软件的正确性.完整性.安全性和质量的过程.软件测试的经典定义是:在规定的条件下对程序进行操作,以发现程序错误,衡量软件质量,并对 ...

  3. gocode 安装

    1.安装git,将git/bin添加至PATH 2.执行go get -u github.com/nsf/gocode 3.在%GOPATH%/bin/下会生成gocode.exe

  4. 《C和指针》章节后编程练习解答参考——第5章

    5.1 题目: 略 解答代码: #include <stdio.h> int main(void) { char ch; while (((ch = getchar()) != EOF) ...

  5. MVC简单分页(未实现无刷新分页)

    分页Html辅助方法 using System.Text; using System.Web: using System.Web.Mvc; namespace System.Web.Mvc { pub ...

  6. Application(全局对象,ViewState

    Application对象生存期和Web应用程序生存期一样长,生存期从Web应用程序网页被访问开始,HttpApplication类对象Application被自动创建,直到没有一个网页被访问时结束, ...

  7. 如何分析matlab程序的主要效率问题

    利用profile on 在需要分析效率的程序段前后加入 profile on profile off 然后,在common line中输入profile viewer即可观察到这段程序的效率

  8. SVN 修改URL路径|SVN 项目路径修改

    在svn的根目录下面右键 输入要修改的地址: 点击ok 搞定... ~~~

  9. C++ 1

    1 new 建立一个堆对象 new 类名(初值列表) 返回一个指针 int * p=new int(3)动态分配 2 delete  释放指针 delete p; delete [] p ;释放动态申 ...

  10. 【HDOJ】4612 Warm up

    双连通缩点+求树的直径,图论基础题目. /* 4612 */ #pragma comment(linker, "/STACK:1024000000,1024000000") #in ...