Android 选择日期时间对话框(可选择开始结束时间,已解决弹出键盘问题)

来源:互联网 发布:登录新熊片数据库 编辑:程序博客网 时间:2024/06/06 15:07

直接上代码:
MainActivity:

package wkk.demo6;import android.app.AlertDialog;import android.app.DatePickerDialog;import android.app.TimePickerDialog;import android.content.DialogInterface;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.util.Log;import android.view.LayoutInflater;import android.view.View;import android.view.WindowManager;import android.widget.Button;import android.widget.DatePicker;import android.widget.TimePicker;import java.util.Calendar;public class MainActivity extends AppCompatActivity implements View.OnClickListener {    private Button showdailogTwo;    private Button showdailog;    private Button time;    //选择日期Dialog    private DatePickerDialog datePickerDialog;    //选择时间Dialog    private TimePickerDialog timePickerDialog;    private Calendar calendar;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        showdailogTwo = (Button) findViewById(R.id.showdailogTwo);        showdailog = (Button) findViewById(R.id.showdailog);        time = (Button) findViewById(R.id.time);        time.setOnClickListener(this);        showdailogTwo.setOnClickListener(this);        showdailog.setOnClickListener(this);        calendar = Calendar.getInstance();    }    @Override    public void onClick(View v) {        switch (v.getId()) {            case R.id.showdailog:                showDailog();                break;            case R.id.showdailogTwo:                showDialogTwo();                break;            case R.id.time:                showTime();                break;        }    }    private void showDailog() {        datePickerDialog = new DatePickerDialog(                this, new DatePickerDialog.OnDateSetListener() {            @Override            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {                //monthOfYear 得到的月份会减1所以我们要加1                String time = String.valueOf(year) + " " + String.valueOf(monthOfYear + 1) + "  " + Integer.toString(dayOfMonth);                Log.d("测试", time);            }        },                calendar.get(Calendar.YEAR),                calendar.get(Calendar.MONTH),                calendar.get(Calendar.DAY_OF_MONTH));        datePickerDialog.show();        //自动弹出键盘问题解决        datePickerDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);    }    private void showDialogTwo() {        View view = LayoutInflater.from(this).inflate(R.layout.dialog_date, null);        final DatePicker startTime = (DatePicker) view.findViewById(R.id.st);        final DatePicker endTime = (DatePicker) view.findViewById(R.id.et);        startTime.updateDate(startTime.getYear(), startTime.getMonth(), 01);        AlertDialog.Builder builder = new AlertDialog.Builder(this);        builder.setTitle("选择时间");        builder.setView(view);        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {            @Override            public void onClick(DialogInterface dialog, int which) {                int month = startTime.getMonth() + 1;                String st = "" + startTime.getYear() + month + startTime.getDayOfMonth();                int month1 = endTime.getMonth() + 1;                String et = "" + endTime.getYear() + month1 + endTime.getDayOfMonth();            }        });        builder.setNegativeButton("取消", null);        AlertDialog dialog = builder.create();        dialog.show();        //自动弹出键盘问题解决        dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);    }    private void showTime() {        timePickerDialog = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {            @Override            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {                Log.d("测试", Integer.toString(hourOfDay));                Log.d("测试", Integer.toString(minute));            }        }, calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), true);        timePickerDialog.show();        timePickerDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);    }}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="wkk.demo6.MainActivity">    <Button        android:text="日期选择"        android:id="@+id/showdailog"        android:layout_width="wrap_content"        android:layout_height="wrap_content" />    <Button        android:id="@+id/showdailogTwo"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="日期选择-双" />    <Button        android:id="@+id/time"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="时间选择" /></LinearLayout>

dialog_date.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:gravity="center_horizontal"    android:orientation="vertical">    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginLeft="10dp"        android:layout_marginTop="15dp"        android:text="开始时间" />    <DatePicker        android:id="@+id/st"        android:layout_width="wrap_content"        android:layout_height="190dp"        android:calendarViewShown="false"></DatePicker>    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginLeft="10dp"        android:text="结束时间" />    <DatePicker        android:id="@+id/et"        android:layout_width="wrap_content"        android:layout_height="190dp"        android:calendarViewShown="false"></DatePicker></LinearLayout>

效果如下:

源码下载:
http://download.csdn.net/detail/w18756901575/9525412

0 0
原创粉丝点击