标签:android style blog http io os ar 使用 java
实现效果:
自定义日期实现步骤:
layout布局:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.android_date" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.android_date.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
package com.example.android_date; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Locale; import android.app.Activity; import android.app.DatePickerDialog; import android.app.DatePickerDialog.OnDateSetListener; import android.app.Dialog; import android.os.Bundle; import android.view.View; import android.widget.DatePicker; import android.widget.TextView; import android.widget.Toast; /** * 自定义日期组件 * * @author yazhizhao 20142014-10-23上午11:45:04 */ public class MainActivity extends Activity { private int mMonth; private int mDay; private int mYear; private TextView tvDate1; private TextView tvDate2; private String tvDate1Str; private String tvDate2Str; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tvDate1 = (TextView) findViewById(R.id.tv_date1); tvDate2 = (TextView) findViewById(R.id.tv_date2); // 为日期控件赋值 final Calendar c = Calendar.getInstance(); mYear = c.get(Calendar.YEAR); mMonth = c.get(Calendar.MONTH); mDay = c.get(Calendar.DAY_OF_MONTH); tvDate1.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub showDialog(1); } }); tvDate2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub showDialog(2); } }); } // 重写创建对话框方法 protected Dialog onCreateDialog(int id) { switch (id) { case 1: // 把Activity上下文、日期设置监听器、以及日期的年月日都作为参数传给那个新成立的对话框组件 return new DatePickerDialog(this, dsl, mYear, mMonth, mDay); case 2: // 把Activity上下文、日期设置监听器、以及日期的年月日都作为参数传给那个新成立的对话框组件 return new DatePickerDialog(this, ds2, mYear, mMonth, mDay); } return null; } // 准备日期设置监听器 private OnDateSetListener dsl = new DatePickerDialog.OnDateSetListener() { public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { mYear = year; mMonth = monthOfYear; mDay = dayOfMonth; tvDate1.setText(mYear + "-" + (mMonth + 1) + "-" + mDay); } }; // 准备日期设置监听器 private OnDateSetListener ds2 = new DatePickerDialog.OnDateSetListener() { public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { mYear = year; mMonth = monthOfYear; mDay = dayOfMonth; tvDate2.setText(mYear + "-" + (mMonth + 1) + "-" + mDay); } }; /** * 对比日期的点击事件 * * @author yazhizhao 20142014-10-23上午11:55:11 * @param v */ public void click(View v) { Date time1 = null; Date time2 = null; // 判断时间1和时间2是否为空 tvDate1Str = tvDate1.getText().toString().trim(); tvDate2Str = tvDate2.getText().toString().trim(); if (tvDate1Str == null || "".equals(tvDate1Str)) { Toast.makeText(getApplicationContext(), "时间1不为空", Toast.LENGTH_SHORT).show(); return; } else if (tvDate2Str == null || "".equals(tvDate2Str)) { Toast.makeText(getApplicationContext(), "时间2不为空", Toast.LENGTH_SHORT).show(); return; } // 日期使用"yyyy-MM-dd"格式 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA); try { time1 = sdf.parse(tvDate1Str); time2 = sdf.parse(tvDate2Str); } catch (ParseException e) { e.printStackTrace(); } // 判断时间1,2前后 Date nowdate = new Date(); if (time1.before(nowdate)) { Toast.makeText(getApplicationContext(), "请选择正确的时间1", Toast.LENGTH_SHORT).show(); return; } else if (time2.before(time1)) { Toast.makeText(getApplicationContext(), "请选择正确的时间2", Toast.LENGTH_SHORT).show(); return; } else { Toast.makeText(getApplicationContext(), "ok", Toast.LENGTH_SHORT) .show(); } } }
标签:android style blog http io os ar 使用 java
原文地址:http://blog.csdn.net/zhaoyazhi2129/article/details/40394591