android之datepicker控件的用法
作者:jingxian 时间:2021-12-26 04:07:31
如下所示:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/textViewDateDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/btnPickDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change the date"
/>
</LinearLayout>
package com.example.mars_2200_datepicker;
import java.util.Calendar;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
private TextView textViewDateDisplay;
private Button btnPickDate;
private int mYear;
private int mMonth;
private int mDay;
static final int DATE_DIALOG_ID = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textViewDateDisplay = (TextView) findViewById(R.id.textViewDateDisplay);
btnPickDate = (Button) findViewById(R.id.btnPickDate);
btnPickDate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
}
});
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
updateDisplay();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
private void updateDisplay() {
textViewDateDisplay.setText(new StringBuilder().append(mYear).append("-")
.append(mMonth + 1).append("-").append(mDay).append(" "));
}
private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
updateDisplay();
}
};
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(this, mDateSetListener, mYear, mMonth,
mDay);
}
return null;
}
}
标签:android,datepicker
0
投稿
猜你喜欢
springboot 定时任务@Scheduled实现解析
2023-11-10 16:18:24
Android编程使用Fragment界面向下跳转并一级级返回的实现方法
2021-08-20 19:56:51
C#中Timer使用及解决重入问题
2022-02-21 00:44:52
Java日期操作方法工具类实例【包含日期比较大小,相加减,判断,验证,获取年份等】
2023-11-25 12:14:40
Java泛型定义与用法实例详解
2023-11-25 11:50:28
Hashmap非线程安全关于hash值冲突处理
2023-11-11 09:22:10
spring boot如何实现切割分片上传
2023-06-29 02:49:26
Android实现基于滑动的SQLite数据分页加载技术(附demo源码下载)
2022-04-21 03:42:02
修改Maven settings.xml 后配置未生效的解决
2023-07-06 22:17:03
Android实现计时与倒计时的常用方法小结
2023-10-28 07:18:02
微信开发--自定义菜单查询返码乱码的解决方法
2023-11-25 04:47:55
mybatis-plus 使用Condition拼接Sql语句各方法的用法
2022-07-06 03:51:12
JAVA算法起步之插入排序实例
2021-05-30 15:52:30
详解Java目录操作与文件操作教程
2023-12-05 13:45:48
C#提取网页中超链接link和text部分的方法
2023-02-24 02:04:14
Android中实现TCP和UDP传输实例
2021-11-08 14:19:59
Java聊天室之实现获取Socket功能
2023-09-19 03:57:10
java中方法递归的简单示例
2022-11-25 11:45:12
JAVA 实现二叉树(链式存储结构)
2022-02-18 11:35:21
解决android studio 打开java文件 内容全变了的问题
2022-03-18 19:31:44