2013-07-24 37 views
0

的佈局有什麼辦法重新設置日期選擇器,這樣不但得不到 「MM/DD/YYYY」 你可以得到 「DD/MM/YYYY」,甚至是 「DD/MM/YY」更改的DatePicker

這是我目前的代碼。

import android.app.Activity; 
import android.app.DatePickerDialog; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.DatePicker; 
import android.widget.EditText; 
import android.widget.TextView; 
import android.view.View.OnClickListener; 
import android.app.DatePickerDialog.OnDateSetListener; 
import java.util.Calendar; 


/** 
* Created by MOS182 on 7/21/13. 
*/ 
public class AddReminder extends Activity { 

    TextView Title, Amount, PaymentDate, ReminderDate, ReminderTime; 
    EditText eTitle, eAmount, ePaymentDate, eReminderDate, eReminderTime; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 


     super.onCreate(savedInstanceState); 
     setContentView(R.layout.reminders_dialog); 
     initializeVariables(); 

     ePaymentDate.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       //To show current date in the datepicker 
       Calendar mcurrentDate = Calendar.getInstance(); 
       int mYear = mcurrentDate.get(Calendar.YEAR); 
       int mMonth = mcurrentDate.get(Calendar.MONTH); 
       int mDay = mcurrentDate.get(Calendar.DAY_OF_MONTH); 

       DatePickerDialog mDatePicker; 
       mDatePicker = new DatePickerDialog(AddReminder.this, new OnDateSetListener() { 
        public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday) { 
         // TODO Auto-generated method stub 
        /*  Your code to get date and time */ 
         selectedmonth = selectedmonth + 1; 
         ePaymentDate.setText("" + selectedday + "/" + selectedmonth + "/" + selectedyear); 
        } 
       }, mYear, mMonth, mDay); 
       mDatePicker.setTitle("Select date"); 
       mDatePicker.show(); 
      } 
     }); 


    } 





    private void initializeVariables() 
    { 
     Title = (TextView) findViewById(R.id.tvTitle); 
     Amount = (TextView) findViewById(R.id.tvAmount); 
     PaymentDate = (TextView) findViewById(R.id.tvPaymentDate); 
     ReminderDate = (TextView) findViewById(R.id.tvReminderDate); 
     ReminderTime = (TextView) findViewById(R.id.tvReminderTime); 
     eTitle = (EditText) findViewById(R.id.etTitle); 
     eAmount = (EditText) findViewById(R.id.etAmount); 
     ePaymentDate = (EditText) findViewById(R.id.etPaymentDate); 
     eReminderDate = (EditText) findViewById(R.id.etReminderDate); 
     eReminderTime = (EditText) findViewById(R.id.etReminderTime); 
    } 


} 

這是當我運行我的代碼並選擇ePaymentDate字段時顯示的內容。

enter image description here

回答

2

揀出器採取由用戶選擇的日期格式,這意味着你真的沒有對其進行格式化,因爲可能是用戶享受最給看到他使用的格式至。

我剛剛測試了一個代碼,並在我的手機上以(dd/mm/yyyy)格式顯示日期,因此選取器顯示的格式相同;在模擬器中,我已將日期以mm/dd/yyyy格式顯示,所以選取器顯示相同。

所以有沒有方法來設置顯示格式

但是,如果您仍然真的想要顯示特定的格式,然後參考this link,有一種精心設計的方式來顯示所需格式的日期,但是會改變原始代碼。

+0

我習慣於看到dd/mm/yyyy,但是當我運行模擬器時,它會出現mm/dd/yyyy,當我在手機上運行它時,它會以dd/mm/yyyy運行嗎? –

+0

如果在您的手機上顯示的是dd/mm/yyyy,那麼會顯示爲dd/mm/yyyy。嘗試一下。它應該工作。 –