日历
来源:互联网 发布:php实现uv pv ip统计 编辑:程序博客网 时间:2024/06/06 23:18
先看效果:
1.MainActivity:
package com.example.administrator.datedemo;import android.app.DatePickerDialog;import android.app.Dialog;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.DatePicker;import android.widget.TextView;import java.util.Calendar;public class MainActivity extends AppCompatActivity { private TextView mStart, mEnd; final int DATE_DIALOG = 1; final int END_DIALOG = 2; private int sYear, sMonth, sDay; private int eYear, eMonth, eDay; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); setListener(); } private void initView() { mStart = (TextView) findViewById(R.id.start); mEnd = (TextView) findViewById(R.id.end); } private void setListener() { mStart.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { showDialog(DATE_DIALOG); } }); final Calendar calendar = Calendar.getInstance(); sYear = calendar.get(Calendar.YEAR); sMonth = calendar.get(Calendar.MONTH); sDay = calendar.get(Calendar.DAY_OF_MONTH); mEnd.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { showDialog(END_DIALOG); } }); final Calendar ca = Calendar.getInstance(); eYear = ca.get(Calendar.YEAR); eMonth = ca.get(Calendar.MONTH); eDay = ca.get(Calendar.DAY_OF_MONTH); } @Override protected Dialog onCreateDialog(int id) { switch (id) { case DATE_DIALOG: return new DatePickerDialog(this, listener, sYear, sMonth, sDay); case END_DIALOG: return new DatePickerDialog(this, endListener, eYear, eMonth, eDay); } return null; } private DatePickerDialog.OnDateSetListener listener = new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker datePicker, int year, int month, int day) { sYear = year; sMonth = month; sDay = day; mStart.setText(new StringBuffer().append(sYear).append("/").append(sMonth + 1).append("/").append(sDay)); } }; private DatePickerDialog.OnDateSetListener endListener = new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker datePicker, int year, int month, int day) { eYear = year; eMonth = month; eDay = day; mEnd.setText(new StringBuffer().append(eYear).append("/").append(eMonth + 1).append("/").append(eDay)); } };}2.布局:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.administrator.datedemo.MainActivity"> <TextView android:id="@+id/start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerInParent="true" android:background="#ff0" android:text="开始时间!" /> <TextView android:id="@+id/end" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/start" android:layout_centerHorizontal="true" android:layout_marginTop="16dp" android:background="#f0f" android:text="结束时间!" /></RelativeLayout>
阅读全文
0 0
- 日历
- 日历
- 日历
- 日历
- 日历
- 日历
- 日历
- 日历
- 日历
- 日历
- 日历
- 日历
- 日历
- 日历
- 日历
- 日历
- 日历
- 日历
- Idea激活方式
- Java
- Android零基础入门第86节:探究Fragment生命周期
- android 8种对话框(Dialog)使用方法汇总
- BZOJ1854: [Scoi2010]游戏
- 日历
- python中NumPy的常见用法
- 【Linux】Mysql初始化root密码和允许远程访问
- freemarker在servlet中的使用
- You're using the staticfiles app without having set the STATIC_ROOT setting to a filesystem path.
- 最长递增子序列
- C++ 控制台打印无输出 无法查找或打开PDB文件
- Spring集成redis缓存
- 【1】coreML入门之结合ARKit场景展示