日期选择器。如何为不同的国家设置它

时间:2014-03-23 11:31:42

标签: android calendar datepicker

我创建了一个日期选择器。 这是我的片段:

import java.util.Calendar;

import my.project.mysimplecal.MainActivity.DateDialogFragmentListener;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.Context;
import android.os.Bundle;
import android.widget.DatePicker;

public class DateDialogFragment extends DialogFragment {

public static String TAG = "DateDialogFragment";
static Context mContext; 
static int mYear;
static int mMonth;
static int mDay;
static DateDialogFragmentListener mListener;

public static DateDialogFragment newInstance(Context context,
        DateDialogFragmentListener listener, Calendar now) {
    DateDialogFragment dialog = new DateDialogFragment();
    mContext = context;
    mListener = listener;

    mYear = now.get(Calendar.YEAR);
    mMonth = now.get(Calendar.MONTH);
    mDay = now.get(Calendar.DAY_OF_MONTH);

    Bundle args = new Bundle();
    args.putString("title", "Set Date");
    dialog.setArguments(args);

    return dialog;
}

public Dialog onCreateDialog(Bundle savedInstanceState) {
    return new DatePickerDialog(mContext, mDateSetListener, mYear, mMonth,
            mDay);
}

private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {

    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear,
            int dayOfMonth) {
        mYear = year;
        mMonth = monthOfYear;
        mDay = dayOfMonth;

        mListener.updateChangedDate(year, monthOfYear, dayOfMonth);
    }
};

}

这是我的主要活动

import java.util.Calendar;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.ToggleButton;
import android.app.FragmentTransaction;
import android.graphics.drawable.Drawable;

public class MainActivity extends Activity {
DateDialogFragment frag;
Button btnPickDate;
CompoundButton btnCycleStart;
CompoundButton btnCycleStop;
CompoundButton btnPillStart;
CompoundButton btnPillStop;
Calendar now;
TextView textView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    now = Calendar.getInstance();
    btnPickDate = (Button) findViewById(R.id.pickDate);
    btnCycleStart = (CompoundButton)   findViewById(R.id.cycleStart);
    btnCycleStop = (CompoundButton) findViewById(R.id.cycleStop);
    btnPillStart = (CompoundButton) findViewById(R.id.pillStart);
    btnPillStop = (CompoundButton) findViewById(R.id.pillStop);
    textView = (TextView) findViewById(R.id.showDate);
    textView.setText(String.valueOf(now.get(Calendar.DAY_OF_MONTH)) + "-"
            + String.valueOf(now.get(Calendar.MONTH) + 1) + "-"
            + String.valueOf(now.get(Calendar.YEAR)));

    //Set the calendar button on listening
    btnPickDate.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            showDialog();
        }
    });
    //Set all the Compound button on listening and
    //set one alternative to another. If start is 
    //clicked then stop cannot be clicked
    btnCycleStart
            .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                public void onCheckedChanged(CompoundButton buttonView,
                        boolean isChecked) {
                    if (isChecked) {
                        btnCycleStop.setChecked(false);
                    }
                }
            });
    btnCycleStop
            .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                public void onCheckedChanged(CompoundButton buttonView,
                        boolean isChecked) {
                    if (isChecked) {
                        btnCycleStart.setChecked(false);
                    }
                }
            });
    btnPillStart
            .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                public void onCheckedChanged(CompoundButton buttonView,
                        boolean isChecked) {
                    if (isChecked) {
                        btnPillStop.setChecked(false);
                    }
                }
            });
    btnPillStop
            .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                public void onCheckedChanged(CompoundButton buttonView,
                        boolean isChecked) {
                    if (isChecked) {
                        btnPillStart.setChecked(false);
                    }
                }
            });

}
//Set the text from the fragment class choice
public void showDialog() {
    FragmentTransaction ft = getFragmentManager().beginTransaction();
    frag = DateDialogFragment.newInstance(this,
            new DateDialogFragmentListener() {
                public void updateChangedDate(int year, int month, int day) {
                    textView.setText(String.valueOf(day) + "-"
                            + String.valueOf(month + 1) + "-"
                            + String.valueOf(year));
                    now.set(year, month, day);
                }
            }, now);

    frag.show(ft, "DateDialogFragment");

}

// Listener between the Date Dialog fragment and the
// activity to update the buttons date
public interface DateDialogFragmentListener {
    public void updateChangedDate(int year, int month, int day);
}

}

如何根据用户所处的状态为要显示的日期设置不同的格式?例如在意大利,人们习惯于看日/月/年,但在美国他们习惯看月/日/年。我希望有人能够并且愿意回答,因为我已经搜索了几个小时而没有成功。

1 个答案:

答案 0 :(得分:0)

使用Calendar获取适合当前区域设置的DateFormat.getDateInstance()对象,而不是从DateFormat字段进行自己的格式设置。然后使用format()Date格式化为字符串。您可以使用Calendar在年​​/月/日字段和Date之间进行转换。