当前位置: 代码迷 >> Android >> android日期控件展示
  详细解决方案

android日期控件展示

热度:55   发布时间:2016-05-01 10:47:56.0
android日期控件显示


  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/starttime"/>

?<EditText android:layout_width="fill_parent"
???? android:layout_height="wrap_content"
???? android:focusable="true"
???? android:focusableInTouchMode="true"
???? android:id="@+id/starttime"/>
?
???? <TextView android:layout_width="fill_parent"
??????? android:layout_height="wrap_content"
??????? android:text="@string/endstime"/>
?
??<EditText android:layout_width="fill_parent"
???? android:layout_height="wrap_content"
???? android:focusable="true"
???? android:focusableInTouchMode="true"
???? android:id="@+id/endtime"/>

</LinearLayout>

?

?

2.strings.xml文件

<?xml version="1.0" encoding="utf-8"?>
<resources>

??? <string name="hello">Hello World, MainActivity!</string>
??? <string name="app_name">日期控件使用</string>
?<string name="starttime">开始时间</string>
?<string name="endstime">结束时间</string>
</resources>

?

3.activity文件

import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.Dialog;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.DatePicker;
import android.widget.EditText;

public class MainActivity extends Activity {
?
?private EditText startText;
?
?private EditText endText;
?
?private Calendar cdar=Calendar.getInstance();
?private int mYear=cdar.get(Calendar.YEAR);
?private int mMonth=cdar.get(Calendar.MONTH);
?private int mDay=cdar.get(Calendar.DATE);
?
?
??? @Override
??? public void onCreate(Bundle savedInstanceState) {
??????? super.onCreate(savedInstanceState);
??????? setContentView(R.layout.main);
??????? startText=(EditText)findViewById(R.id.starttime);
??????? endText=(EditText)findViewById(R.id.endtime);
???????
??????? //注册点击时间的监听
??????? startText.setOnClickListener(new MyOnClickListener(startText.getId()));
??????? endText.setOnClickListener(new MyOnClickListener(endText.getId()));
???????
??????? //焦点改变时的监听
??????? startText.setOnFocusChangeListener(new MyOnFocusChangeListener(startText.getId()));
??????? endText.setOnFocusChangeListener(new MyOnFocusChangeListener(endText.getId()));
???????
??? }
???
??? // 隐藏手机键盘
??? private void hideIM(View edt){
??????? try {
??????????? InputMethodManager im = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
??????????? IBinder windowToken = edt.getWindowToken();
??????????? if (windowToken != null) {
??????????????? im.hideSoftInputFromWindow(windowToken, 0);
??????????? }
??????? }catch (Exception e) {
??????????? e.printStackTrace();
??????? }
??? }

??? /**
???? * 创建要显示的对话框
???? */
??? protected Dialog onCreateDialog(int id) {
???
??????? switch (id) {??
??????????? case R.id.starttime:
??????????????? return new DatePickerDialog(this, new MyOnDateSetListener(id), mYear, mMonth, mDay);
??????????? case R.id.endtime:
??????????????? return new DatePickerDialog(this, new MyOnDateSetListener(id), mYear, mMonth, mDay);
??????? }
??????? return null;
??? }
???
???
??? /**
???? * 点击事件的监听
???? * @author sweet
???? *
???? */
??? private final class MyOnClickListener implements OnClickListener{
??? ?private int pointId;
??? ?public MyOnClickListener(int id){
??? ??this.pointId=id;
??? ?}
[email protected]
??public void onClick(View v) {
??? hideIM(v);
???showDialog(pointId);
??}
??? ?
??? }
???
??? /**
???? * 焦点改变时的监听对象
???? * @author sweet
???? *
???? */
??? private final class MyOnFocusChangeListener implements OnFocusChangeListener{
??? ?private int pointId;
??? ?public MyOnFocusChangeListener(int id){
??? ??this.pointId=id;
??? ?}

[email protected]
??public void onFocusChange(View v, boolean hasFocus) {
???if (hasFocus) {
???????????? hideIM(v);
???????????? showDialog(pointId);?
???????? }
??}
??? ?
??? }
???
??? /**
???? * 选择日期控件
???? * @author sweet
???? *
???? */
??? private final class MyOnDateSetListener implements OnDateSetListener{
??? ?private int pointId;
??? ?public MyOnDateSetListener(int id){
??? ??this.pointId=id;
??? ?}
??? ?
[email protected]
??public void onDateSet(DatePicker view, int year, int monthOfYear,int dayOfMonth) {
???????????? String mm = monthOfYear<=9 ? "0" + (monthOfYear + 1) : String.valueOf(monthOfYear + 1);
???????????? String dd=dayOfMonth <= 9 ? "0" + dayOfMonth : String.valueOf(dayOfMonth);
???????????? ((EditText)findViewById(pointId)).setText(String.valueOf(year) + "-" + mm + "-" + dd);
??}
??? }
}

?

?

?

  相关解决方案