2017-05-05 72 views
0

enter image description here的DatePicker可见按钮

正如你所知道的,我使用的日期选择器(不同的语言..在顶部被写入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

点击'之上year'标题改变全年,单击其他时间,你可以挑选的十年。它适用于我用作应用程序用户的许多数据助手,所以它也可以在这里工作。 – miljon

+0

@miljon你是对的,我看不到那个,因为那对我也是无形的。 –

回答

1
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> 

original answer

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

    }; 
+0

myCalendar没有定义,也getActivity() –