【问题标题】:Date picker. How to set it for different State日期选择器。如何为不同的状态设置它
【发布时间】:2014-03-23 11:31:42
【问题描述】:

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

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

}

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

【问题讨论】:

    标签: android calendar datepicker


    【解决方案1】:

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

    【讨论】:

    • 非常感谢,我投票给你的名声太差了,对不起。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多