我是Android编程新手,我使用的是使用DialogFragment创建的基本Datepicker。我的DatePicker在点击等方面显示正常,但问题是它也显示了日历视图。我不想要这个。我一直在寻找一天的解决方案,我现在不愿意使用自定义Datepickers,因为我想先熟悉基础知识。
我还阅读了诸如
之类的建议yourDatepicker.setCalendarViewShown(false);
或在XML中将其设置为false。但是我的DatePicker来自DialogFragment,那么如何访问这个日期选择器呢?如何设置其最终视图?我不想在源代码中进行更改,因为我仍在学习绳索。
我的日期选择代码:
public class DueDatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(getActivity(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day){
// Code to set Due Date TextField to the selected date.
TextView reminderText = (TextView)getActivity().findViewById(R.id.addTaskFormDueDateHolder);
String dueDateStr = year + "-" + month + "-" + day;
reminderText.setText(dueDateStr);
}
}
答案 0 :(得分:1)
我使用了Datepicker Dialog Fragment,在对话框中显示日期选择器。
这是我的完整代码
MainActivity.java
package com.example.testmydrag;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.DatePicker;
import android.support.v4.app.FragmentActivity;
import android.app.Dialog;
import android.app.DatePickerDialog;
import android.support.v4.app.DialogFragment;
import java.util.Calendar;
public class MainActivity extends FragmentActivity {
EditText mEdit;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void selectDate(View view) {
DialogFragment newFragment = new SelectDateFragment();
newFragment.show(getSupportFragmentManager(), "DatePicker");
}
public void setTheDate(int year, int month, int day) {
mEdit = (EditText)findViewById(R.id.Text);
mEdit.setText(month+"/"+day+"/"+year);
}
/*
* Date Picker Dialog
*/
public class SelectDateFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar c = Calendar.getInstance();
int yy = c.get(Calendar.YEAR);
int mm = c.get(Calendar.MONTH);
int dd = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dialog = new DatePickerDialog(getActivity(),this,c.YEAR, c.MONTH, c.DATE);
/*Calendar View if you want to Remove Set it to False*/
dialog.getDatePicker().setCalendarViewShown(true);
/*Spinner View if you want to Show Set it to True*/
dialog.getDatePicker().setSpinnersShown(false);
dialog.setTitle("Pick a date");
return dialog;
}
public void onDateSet(DatePicker view, int yy, int mm, int dd) {
setTheDate(yy, mm+1, dd);
}
}
}
main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<EditText android:text="@+string/date_text"
android:id="@+id/Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@+string/pick_date"
android:onClick="selectDate" />
</LinearLayout>
答案 1 :(得分:0)
我找到了一个临时解决方案,但我并不完全满意。您需要最低版本的SDK版本11。
基本上我在DueDatePickerFragment中提到的onAcreateDialog函数中添加了对getDatePicker()的调用,然后更改了它的设置。我不明白为什么我需要这样做,以及为什么它在我的模拟器中显得如此错误。似乎没有其他人遇到这个问题。任何解释都将受到高度赞赏。这是代码:
@Override
public Dialog onCreateDialog(Bundle savedInstanceState){
Log.i("DUEDATEPICKERDIALOG", "Inside onCreateDIALOG");
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePickerDialogObj = new DatePickerDialog(getActivity(), this, year, month, day);
DatePicker dueDatePicker = datePickerDialogObj.getDatePicker();
float scaleVal = (float) 0.6;
dueDatePicker.setScaleX(scaleVal);
dueDatePicker.setScaleY(scaleVal);
dueDatePicker.setCalendarViewShown(false);
return datePickerDialogObj;
}
希望这有助于某人。