Android 日期选择器。如何设置不同的状态

Android 日期选择器。如何设置不同的状态,android,calendar,datepicker,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 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);
}

}

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

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

非常感谢您,我对您的投票声誉太差了,对不起。