1. 程式人生 > >Android 選擇日期時間對話方塊(可選擇開始結束時間,已解決彈出鍵盤問題)

Android 選擇日期時間對話方塊(可選擇開始結束時間,已解決彈出鍵盤問題)

直接上程式碼:
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>

效果如下: