2016-10-02 75 views
0

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

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

這是我的代碼:

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文本

我希望你明白我的意思

回答

0

嘿,通過使用這兩種方法,你可以解決你的問題。只需在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 + ""); 

    } 

} 

希望這會對你有幫助。

+0

@zooky你解決了你的問題嗎? – Saveen