当前位置: 首页 > 知识库问答 >
问题:

Android Studio-我想通过Dialogfragment中的按钮显示一个datepicker

堵茂勋
2023-03-14

我想我的问题是我有两个“OnCreateDialog”,一个用于myDialogfragment,一个用于日期选择器。

有人能帮忙或建议吗?

共有1个答案

艾望
2023-03-14

对于日期和时间,我们将创建两个片段类。
DatePickerFragment

package com.shashank;

import android.annotation.SuppressLint;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.widget.DatePicker;
import android.widget.TextView;

import java.util.Calendar;

@SuppressLint("ValidFragment")
public class DatePickerFragment extends DialogFragment implements 
DatePickerDialog.OnDateSetListener {
private TextView tvdate;

@SuppressLint("ValidFragment")
public DatePickerFragment(TextView etIncomeDate) {
    this.tvdate = etIncomeDate;
}


@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    Calendar c = Calendar.getInstance();
    int year = c.get( Calendar.YEAR );
    int month = c.get( Calendar.MONTH);
    int day = c.get( Calendar.DAY_OF_MONTH );
    return new DatePickerDialog(getActivity(), (DatePickerDialog.OnDateSetListener) this, year, month, day);
}

@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
   populateSetDate(year, month+1, dayOfMonth );
}
public void populateSetDate(int year, int month, int day) {
    if(day<=9 && month <=9)
    {
        tvdate.setText(year+"-"+"0"+month+"-"+"0"+day);
    }
    else if(day<=9 && month>9)
    {
        tvdate.setText(year+"-"+month+"-"+"0"+day);
    }
    else if(day>9 && month<=9){
        tvdate.setText(year+"-"+"0"+month+"-"+day);
    }
    else{
        tvdate.setText(year+"-"+month+"-"+day);
    }
}
}


TimePickerFragment

package com.shashank;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.widget.TextView;
import android.widget.TimePicker;

import java.util.Calendar;

@SuppressLint("ValidFragment")
public class TimePickerFragment extends DialogFragment implements 
TimePickerDialog.OnTimeSetListener{
private TextView tvTime;
private Calendar c;

@SuppressLint("ValidFragment")
public TimePickerFragment(TextView etIncomeTime) {
    this.tvTime = etIncomeTime;
}

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
     c = Calendar.getInstance();
    int hour = c.get( Calendar.HOUR_OF_DAY );
    int minute = c.get( Calendar.MINUTE );
    return new TimePickerDialog( getActivity(), (TimePickerDialog.OnTimeSetListener) this, hour, minute, android.text.format.DateFormat.is24HourFormat(getContext()) );
}


@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
    populateSetTime(hourOfDay, minute);
}

public void populateSetTime(int hour, int minute) {
    if(minute<=9 ||minute==0)
    tvTime.setText(hour+":0"+minute);
    else
        tvTime.setText( hour+":"+minute );
}
public String checkAmPm(){
    String am_pm = " ";
    if (c.get(Calendar.AM_PM) == Calendar.AM)
        am_pm = "AM";
    else if (c.get(Calendar.AM_PM) == Calendar.PM)
        am_pm = "PM";

    return am_pm;
}
}


现在创建一个带有布局的片段类,我们可以在其中放置日期和时间选择器。

<?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:orientation="vertical"
android:padding="16dp">

<LinearLayout
    android:layout_width="300dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginBottom="20dp">
    <TextView
        android:id="@+id/etDate"
        android:layout_width="180dp"
        android:layout_height="45dp"
        android:layout_gravity="center"
        android:ems="10"
        android:gravity="center"
        android:hint="DD/MM/YYYY"
        android:inputType="date"
        android:padding="5dp"
        android:textSize="18sp" />
    <TextView
        android:layout_width="90dp"
        android:layout_height="45dp"
        android:text="Date"
        android:layout_gravity="center"
        android:gravity="center"
        android:textSize="18sp"
        android:id="@+id/etHintDate"/>

</LinearLayout>

