[英]How to pass date selected from CalendarView (current as default) into next Activity?
我當前有一個CalendarView活動,我希望用戶從CalendarView中選擇要傳遞給下一個片段的日期,從而該日期將顯示在TextView中。 我已經看了很多遍,但是我不知道如何將日期提取到字符串中,然后在活動將傳遞意圖的片段中再次檢索它。
我嘗試參考此答案: Android CalendarView:如何獲取正確格式的日期? 並且我設法在LogCat中顯示了日期,但它沒有傳遞給片段。 如果可能的話,有人可以告訴我如何重新調整代碼以將其傳遞給片段嗎? 非常感謝你們!
如果在MainActivity中保留對Fragment
的引用,則可以像與其他任何對象一樣與其進行交互。 一個例子:
MainActivity.java
MyFragment my_fragment;
...
public void onCreate() {
...
my_fragment = new MyFragment();
...
}
public void sendDateToFragment(Calendar date) {
my_fragment.setDate(date);
}
MyFragment.java
Calendar calendar;
...
public void setDate(Calendar date) {
// You can choose any type of object to accept,
// this is just an example.
calendar = date;
}
希望這可以幫助。
您可以通過捆綁包處理:
public void send(int day, int month, int year){
Bundle date = new Bundle();
date.putInt("Day", day);
date.putInt("Month", month);
date.putInt("Year", year);
Intent in=new Intent(Activity.this,SecondActivity.class);
in.putExtras(date);
startActivity(in);
}
在第二個活動中:
public Date get(){
Bundle second = getIntent().getExtras();
return new Date(second.getInt("Year"),
second.getInt("Month"),second.getInt("Day"));
}
它會返回您的日期,您可以調用第二個活動Date date = get();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.