2017-05-22 66 views
0

我正在使用Eclipse來構建一個Android應用程序,我有一個Activity有2個EditTexts,當我點擊它們時,他們會將我重定向到其他活動,然後我將從CalendarView中選擇一個日期並從TimePicker中選擇時間。之後,我將點擊完成,它會將我重新導向到EditText填充的前一個活動。從CalendarView和TimePicker的時間填充EditText

現在,我的問題是,如何使用dd/MMM/yyyy和24hr格式的CalendarView和TimePicker中選擇的日期和時間填充EditText? 我嘗試使用意圖,但我不知道如何編寫

這裏是我的部分代碼:

public class RentStartActivity extends Activity { 
CalendarView calendar1; 
TimePicker time1; 
Button btnCancel1, btnDone1; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_rent_start); 
    btnCancel1 = (Button) findViewById(R.id.btnCancel1); 
    btnCancel1.setOnClickListener(new OnClickListener(){ 
     public void onClick(View v){ 
      Intent cancel = new Intent(RentStartActivity.this, SearchActivity.class); 
      startActivity(cancel); 
     } 
    }); 

    btnDone1 = (Button) findViewById(R.id.btnDone1); 
    btnDone1.setOnClickListener(new OnClickListener(){ 
     public void onClick(View v){ 
      Intent done = new Intent(RentStartActivity.this, SearchActivity.class); 
      done.putExtra("Date1", calendar1.getDate()); 
      done.putExtra("Time", time1.getDrawingTime()); 
      startActivity(done); 
     } 
    }); 
} 
+0

首先確認日期和時間值正確。之後在您的SearchActivity中使用意向獲取數據。 String date = getIntent()。getStringExtra(「Date1」); String time = getIntent()。getStringExtra(「Time」); –

回答

0

使用startActivityForResult() 在SearchActivity:

Intent i = new Intent(this, RentStartActivity.class); 
    startActivityForResult(i, 1); 
在RentStartActivity

btnDone1 = (Button) findViewById(R.id.btnDone1); 
btnDone1.setOnClickListener(new OnClickListener(){ 
    public void onClick(View v){ 
     Intent done = new Intent(); 
     done.putExtra("Date1", calendar1.getDate()); 
     done.putExtra("Time", time1.getDrawingTime()); 
     setResult(Activity.RESULT_OK,done); 
     startActivity(done); 
     finish(); 
    } 
}); 

並獲得數據SearchActivity:

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 

    if (requestCode == 1) { 
     if(resultCode == Activity.RESULT_OK){ 
      String date = data.getStringExtra("Date1"); 
      String time = data.getStringExtra("Time"); 
     } 
     if (resultCode == Activity.RESULT_CANCELED) { 
      //Write your code if there's no result 
     } 
    } 
} 

要格式化日期時間您可以訪問here