android时间选择器xml,【Android】时间选择器 选择日期DatePicker

首先上一下效果图

bd8e9256582d?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

bd8e9256582d?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

布局比较简单 就是一个button以及textView 下面我就直接放代码吧。

package daviddatepickerdemo.qq986945193.com.daviddatepickerdemo;

import java.util.Calendar;

import android.app.Activity;

import android.app.DatePickerDialog;

import android.app.Dialog;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.DatePicker;

import android.widget.TextView;

/**

* 时间选择器

*/

public class MainActivity extends Activity {

int mYear, mMonth, mDay;

Button btn;

TextView dateDisplay;

final int DATE_DIALOG = 1;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

btn = (Button) findViewById(R.id.dateChoose);

dateDisplay = (TextView) findViewById(R.id.dateDisplay);

btn.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

showDialog(DATE_DIALOG);

}

});

final Calendar ca = Calendar.getInstance();

mYear = ca.get(Calendar.YEAR);

mMonth = ca.get(Calendar.MONTH);

mDay = ca.get(Calendar.DAY_OF_MONTH);

}

@Override

protected Dialog onCreateDialog(int id) {

switch (id) {

case DATE_DIALOG:

return new DatePickerDialog(this, mdateListener, mYear, mMonth, mDay);

}

return null;

}

/**

* 设置日期 利用StringBuffer追加

*/

public void display() {

dateDisplay.setText(new StringBuffer().append(mMonth + 1).append("-").append(mDay).append("-").append(mYear).append(" "));

}

private DatePickerDialog.OnDateSetListener mdateListener = new DatePickerDialog.OnDateSetListener() {

@Override

public void onDateSet(DatePicker view, int year, int monthOfYear,

int dayOfMonth) {

mYear = year;

mMonth = monthOfYear;

mDay = dayOfMonth;

display();

}

};

}

xml 布局:

?xml version="1.0" encoding="utf-8"?

android:layout_width="match_parent"

android:layout_height="match_parent"

android:gravity="center"

android:orientation="vertical">

android:id="@+id/dateDisplay"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="时间显示"/>

android:id="@+id/dateChoose"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="选择日期" />

铛铛铛铛~ 代码在这儿就结束啦~~  喜欢的就关注一下我吧~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值