【问题标题】:Date picker on click (Like Google Calendar )点击日期选择器(如 Google 日历)
【发布时间】:2016-10-02 15:07:29
【问题描述】:

所以我希望在单击按钮时弹出一个时间和日期选择器,当您按下右下角的 fab 按钮时,谷歌日历会弹出一个按钮。将出现一个显示日期和时间的新窗口,当您单击其中一个时,将打开一个选择器。

我不确定我是否需要在其中创建一个带有时间选择器的片段并缩小尺寸,或者我是否必须通过代码创建一个。

这是我的代码:

public class NewEventActivity extends AppCompatActivity {

    private LayoutInflater inflater;
    private String[] arraySpinner;
    private Button date_from;
    private Button date_to;
    private Button time_from;
    private Button time_to;
    private EditText title;
    private EditText invite;



    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.new_event_fragment);
        findviewbyids();
        initPickers();

    }

    public void findviewbyids(){
        date_from    = (Button) findViewById(R.id.bt_date_from);
        time_from    = (Button) findViewById(R.id.bt_time_from);
        date_to  = (Button) findViewById(R.id.bt_date_to);
        time_to  = (Button) findViewById(R.id.bt_time_to);
        title  = (EditText) findViewById(R.id.et_title);
        invite  = (EditText) findViewById(R.id.et_invite);

    }

    public void initPickers(){
        Date today = new Date();
        DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
        DateFormat tf = new SimpleDateFormat("HH:mm:ss");


        String reportDate = df.format(today);
        String reportTime = tf.format(today);

        date_from.setText(reportDate);
        time_from.setText(reportTime);
        date_to.setText(reportDate);
        time_to.setText(reportTime);

        date_from.setVisibility(View.VISIBLE);
        date_to.setVisibility(View.VISIBLE);
        time_from.setVisibility(View.VISIBLE);
        time_to.setVisibility(View.VISIBLE);

        date_from.setBackgroundColor(Color.TRANSPARENT);
        date_to.setBackgroundColor(Color.TRANSPARENT);
        time_from.setBackgroundColor(Color.TRANSPARENT);
        time_to.setBackgroundColor(Color.TRANSPARENT);

        Spinner group = (Spinner) findViewById(R.id.sp_group);

        this.arraySpinner = new String[] {
                "1", "2", "3", "4", "5"
        };

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, arraySpinner);
        group.setAdapter(adapter);


        date_from.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

            }
        });
    }
}

现在,当我单击 date_from 按钮时,应该会出现一个 datePicker,并且在设置日期后它应该会覆盖 date_from

上的文本

我希望你明白我的意思

【问题讨论】:

    标签: android datepicker calendar


    【解决方案1】:

    嘿,通过使用这两种方法,您可以解决您的问题。只需在 textview 或 edittext 中设置日期。

    第二种方式你可以使用this customized 日期选择器。

    void showDatePicker() {
    
        DatePickerDialog datePickerDialog = new DatePickerDialog(
                activity,
                DatePickerDialog.THEME_HOLO_DARK, new mDateSetListener(),
                mYear, mMonth, mDay);
        datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000L * 60 * 60 * 24 * (36524)
                / 100 * 84);
    
        datePickerDialog.getDatePicker().setDescendantFocusability(
                DatePicker.FOCUS_BLOCK_DESCENDANTS);
    
        datePickerDialog.getDatePicker().setMaxDate(System.currentTimeMillis());
    
        datePickerDialog.setCancelable(false);
        datePickerDialog.setCanceledOnTouchOutside(false);
        datePickerDialog.show();
    
    }
    
    
    class mDateSetListener implements DatePickerDialog.OnDateSetListener {
    
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear,
                              int dayOfMonth) {
            mYear = year;
            mMonth = monthOfYear;
            mDay = dayOfMonth;
    
            mDateBirth.setText((mDay >= 10 ? mDay + "" : "0" + mDay)
                    + ""
                    + "/"
                    + ((mMonth + 1) >= 10 ? (mMonth + 1) + "" : "0"
                    + (mMonth + 1)) + "" + "/" + mYear + "");
    
        }
    
    }
    

    希望这会对你有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-25
      • 1970-01-01
      • 2016-10-24
      相关资源
      最近更新 更多