2014-01-06 125 views
2

我有三個視圖項目。 1.按鈕2. CalendarView 3. TextView 我想在TextView上顯示選定的日期,當我選擇日期並單擊按鈕。日期應該只在點擊按鈕時出現在TextView中。這裏是我的代碼:在Android的CalendarView中顯示選定的日期

CalendarView cal = null; 
TextView text = null; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    cal = (CalendarView) findViewById(R.id.calendarView); 
    Button but = (Button) findViewById(R.id.button); 
} 

void Touchy(View view){ 
    text = (TextView) findViewById(R.id.textView); 
    cal.setOnDateChangeListener(new CalendarView.OnDateChangeListener() { 
     @Override 
     public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) { 
      text.setText(""); 
      int d = dayOfMonth; 
      int m = month; 
      int y = year; 
      String c = d+"-"+m+"-"+y; 
      text.append(c); 
     } 
    }); 
} 

XML

<Button 
    style="?android:attr/buttonStyleSmall" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="+" 
    android:id="@+id/button" 
    android:layout_gravity="right|top" 
    android:onClick="Touchy"/> 

但是應用程序時,我點擊按鈕停止。什麼可能是錯的?

+1

發佈stacktrace。所有相同的Touchy的正確簽名是public void Touchy(View view)。您無法將其簽名更改爲包 – Blackbelt

+0

在類頂部啓動值時,我使用CaldendarView cal;然後在onCreate中做你所做的事情。 –

+0

@blackbelt謝謝..公告聲明解決了這個問題 – stud91

回答

0

您發佈的方法的簽名是錯誤的。它應該是

public void Touchy(View view) {} 
相關問題