2012-04-26 60 views
4

當前我正在使用下面的代碼,並且可以獲取當前日期以在文本字段中顯示。不過,我不知道如何顯示選定的日期。如何從CalendarView中提取日期並顯示選定的日期?

我也使用DatePicker,它顯示精選的日期,但我更喜歡CalendarView。由於

Calendar c = Calendar.getInstance(); 
     SimpleDateFormat ss = new SimpleDateFormat("dd-MM-yyyy"); 
     Date date = new Date(); 
     String currentdate= ss.format(date); 
     TextView editText = (TextView) this.findViewById(R.id.textfield1); 
     editText.setText(currentdate); 
+0

你的問題不明確。你想要什麼? – Snicolas 2012-04-26 19:17:57

+0

如果用戶在CalendarView上選擇日期,我想將它存儲在變量中,並將其顯示在下一個活動中,那麼用戶可以得到您選擇的日期消息25-04-2012 – GuyWhoReadsStockoverflow 2012-04-26 19:19:41

+0

當您調用下一個活動時將它傳遞到意圖的額外。否則,請學習如何使用startActivityForResult – Snicolas 2012-04-26 19:22:29

回答

2
中的onCreate

CalendarView v = new CalendarView(this); 
v.setOnDateChangeListener(new CalendarView.OnDateChangeListener() { 
    public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) { 
     this.calendar = new GregorianCalendar(year, month, dayOfMonth); 
    }//met 
}); 
+0

我得到此錯誤日曆 日曆無法解析或不是字段 – GuyWhoReadsStockoverflow 2012-04-26 19:46:05

+1

大聲笑。所以你可以創建它! :)日食中的CTRL + 1 ... – Snicolas 2012-04-26 19:48:23

0
Calendar c = Calendar.getInstance(); 

CalendarCalendarView不同。 CalendarViewView的後裔,所以它有許多事件處理程序,如onClickListener。也許這就是你想要找到的東西?

+0

會像這樣的工作: CalendarView calendarmy =(CalendarView)findViewById(R.id.calendarView1); – GuyWhoReadsStockoverflow 2012-04-26 19:38:01

2
CalendarView view = new CalendarView(this); 
setContentView(view); 

view.setOnDateChangeListener(new OnDateChangeListener() { 

    @Override 
    public void onSelectedDayChange(CalendarView arg0, int year, int month, 
     int date) { 
     Toast.makeText(getApplicationContext(),date+ "/"+month+"/"+year,4000).show(); 
    } 
}); 

這將顯示用戶選擇的日期。

0
CalendarView view = new CalendarView(this); 
setContentView(view); 

view.setOnDateChangeListener(new OnDateChangeListener() { 

    @Override 
    public void onSelectedDayChange(CalendarView arg0, int year, int month, 
     int date) { 
     Toast.makeText(getApplicationContext(),date+ "/"+month+"/"+year,4000).show(); 
    } 
}); 
1
Calendar c = Calendar.getInstance(); 
    SimpleDateFormat ss = new SimpleDateFormat("dd-MM-yyyy"); 
    Date date = new Date(); 
    String currentdate= ss.format(date); 
    TextView editText = (TextView) this.findViewById(R.id.textfield1); 
    editText.setText(currentdate); 

上面一個是下面的代碼我們的代碼嘗試

CalendarView cv = (CalendarView)findViewById(R.id.cv1); 
Calendar c = Calendar.getInstance(); 
    SimpleDateFormat ss = new SimpleDateFormat("dd-MM-yyyy"); 
    Date date = new Date(cv); 
    String currentdate= ss.format(date); 
    TextView editText = (TextView) this.findViewById(R.id.textfield1); 
    editText.setText(currentdate); 
+1

添加一個解釋你做什麼來解決問題將是有幫助的。 – 2014-02-24 05:54:30

+0

日期日期=新日期(cv);已被棄用。 – 2018-01-04 06:19:16

相關問題