打开时间/日期按钮点击谷歌日历 [英] open time/datePicker on button click like google Calendar does

查看:150
本文介绍了打开时间/日期按钮点击谷歌日历的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想要一个时间和日期选择器弹出,当我点击一个按钮看谷歌日历做,当你按下右下角的fab按钮。将有一个新窗口显示日期和时间,当您点击其中一个时,选择器打开。



我不知道我是否需要创建一个片段有一个时间选择器,只是使尺寸更小,或者我必须通过代码创建一个。



这是我的代码: / p>

public class NewEventActivity扩展AppCompatActivity {

  private LayoutInflater inflater; 
private String [] arraySpinner;
private Button date_from;
private Button date_to;
private Button time_from;
private Button time_to;
私人EditText标题;
私人EditText邀请;



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(今天);
String reportTime = tf.format(今);

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

上的文本

我希望你能得到我的意思



更新:



好的,我解决了这样:

  @Override 
protected Dialog onCreateDialog(int id){
/ / TODO自动生成的方法stub
if(id == 999){
df.format(today);
month = today.getMonth();
year = today.getYear();
day = today.getDay();
DatePickerDialog dlg = new DatePickerDialog(this,myDateListener,year,month,day);
return dlg;
}
返回null;
}

private DatePickerDialog.OnDateSetListener myDateListener = new DatePickerDialog.OnDateSetListener(){
@Override
public void onDateSet(DatePicker arg0,int arg1,int arg2,int arg3){
year = arg1;
month = arg2;
day = arg3;
日期date =新日期(年,月,日);
df.format(date);
reportDate = df.format(date);
date_from.setText(reportDate);
}
};

现在我遇到这个问题:
不知何故当弹出对话框时,会告诉我年116,而我们有星期五的第三。



当我选择一个日期,例如6.10.2016我的按钮显示3800/00/06



所以这意味着它只得到正确的一天。



但为什么?

解决方案

这样的事情 http://www.tutorialspoint .com / android / android_datepicker_control.htm 这是使用日期选择器的标准方式,我有一种感觉,这就是您需要的。


so i want a time and a date picker to pop up when i click on a button look google Calendar does when you press the fab button on the bottom right. There will be a new window which shows up a date and a time and when you click one of them a picker opens.

Im not sure if i need to create a fragment with a time picker in it and just make the size smaller or if i have to create one via code.

This is my code:

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) {


        }
    });


}

}

now when i click on the date_from button a datePicker should appear and after setting the date it should overwrite the text on date_from

i hope you get what i mean

Update:

ok i solved it like this:

@Override
protected Dialog onCreateDialog(int id) {
    // TODO Auto-generated method stub
    if (id == 999) {
        df.format(today);
        month = today.getMonth();
        year = today.getYear();
        day = today.getDay();
        DatePickerDialog dlg = new DatePickerDialog(this, myDateListener, year, month, day);
        return dlg;
    }
    return null;
}

private DatePickerDialog.OnDateSetListener myDateListener = new DatePickerDialog.OnDateSetListener() {
    @Override
    public void onDateSet(DatePicker arg0, int arg1, int arg2, int arg3) {
        year = arg1;
        month = arg2;
        day = arg3;
        Date date = new Date(year,month,day);
        df.format(date);
        reportDate = df.format(date);
        date_from.setText(reportDate);
    }
};

now i got this problems: somehow when the dialog pops up it tells me its the year 116 and that we have Friday the 3rd.

when i select a date then for example 6.10.2016 my Button shows up 3800/00/06

so that means it only gets the correct day.

but why?

解决方案

Something like this http://www.tutorialspoint.com/android/android_datepicker_control.htm ?? This is standard way to use date picker, I have a feeling that that's what you need.

这篇关于打开时间/日期按钮点击谷歌日历的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