2015-09-30 45 views
-6

我想在点击按钮上显示日期选择器。 是否有一个控制使用,或者我需要在按钮的onClick()方法中创建我自己的控件。如果有的话,有什么建议在那里做什么?如何在Android应用程序中创建日期选择器

在此先感谢

+0

你的意思是说日期选择器? –

+0

用户'DatePickerDialog' –

+0

有一个内置的日期和时间选择器对话框在Android和更多的图书馆可用其中之一将是https://github.com/wdullaer/MaterialDateTimePicker 使用任何你选择的.. –

回答

0

您可以试试以下代码。

CalenderActivity.java

public class CalenderActivity extends Activity implements OnDateChangedListener, OnMonthChangedListener { 

private static final DateFormat FORMATTER = SimpleDateFormat.getDateInstance(); 
SimpleDateFormat fmt = new SimpleDateFormat("dd/MM/yyyy"); 


@Bind(R.id.calendarView) 
MaterialCalendarView widget; 
@Bind(R.id.textView) 
TextView textView; 
Button ok; 
int new_date,new_month; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    this.requestWindowFeature(Window.FEATURE_NO_TITLE); 

    setContentView(R.layout.activity_calender); 

    getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); 

    ButterKnife.bind(this); 

    widget.setOnDateChangedListener(this); 
    widget.setOnMonthChangedListener(this); 




    widget.setSelectedDate(Calendar.getInstance()); 

    Calendar c = Calendar.getInstance(); 
    System.out.println("Current time => " + c.getTime()); 

    SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy"); 
    String formattedDate = df.format(c.getTime()); 
    textView.setText(formattedDate); 


    // Toast.makeText(getApplicationContext(),"date is :"+new_date+"/"+new_month+"/"+c.get(Calendar.YEAR),Toast.LENGTH_LONG).show(); 



    ok = (Button) findViewById(R.id.ok_button); 
    ok.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 

      Intent returnIntent = new Intent(); 
      returnIntent.putExtra("result", textView.getText().toString()); 
      setResult(RESULT_OK, returnIntent); 
      CalenderActivity.this.finish(); 


     } 
    }); 
} 

@Override 
public void onDateChanged(@NonNull MaterialCalendarView widget, @Nullable CalendarDay date) { 
    if(date == null) { 
     textView.setText(null); 
    } 
    else { 

     // Toast.makeText(getApplicationContext(), "selected date is : " + date.getDate(), Toast.LENGTH_SHORT).show(); 
     textView.setText(fmt.format(date.getDate())); 
    } 
} 

@Override 
public void onMonthChanged(MaterialCalendarView widget, CalendarDay date) { 

// Toast.makeText(this, FORMATTER.format(date.getDate()), Toast.LENGTH_SHORT).show(); 
} 
} 

activity_calender.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      xmlns:tools="http://schemas.android.com/tools" 
      android:orientation="vertical" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:paddingLeft="@dimen/activity_horizontal_margin" 
      android:paddingRight="@dimen/activity_horizontal_margin" 
      android:paddingTop="@dimen/activity_vertical_margin" 
      android:paddingBottom="@dimen/activity_vertical_margin" 
      tools:context=".BasicActivity" 
> 

<com.prolificinteractive.materialcalendarview.MaterialCalendarView 
    android:id="@+id/calendarView" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    /> 

<TextView 
    android:id="@+id/textView" 
    android:layout_margin="16dp" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:text="" 
    android:textColor="#000000" 
    android:textAppearance="?android:attr/textAppearanceLarge" 
    /> 
<Button 
    android:id="@+id/ok_button" 
    android:layout_width="150dp" 
    android:layout_height="50dp" 
    android:text="OK" 
    android:textColor="@color/white" 
    android:padding="5dp" 
    android:background="@drawable/btn_calender_bg" 
    android:positiveButtonText="Ok" 
    android:layout_gravity="center_horizontal"/> 

在 “点击” 按钮的最后一传的意图。 希望这有助于!

0

尝试在对话框中打开日历的库。

https://github.com/wdullaer/MaterialDateTimePicker

和显示对话框像以下按钮的单击事件。

Button ivLaunchPicker=(Button)findViewById(R.id.button1); 
ivLaunchPicker.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       // DialogFragment to host SublimePicker 
       SublimePickerFragment pickerFrag = new SublimePickerFragment(); 
       pickerFrag.setCallback(mFragmentCallback); 

       // Options 
       Pair<Boolean, SublimeOptions> optionsPair = getOptions(); 

       if (!optionsPair.first) { // If options are not valid 
        Toast.makeText(Sampler.this, "No pickers activated", 
          Toast.LENGTH_SHORT).show(); 
        return; 
       } 

       // Valid options 
       Bundle bundle = new Bundle(); 
       bundle.putParcelable("SUBLIME_OPTIONS", optionsPair.second); 
       pickerFrag.setArguments(bundle); 

       pickerFrag.setStyle(DialogFragment.STYLE_NO_TITLE, 0); 
       pickerFrag.show(getSupportFragmentManager(), "SUBLIME_PICKER"); 
      } 
     }); 
相关问题