【问题标题】:Date and time picker in same dialogbox同一对话框中的日期和时间选择器
【发布时间】:2015-01-06 15:14:11
【问题描述】:

如何在同一个对话框中显示日期选择器和时间选择器? 如果我使用

<TimePicker
    android:id="@+id/timePicker1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<DatePicker
    android:id="@+id/datePicker1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

应该在 MainAcitiviy.java 上做什么 我是安卓新手。我搜索了但没有得到任何东西,,,

【问题讨论】:

标签: android xml datepicker datetimepicker


【解决方案1】:

您可以将日期选择器和时间选择器都放在布局 XML 中:Referring this date_time_picker.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:padding="8dp"
android:layout_height="match_parent">

<DatePicker
android:id="@+id/date_picker"
android:layout_width="match_parent"
android:calendarViewShown="true"
android:spinnersShown="false"
android:layout_weight="4"
android:layout_height="0dp" />

<TimePicker
android:id="@+id/time_picker"
android:layout_weight="4"
android:layout_width="match_parent"
android:layout_height="0dp" />

<Button
android:id="@+id/date_time_set"
android:layout_weight="1"
android:layout_width="match_parent"
android:text="Set"
android:layout_height="0dp" />

</LinearLayout>

代码

    final View dialogView = View.inflate(activity, R.layout.date_time_picker, null);
final AlertDialog alertDialog = new AlertDialog.Builder(activity).create();

dialogView.findViewById(R.id.date_time_set).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

         DatePicker datePicker = (DatePicker) dialogView.findViewById(R.id.date_picker);
         TimePicker timePicker = (TimePicker) dialogView.findViewById(R.id.time_picker);

         Calendar calendar = new GregorianCalendar(datePicker.getYear(),
    datePicker.getMonth(),
    datePicker.getDayOfMonth(),
     timePicker.getCurrentHour(),
                            timePicker.getCurrentMinute());

         time = calendar.getTimeInMillis();
         alertDialog.dismiss();
    }});
    alertDialog.setView(dialogView);
    alertDialog.show();

【讨论】:

    【解决方案2】:

    您可以从单个对话框中获取日期和时间。

    使用以下代码:

    new SingleDateAndTimePickerDialog.Builder(getActivity())
     // .bottomSheet() 
     // .curved() .title("Select Date")
     .titleTextColor(getResources().getColor(R.color.white))
     .minutesStep(1)
     .backgroundColor(getResources().getColor(R.color.smokewhite))
     .mainColor(getResources().getColor(R.color.colorAccent))
     .listener(new SingleDateAndTimePickerDialog.Listener()
     {
       @Override public void onDateSelected(Date date)
        {
           // selectpackages();
           String DATE_FORMAT_NOW = "dd-MM-yyyy HH:mm:ss";
           // String DATE_FORMAT_NOW1 = "yyyy-MM-dd HH:mm:ss";
    
           SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
           //  SimpleDateFormat sdf1 = new SimpleDateFormat(DATE_FORMAT_NOW1);
    
           stringDate = sdf.format(date);
    
           getDateDifference(stringDate);
    
           // Toast.makeText(getActivity(),""+stringDate,Toast.LENGTH_SHORT).show();
        }
     }).display();
    

    使用这个依赖: 编译'com.github.florent37:singledateandtimepicker:1.1.0'

    【讨论】:

      【解决方案3】:

      这是一个很好的入门教程:http://www.androidapplicationdevelopment.guru/2014/06/custom-dialog-box-in-android-with-example.html

      只需按照他的操作进行,但使用 TimePicker 和 DatePicker 而不是他的 dialog.xml 放置您的布局(重写它,使其不显示小猫,而是显示您的选择器)

      希望这会有所帮助!

      【讨论】:

        猜你喜欢
        • 2011-08-23
        • 2021-08-11
        • 2016-11-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-07
        • 1970-01-01
        相关资源
        最近更新 更多