我想在点击按钮时显示日期选择器。 是否有一个控件用于或者我需要在按钮的onClick()方法中创建自己的控件。如果有任何关于该做什么的建议?
提前致谢
答案 0 :(得分: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"/>
最后在按钮的“点击”中传递Intent。 希望这会有所帮助!!
答案 1 :(得分: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");
}
});