正如你所知,我正在使用日期选择器(不同语言......最重要的是2015年11月写的)。
我正在使用OnClickListener,当点击a1
EditText时,会弹出DatePicker对话框,您可以选择日期,完成后会在EditText中收到日期。
但是我在对话框的构造按钮和选择年份方面遇到了麻烦,它们是不可见的,我只能点击它们,虽然没有看到它们。
如何启用/显示确认按钮和年份日期选择?
Activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_first_entry"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.none.myapplication.firstEntry">
<EditText
android:id="@+id/a1"
android:layout_below="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
Activity.java
private EditText a1;
private DatePickerDialog fromDatePickerDialog;
private SimpleDateFormat dateFormatter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first_entry);
dateFormatter = new SimpleDateFormat("dd-MM-yyyy");
findViewsById();
setDateTimeField();
}
private void findViewsById() {
a1 = (EditText) findViewById(R.id.a1);
a1.setInputType(InputType.TYPE_NULL);
a1.requestFocus();
}
private void setDateTimeField() {
a1.setOnClickListener(this);
Calendar newCalendar = Calendar.getInstance();
fromDatePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Calendar newDate = Calendar.getInstance();
newDate.set(year, monthOfYear, dayOfMonth);
a1.setText(dateFormatter.format(newDate.getTime()));
}
},newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));
}
@Override
public void onClick(View view) {
if(view == a1) {
fromDatePickerDialog.show();
}
}
答案 0 :(得分:4)
fromDatePickerDialog = new DatePickerDialog(this, R.style.DialogTheme, new DatePickerDialog.OnDateSetListener() {
//rest of the code
}
并在你的styles.xml中
<style name="DialogTheme" parent="Theme.AppCompat.Light.Dialog">
<item name="colorAccent">@color/blue_500</item>
</style>
答案 1 :(得分:0)
检查此代码段 -
public void showDatePickerDialog(){
DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(), date, myCalendar.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH), myCalendar.get(Calendar.DAY_OF_MONTH));
datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
datePickerDialog.show();
//clientDate.setText(""+myCalendar.get(Calendar.DAY_OF_MONTH)+"/"+(myCalendar.get(Calendar.MONTH)+1)+"/"+myCalendar.get(Calendar.YEAR));
}
DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
//updateYourLabel(); // or whatever you want
//Toast.makeText(context, ""+dayOfMonth+"/"+(monthOfYear+1)+"/"+year, Toast.LENGTH_SHORT).show();
clientDate.setText(""+dayOfMonth+"/"+(monthOfYear+1)+"/"+year);
}
};
答案 2 :(得分:0)
对于那些没有得到上述解决方案有用的人,试试这个解决方案-
styles.xml:
<!-- Date Picker Styles -->
<style name="DialogTheme" parent="Theme.AppCompat.Light.Dialog">
<item name="colorAccent">@color/titletext_blue</item> <!-- Header Background -->
<item name="android:colorAccent">@color/titletext_blue</item>
<item name="android:textColor">@color/titletext_blue</item>
<item name="android:buttonBarPositiveButtonStyle">@style/DialogButtonStyled</item>
<item name="android:buttonBarNegativeButtonStyle">@style/DialogButtonStyled</item>
<item name="android:buttonBarNeutralButtonStyle">@style/DialogButtonStyled</item>
<item name="android:colorControlActivated">#4971AC</item> <!--selected day-->
<!-- <item name="android:textColorPrimary">#89B5F6</item> <!–days of the month–>-->
<!-- <item name="android:textColorSecondary">#0E3E85</item> <!–days of the week–>-->
</style>
<!-- Date picker ok/cancel button styles -->
<style name="DialogButtonStyled" parent="Theme.MaterialComponents.Light">
<item name="android:textColor">@color/titletext_blue</item>
<item name="android:background">@color/transparent</item>
<item name="android:textSize">15dp</item>
</style>
并调用日期选择器如下:
new DatePickerDialog(getActivity(), R.style.DialogTheme,(DatePickerDialog.OnDateSetListener) getActivity(), year, month, day);
这应该对你有帮助:) 干杯!