android开发中自定义dialog的布局样式实例

在网上找了很多关于dialog的自定义样式的问题,还有很多人写得比较复杂,需要改动style什么的,或者是自定义dialog搞得很复杂,我最后还是找到了方法来实现。

下面是我的dialog布局xml文件:

[mw_shl_code=java,true]<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/transparent"
    android:layout_margin="50dp">
   
    <RelativeLayout
        android:id="@+id/rl_dialog_content"
        android:layout_width="fill_parent"
        android:layout_height="200dp"
        android:background="@drawable/alertdialog_bg">
            <TextView
                android:id="@+id/dialog_text"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="现在就打电话给客服:arjinmc"           
                android:layout_marginTop="50dp"     
                android:layout_marginLeft="30dp"
                android:layout_marginRight="30dp"
                android:maxLines="5"
                android:gravity="center"
                />        
               
            <LinearLayout
                android:id="@+id/ll_buttons"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:layout_alignParentBottom="true"
                android:layout_margin="30dp">
               
               
                <Button
                 android:id="@+id/dialog_cancel"
                 android:text="@string/alert_cancel"
                 android:background="@drawable/btn_long_white"
                 style="@style/dialog_button"
                 android:layout_weight="1"
                 android:textColor="@color/tabs_font"
                 />
                <Button
                 android:id="@+id/dialog_ok"
                 android:text="@string/alert_ok"
                 android:background="@drawable/btn_long_red"
                 style="@style/dialog_button"
                 android:layout_weight="1"
                 android:layout_marginLeft="5dp"/>
               
            </LinearLayout>
    </RelativeLayout>
    <ImageButton
        android:id="@+id/dialog_close"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/alertdialog_close"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="20dp"
       />   
   

</RelativeLayout>
[/mw_shl_code]

在代码中需要这样写就可以了:
[mw_shl_code=java,true]//布局文件转换为view对象
  LayoutInflater inflaterDl = LayoutInflater.from(this);
  RelativeLayout layout = (RelativeLayout)inflaterDl.inflate(R.layout.layout_dialog, null );
 
  //对话框
  final Dialog dialog = new AlertDialog.Builder(SettingActivity.this).create();
  dialog.show();
  dialog.getWindow().setContentView(layout);
 
 
  //取消按钮
  Button btnCancel = (Button) layout.findViewById(R.id.dialog_cancel);
  btnCancel.setOnClickListener(new OnClickListener() {
        
        @Override
        public void onClick(View v) {
                Toast.makeText(getApplicationContext(), "cancel", Toast.LENGTH_SHORT).show();                                
        }
  });
 
 
  //确定按钮
  Button btnOK = (Button) layout.findViewById(R.id.dialog_ok);
  btnOK.setOnClickListener(new OnClickListener() {
        
        @Override
        public void onClick(View v) {
                Toast.makeText(getApplicationContext(), "ok", Toast.LENGTH_SHORT).show();                                
        }
  });
 
 
  //关闭按钮
  ImageButton btnClose = (ImageButton) layout.findViewById(R.id.dialog_close);
  btnClose.setOnClickListener(new OnClickListener() {
        
        @Override
        public void onClick(View v) {
                dialog.dismiss();                                
        }
  });[/mw_shl_code]

非常easy!自己动手吧。

自定义dialog的样式并比较日期

<style name="myDialogTheme" parent="android:style/Theme.Dialog">
<item name="android:windowNoTitle">true</item>
</style>

1、布局main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
    <Button
        android:id="@+id/btn"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:text="按下按钮试试"/>

</LinearLayout>

2、ly_dialogcontent

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="220dp"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:gravity="center"
    android:layout_gravity="center"
    android:background="#88E0EEEE">
    <LinearLayout
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:orientation="vertical">
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textColor="@android:color/white"
            android:textSize="20sp"
            android:text="查询"
            android:gravity="center"
            android:layout_gravity="center"
            android:padding="10dp"/>
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:gravity="center">
            <TextView
                
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="开始时间:"/>
            <EditText
                android:id="@+id/beginTime"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:singleLine="true"/>
          </LinearLayout>
          <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:gravity="center">
            <TextView
                
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="开始时间:"/>
            <EditText
                android:id="@+id/endTime"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:singleLine="true"/>
          </LinearLayout>
          <LinearLayout
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:gravity="center">
              <Button
                  android:id="@+id/search"
                  android:layout_height="wrap_content"
                  android:layout_width="wrap_content"
                  android:layout_weight="1.0"
                  android:text="查询"/>
              <Button
                  android:id="@+id/cancel"
                  android:layout_height="wrap_content"
                  android:layout_width="wrap_content"
                  android:layout_weight="1.0"
                  android:text="取消"/>
              
          </LinearLayout>
        
    </LinearLayout>

    

