2013-04-01 97 views
0

我想編寫一個具有按鈕的應用程序,當我點擊按鈕時,我想在對話框中的Datepicker對話框中顯示日曆。api級別大於11的日期選取器的Android日曆視圖

我試着看構造函數喜歡new DatePickerDialog.OnDateSetListener()日曆。這是不可能的嗎?

api級別11後,它支持日曆視圖。

回答

1

嗨請嘗試以下代碼,希望它會幫助你:

<Button 
      android:id="@+id/birthday" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"> 

,現在叫這個按鈕到您的活動

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_register); 

     editBirthday = (Button)findViewById(R.id.birthday); 


     /* 
     * Change Birth day on click of edit box 
     */ 
     editBirthday.setOnTouchListener(new OnTouchListener() { 

      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       // TODO Auto-generated method stub 

       InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
       imm.hideSoftInputFromWindow(editBirthday.getWindowToken(), 0); 
       showDialog(DATE_DIALOG_ID); 
       return true; 
      } 
     }); 

    } 

    @Override 
    protected Dialog onCreateDialog(int id) { 
     switch (id) { 
     case DATE_DIALOG_ID: 
      return new DatePickerDialog(this, mDateSetListener, year, month, 
        day); 
     } 
     return null; 
    } 

    // updates the date we display in the TextView 
    private void updateDisplay() { 
     /* 
     * Hide virtual keyboard 
     */ 
     editBirthday.setText(new StringBuilder() 
       // Month is 0 based so add 1 
       .append(year).append("-").append(month + 1).append("-") 
       .append(day).append("")); 
    } 

    private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() { 

     public void onDateSet(DatePicker view, int myear, int monthOfYear, 
       int dayOfMonth) { 
      year = myear; 
      month = monthOfYear; 
      day = dayOfMonth; 
      updateDisplay(); 
     } 
    }; 
+0

感謝您的幫助。我用這個http://stackoverflow.com/questions/3712710/android-calendar-view-for-date-picker問題不適合我的需求。它顯示日期選擇器不是'Calender'。我看到的唯一區別是您已設置更改點擊事件觸摸事件。我需要展示一個日曆。謝謝。 – newday

+0

嘗試這 - 如果(android.os.Build.VERSION.SDK_INT> 10) \t \t { \t \t Datepicker.setCalendarViewShown(真); \t \t} –

+0

我在哪裏調用'Datepicker.setCalendarViewShown(true);'方法?在代碼 – newday

1

//此代碼顯示在對話框日曆,希望它幫助你

LayoutInflater inflater =(LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    LinearLayout ll = (LinearLayout)inflater.inflate(R.layout.calendar, null, false); 
    CalendarView cv = (CalendarView) ll.findViewById(R.id.calendarView); 
    cv.setBackgroundColor(Color.BLACK); 


    cv.setOnDateChangeListener(new CalendarView.OnDateChangeListener() { 

    @Override 
    public void onSelectedDayChange(CalendarView view, int year, int month, 
      int dayOfMonth) { 
     // TODO Auto-generated method stub 

     Log.d("date selected", "date selected " + year + " " + month + " " + dayOfMonth); 
     int rows = showMapFragment(year, month, dayOfMonth); 

     if (rows == 0){ 
      Toast.makeText(getApplicationContext(), year + "/" + (month + 1) +"/" + dayOfMonth + " No route to display", Toast.LENGTH_SHORT).show(); 
     } 

     } 
    }); 

    ll.findViewById(R.id.calendarView).setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      Log.d("calendar view", "calendar view clicked"); 
     } 
    }); 

    Dialog calendarDialog = new Dialog(Main.this); 
    calendarDialog.setContentView(ll); 




    calendarDialog.setTitle("Pick a date to view your performance and route"); 
    calendarDialog.show();  

//下面是日曆佈局的xml文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/myLayout" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 
<CalendarView 
android:id="@+id/calendarView" 
android:layout_width="match_parent" 
android:layout_height= "400dp" 
android:showWeekNumber="false" 
android:clickable="true" 
android:weekDayTextAppearance="@style/calendarStyle" 
android:dateTextAppearance="@style/calendarStyle" 
/> 

</LinearLayout>