2016-09-29 80 views
0

所以我想要一個時間和日期選取器彈出,當我點擊一個按鈕看起來谷歌日曆時,當你按右下方的晶圓廠按鈕。將會有一個新窗口顯示日期和時間,當你點擊其中一個選擇器打開時。打開時間/ datePicker按鈕點擊像谷歌日曆做

林不知道如果我需要創建一個片段與時間選擇器在其中,只是使尺寸更小或如果我必須通過代碼創建一個。

這是我的代碼:

公共類NewEventActivity擴展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按鈕單擊日期選擇器應該會出現和設定後它應該覆蓋文本的日期date_from

我希望你明白我的意思

更新:

好,我解決了這個問題是這樣的:

@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); 
    } 
}; 

現在我得到這個問題: 不知何故,當對話框彈出,它告訴我是116年,我們是星期五的第三。

當我選擇一個日期,然後例如2016年6月10日我的按鈕顯示出來3800/00/06

因此,這意味着它只是變得正確的日期。

但是爲什麼?

回答