簡體   English   中英

如何僅使用點擊事件和意圖將當前日期從一個活動傳遞到android中的下一個活動

[英]How to Pass Current Date from One activity to next activity in android only using click event and intent

如何僅使用click事件和意圖將android中的當前日期從一個活動傳遞到下一個活動(將當前日期傳遞到soap服務)。

From the current activity:
Intent intent = new Intent(this, NextActivity.class);
intent.putExtra("date", dateObj.getTime());
startActivity(intent);
From next activity:

Date dateObj = new Date(getIntent().getExtras().getLongExtra("date", -1));

您應該將時間作為長值傳遞,然后將其轉換為其他活動,以避免由於不同設備上的字符串問題而引起的日期解析。

public String getCurrentDate() {

    Time time = new Time();
    time.setToNow();
    time.month = time.month + 1;

    String date = String.valueOf(time.year) + "-"
            + String.valueOf(time.month) + "-"
            + String.valueOf(time.monthDay);
    return date;
}

在您的onClick偵聽器中添加此代碼

Intent intent = new Intent(this, NextActivity.class);
intent.putExtra("date", getCurrentDate());
startActivity(intent);

在您的下一個活動的onCreate()中添加此代碼。

String date=getIntent().getStringExtra("date");

You can parse this date into Date object

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM