2015-09-30 69 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"); 
      } 
     }); 
相關問題