Android日期选择器月份为数字 [英] Android Date Picker month as number

查看:59
本文介绍了Android日期选择器月份为数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Android DatePicker中,在某些操作系统上,其月份显示为"Jan,Feb,Mar ... Dec",在其他操作系统上,其显示为1,2,3..12

In Android DatePicker, in some OS it shows Month as "Jan, Feb, Mar...Dec", on other it shows as 1,2,3..12

有没有办法使它始终保持一致,以使其始终以月为单位显示1,2,3 .. 12?

Is there a way to make it consistent througthout so that it should display 1,2,3.. 12 always as month?

显示1,2,3..12而不是字符串的原因是对各种语言的本地化支持.

Reason for displaying 1,2,3..12 instead of string is the localization support for various kind of languages.

也建议这样做吗? 谢谢.

Also is it advisable to do so? Thanks.

推荐答案

  <?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);
        }
    };

}

这篇关于Android日期选择器月份为数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