</RelativeLayout>

3、Mainactivity

package com.ct.dialog;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.os.Bundle;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;
import android.widget.Toast;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    private Button btnBn;
    private Dialog dlg;
    private LayoutInflater mInflater;
    private Calendar calendar;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        btnBn = (Button)findViewById(R.id.btn);
        init();
        btnBn.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                dlg.show();
            }
        });
    }
    
    private void init(){
        calendar = Calendar.getInstance();
        dlg = new Dialog(MainActivity.this, R.style.myDialogTheme);
        mInflater = LayoutInflater.from(MainActivity.this);
        dlg.setCancelable(true);
        dlg.setCanceledOnTouchOutside(true);
        
        View view = mInflater.inflate(R.layout.ly_dialogcontent, null);
        final EditText begin = (EditText)view.findViewById(R.id.beginTime);
        final EditText end = (EditText)view.findViewById(R.id.endTime);
        Button sbtn = (Button)view.findViewById(R.id.search);
        Button clebt = (Button)view.findViewById(R.id.cancel);
        begin.setInputType(InputType.TYPE_NULL);
        end.setInputType(InputType.TYPE_NULL);
        dlg.setContentView(view);
        //取消
        clebt.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                dlg.dismiss();
            }
        });
        
        //查询
        sbtn.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                String beginTime =  begin.getText().toString();
                String endTime = end.getText().toString();
                if (beginTime.equals("")|| endTime.equals("")) {

                    Toast.makeText(MainActivity.this, "都不能为空", Toast.LENGTH_LONG).show();

                }else if(!isLarge(beginTime, endTime)){
                    Toast.makeText(MainActivity.this, "结束时间不能比开始时间小", Toast.LENGTH_LONG).show();
                } else {
                    dlg.dismiss();}
            }
        });
        
        //开始时间
        begin.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                final String second = "00";
                DatePickerDialog dpg = new DatePickerDialog(MainActivity.this,
                        new OnDateSetListener() {
                            
                            @Override
                            public void onDateSet(DatePicker view, int year, int monthOfYear,
                                    int dayOfMonth) {
                                // TODO Auto-generated method stub
                                begin.setText(year+"-"+
                                format(++monthOfYear)+"-"+
                                format(dayOfMonth));
                                
                                TimePickerDialog tpg = new TimePickerDialog(MainActivity.this,
                                        new OnTimeSetListener(){

                                            @Override
                                            public void onTimeSet(
                                                    TimePicker arg0, int hourOfDay,
                                                    int minute) {
                                                // TODO Auto-generated method stub
                                                begin.setText(begin.getText().toString()
                                                        + " "
                                                        + format(hourOfDay)
                                                        + ":"
                                                        + format(minute)
                                                        + ":" + second);
                                            }},calendar.get(Calendar.HOUR_OF_DAY),
                                        calendar.get(Calendar.MINUTE), true);
                                tpg.show();
                            }
                        }, calendar.get(Calendar.YEAR),
                        calendar.get(Calendar.MONTH),
                        calendar.get(Calendar.DAY_OF_MONTH));
                
                dpg.show();
            }
        });
        //结束时间
        end.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                final String second = "00";
                DatePickerDialog dpg = new DatePickerDialog(MainActivity.this,
                        new OnDateSetListener() {
                            
                            @Override
                            public void onDateSet(DatePicker view, int year, int monthOfYear,
                                    int dayOfMonth) {
                                // TODO Auto-generated method stub
                                end.setText(year+"-"+
                                format(++monthOfYear)+"-"+
                                format(dayOfMonth));
                                
                                TimePickerDialog tpg = new TimePickerDialog(MainActivity.this,
                                        new OnTimeSetListener(){

                                            @Override
                                            public void onTimeSet(
                                                    TimePicker arg0, int hourOfDay,
                                                    int minute) {
                                                // TODO Auto-generated method stub
                                                end.setText(begin.getText().toString()
                                                        + " "
                                                        + format(hourOfDay)
                                                        + ":"
                                                        + format(minute)
                                                        + ":" + second);
                                            }},calendar.get(Calendar.HOUR_OF_DAY),
                                        calendar.get(Calendar.MINUTE), true);
                                tpg.show();
                            }
                        }, calendar.get(Calendar.YEAR),
                        calendar.get(Calendar.MONTH),
                        calendar.get(Calendar.DAY_OF_MONTH));
                
                dpg.show();
            }
        });
        
        
    }
    
    /**
     * 比较两个时间的大小
     * @throws ParseException
     *
     * */
    public static boolean isLarge(String beginTime,String endTime) {
        boolean flag = false;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date begin = sdf.parse(beginTime);
            Date edn = sdf.parse(endTime);
            if(edn.getTime() - begin.getTime() >0){
                flag = true;
            }else {
                flag = false;
            }
            
        } catch (ParseException e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        return flag;
        
    }
    
    
    /**
     * 在一位数字前加0变成两位数字
     *
     * @param value
     * @return
     */
    private String format(int value) {
        String s = String.valueOf(value);
        if (s.length() == 1) {
            s = "0" + s;
        }
        return s;
    }
}

 (在F:\java\z自定义dialog+点击弹出时间调整那个\MyDialogTheme)

