2012-07-31 68 views
3

在Android中的DatePicker,在某些OS它顯示月爲「一月,二月,三月......十二月」,在其他它顯示爲1,2,3..12Android的日期選取器每月數

是有一種方法可以使其始終如一地顯示1,2,3 .. 12作爲月份嗎?

顯示1,2,3..12而不是字符串的原因是對各種語言的本地化支持。

也建議這樣做? 謝謝。 enter image description here

+1

該鏈接可以是有益的[DateSlider作爲Android的一種替代的DatePicker](http://blog.codeus.net/dateslider- 1-0-an-alternative-datepicker-for-android /) – Ali 2012-07-31 09:00:09

+0

我認爲這是每個操作系統的用戶界面模式,是否有任何具體的變化原因?如果沒有,那麼不要玩默認的UI模式。 – 2012-07-31 09:07:39

+2

阿里的鏈接已經死亡。請使用:http://web.archive.org/web/20120529063131/http://blog.codeus.net/dateslider-1-0-an-alternative-datepicker-for-android – 2013-01-23 21:41:15

回答

1
<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 
<Button 
    android:id="@+id/btnChangeDate" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Change Date" /> 
<TextView 
    android:id="@+id/lblDate" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Current Date (April-10-2012): " 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 
    <TextView 
    android:id="@+id/tvDate" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="" 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 
    <DatePicker 
    android:id="@+id/dpResult" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" />  

    </LinearLayout> 

日期選擇器類

import java.util.Calendar; 
import java.util.Locale; 
import com.datepicker.R.string; 
import android.app.Activity; 
import android.app.DatePickerDialog; 
import android.app.Dialog; 
import android.os.Bundle; 
    import android.text.format.DateFormat; 
    import android.view.View; 
    import android.view.View.OnClickListener; 
    import android.widget.Button; 
    import android.widget.DatePicker; 
    import android.widget.TextView; 


public class DatepickerActivity extends Activity { 
    private TextView tvDisplayDate; 
    private DatePicker dpResult; 
    private Button btnChangeDate; 
    private int year; 
    private int month; 
    private int day; 
    private String str; 
    public static long UTC (int year, int month, int day, int hour, int minute, int second) { 
     return 1; 
    } 

    static final int DATE_DIALOG_ID = 999; 

    //private static final int August =4; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
    setCurrentDateOnView(); 
     addListenerOnButton(); 
    } 

    // display current date 
    public void setCurrentDateOnView() { 

     tvDisplayDate = (TextView) findViewById(R.id.tvDate); 
     dpResult = (DatePicker) findViewById(R.id.dpResult); 

     final Calendar c = Calendar.getInstance(); 
     year = c.get(Calendar.YEAR); 
     month = c.get(Calendar. MONTH); 
     day = c.get(Calendar.DAY_OF_MONTH); 
    // str=c.getDisplayName(c.MONTH, 2, Locale.US); 

       // set current date into textview 
     tvDisplayDate.setText(new StringBuilder() 
     // Month is 0 based, just add 1 
     .append(month + 1).append("-").append(day).append("-") 
     .append(year).append(" ")); 

     // set current date into datepicker 
     dpResult.init(year, month, day, null); 
    } 
    public void addListenerOnButton() { 

     btnChangeDate = (Button) findViewById(R.id.btnChangeDate); 
     btnChangeDate.setOnClickListener(new OnClickListener() { 


      public void onClick(View v) { 
       showDialog(DATE_DIALOG_ID); 
      } 
     }); 
    } 

    @Override 
    protected Dialog onCreateDialog(int id) { 
     switch (id) { 
      case DATE_DIALOG_ID: 
       // set date picker as current date 
       return new DatePickerDialog(this, datePickerListener,year, month,day); 
     } 
     return null; 
    } 
    private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() { 

     // when dialog box is closed, below method will be called. 
     public void onDateSet(DatePicker view, int selectedYear,int selectedMonth, int selectedDay) { 
      year = selectedYear; 
      month = selectedMonth; 
      day = selectedDay; 

      // set selected date into textview 
      tvDisplayDate.setText(new StringBuilder().append(str + 1) 
        .append("-").append(day).append("-").append(year) 
        .append(" ")); 

      // set selected date into datepicker also 
      dpResult.init(year, month, day, null); 
     } 
    }; 

}

相關問題