zoukankan      html  css  js  c++  java
  • 自定义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)

  • 相关阅读:
    Spring实战(第4版).pdf
    java-spring基于redis单机版(redisTemplate)实现的分布式锁+redis消息队列,可用于秒杀,定时器,高并发,抢购
    docker(ubuntu)中安装cron运行crontab定时任务
    java8中日期字符串的月日时分秒自动补零
    img垂直居中div
    使用InstallAnywhere7.1制作Java exe程序安装包
    在IDEA中将项目部署到Tomcat的方法及两种模式的区别
    将Java web应用部署到Tomcat 及部署到Tomcat根目录 的三种方式
    MySQL 5.7.19 忘记密码 重置密码 配置文件my.ini示例 服务启动后停止 log配置
    windows 同时启动多个Tomcat 控制台日志(startup.bat)输出到指定文件中
  • 原文地址:https://www.cnblogs.com/ct732003684/p/2856881.html
Copyright © 2011-2022 走看看