时间: 2024-09-21 08:32:00

android开发中自定义dialog的布局样式实例的相关文章

Android编程中自定义dialog用法实例_Android

本文实例讲述了Android编程中自定义dialog用法.分享给大家供大家参考,具体如下: dialog是android中提供的一组弹出提示框,非常好用,可是它的样式是一个定式,有时候我们需求定义一些自己的样式 1.定义一个样式文件,此文件继承自Theme.Dialog,在style.xml文件中建立一个自己的样式 <style name="addNoteType_error_Dialog" parent="@android:Theme.Dialog">

Android开发中自定义ProgressBar控件的方法示例

本文实例讲述了Android开发中自定义ProgressBar控件的方法.分享给大家供大家参考,具体如下: 很简单,首先加载Drawable,在onMeasure设置好其区域大小, 然后使用canvas.clipRect绘图 public class ProgressView extends ImageView { private Drawable maskDraw; /** * 加载的进度 0-100 */ private int mProcess = 20; public ProgressV

Android开发中日期工具类DateUtil完整实例

本文实例讲述了Android开发中日期工具类DateUtil.分享给大家供大家参考,具体如下: /** * 日期操作工具类. * @Project ERPForAndroid * @Package com.ymerp.android.tools * @author chenlin * @version 1.0 */ @SuppressLint("SimpleDateFormat") public class DateUtil { private static final String

Android App开发中自定义View和ViewGroup的实例教程_Android

ViewAndroid所有的控件都是View或者View的子类,它其实表示的就是屏幕上的一块矩形区域,用一个Rect来表示,left,top表示View相对于它的parent View的起点,width,height表示View自己的宽高,通过这4个字段就能确定View在屏幕上的位置,确定位置后就可以开始绘制View的内容了. View绘制过程View的绘制可以分为下面三个过程: MeasureView会先做一次测量,算出自己需要占用多大的面积.View的Measure过程给我们暴露了一个接口o

android开发中 PopupWindow 自适应宽度布局

PopupWindow用法 使用PopupWindow可实现弹出窗口效果,,其实和AlertDialog一样,也是一种对话框,两者也经常混用,但是也各有特点.下面就看看使用方法.首先初始化一个PopupWindow,指定窗口大小参数. PopupWindow mPop = new PopupWindow(getLayoutInflater().inflate(R.layout.window, null),LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_C

Android 中自定义Dialog样式的Activity点击空白处隐藏软键盘功能(dialog不消失)

一.需求触发场景: 项目中需要开发带有EditText的Dialog显示,要求在编辑完EditText时,点击Dilog的空白处隐藏软键盘.但是Dialog不会消失.示例如下: 二.实现方法: 发布需求时,我个人曾想过直接通过new的方式直接创建Dialog,经过多次尝试,无法实现要求,所以采用将Activity设置为Dialog样式进行展示,调用方法实现需求.具体实现如下: 本次演示示例的工程结构: 2.1AndroidMainfest.xml配置文件 需要在配置文件中将需要显示为dialog

Android开发中实现IOS风格底部选择器(支持时间 日期 自定义)_Android

本文Github代码链接 https://github.com/AndroidMsky/AndoirdIOSPicker 先上图吧: 这是笔者最近一个项目一直再用的一个选择器库,自己也在其中做了修改,并决定持续维护下去. 先看使用方法: 日期选择: private void showDateDialog(List<Integer> date) { DatePickerDialog.Builder builder = new DatePickerDialog.Builder(this); bui

Android开发中Dialog半透明背景消失_Android

近日,遇到一个Dialog半透明背景消失的问题,背景需求是自定义Dialog实现警告提示框: // 初始化警告弹出框 alertDialog = new EmpAlertView(context, Utils.getIdByName(context, "style", "alert_style")); alertDialog.setCanceledOnTouchOutside(false); LayoutInflater inflater = (LayoutInfl

Android中自定义水平进度条样式之黑色虚线_Android

以下内容给大家介绍Android中自定义水平进度条样式之黑色虚线,对代码实现方法感兴趣的朋友一起学习吧. 布局layout中使用: <ProgressBar android:id="@+id/progress_bar" style="?android:attr/progressBarStyleHorizontal" <!--必须设置为水平--> android:progressDrawable="@drawable/myprogress&