<LinearLayout
    android:layout_width="300dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center">
    <TextView
        android:id="@+id/etTime"
        android:layout_width="180dp"
        android:layout_height="45dp"
        android:layout_gravity="center"
        android:ems="10"
        android:hint="HH:MM"
        android:gravity="center"
        android:inputType="date"
        android:padding="5dp"
        android:textSize="18sp"
        android:layout_marginRight="5dp"
        />
    <TextView
        android:layout_width="85dp"
        android:layout_height="45dp"
        android:text="Time"
        android:layout_gravity="center"
        android:gravity="center"
        android:textSize="18sp"
        android:id="@+id/etHintTime"/>

</LinearLayout>

</LinearLayout>
package com.shashank;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.shashank.DatePickerFragment;
import com.shashank.TimePickerFragment;

import java.util.Calendar;
import java.util.List;

public class HomeFragment extends Fragment {

String date, time = null;

final String DATE_PATTERN = "(0?[1-9]|1[012]) [/.-] (0?[1-9]|[12][0-9]|3[01]) [/.-] ((19|20)\\d\\d)";
TextView etDate, etTime, etHintDate, etHintTime;
private  int year, month , day, hour, minute;


public HomeFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    getActivity().setTitle("Date Time Picker");
    // Inflate the layout for this fragment
    View view = inflater.inflate( R.layout.fragment_home,container, false );


    etDate = (TextView) view.findViewById( R.id.etDate );
    etTime = (TextView) view.findViewById( R.id.etTime );

    etHintDate= (TextView) view.findViewById( R.id.etHintDate);
    etHintTime = (TextView) view.findViewById( R.id.etHintTime );

    Calendar cal = Calendar.getInstance();
    int year = cal.get( Calendar.YEAR );
    int month = cal.get( Calendar.MONTH);
    int day = cal.get( Calendar.DAY_OF_MONTH );
    int hour = cal.get( Calendar.HOUR_OF_DAY );
    int minute = cal.get( Calendar.MINUTE );
    etDate.setText( year+"-"+"0"+month+"-"+day );
    etTime.setText( hour+":"+minute );


        @Override
        public void onClick(View view) {
            date = etDate.getText().toString();
            time = etTime.getText().toString();
        }


    etHintTime.setOnClickListener( new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            DialogFragment newFragment = new TimePickerFragment(etTime);
            newFragment.show(getFragmentManager(), "TimePicker");


        }
    } );

    etHintDate.setOnClickListener( new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            DialogFragment newFragment = new DatePickerFragment(etDate);
            newFragment.show(getFragmentManager(), "DatePicker");

        }
    } );


    return view;
}
}
import com.shashank.DatePickerFragment;
import com.shashank.TimePickerFragment;
 类似资料:
  • 我需要帮助创建一个div,当你点击一个按钮时,里面有自定义超文本标记语言的div会被创建。 如果这是可能的(很可能是),我正在制作一个邮箱。 谢谢!!!

  • 我正在尝试网络抓取,我需要模拟对buttoN的点击,我已经尝试过了: 并返回此错误: selenium . common . exceptions . nosuchelementexception:消息:没有这样的元素:找不到元素:{"method":"xpath "," selector ":"//*[@ id = " CTL 00 _ CP h1 _ BtnTipoGobierno "]" }

  • 这是我的代码: 我所要做的就是使esc按钮与backspace按钮完全相同。所以我想删除

  • 所以假设我有一个“开”和一个“关”按钮。当我按下打开按钮时,我希望打开按钮隐藏自己,关闭按钮显示出来,反之亦然。 一个人怎么能这么做?

  • 嘿,我正在做一个java项目,我会试着把我的问题概括出来... 我有一个jpanelX,其中包含juckons1到5。所有这些按钮都连接到相同的actionlistener和相同的操作执行方法。我还将点击的按钮的来源保存到全局字符串变量中。 我有另一个JpanelY。JpanelY包含字符串数组。 我想将这两种行为联系起来: > 用户点击JpanelX上的按钮 显示的是JpanelY而不是Jpan

  • 我想创建函数时,我把任何输入(我有6),将显示一个按钮搜索,当字段为空,相同的按钮将被隐藏。现在,我的问题是,当我写的时候,我得到了我在file.js和.jsp中声明的两个按钮(搜索并继续)。 我想要一个帮助只为这两个表达式: 而且 您可以在这里找到代码-->https://jsfidle.net/fm1y5c8v/1/ null null thnx,