我有三个视图项目。1. 按钮 2. CalendarView 3. TextView 当我选择日期并单击按钮时,我试图在 TextView 上显示所选日期。日期应仅在单击按钮时出现在 TextView 中。这是我的代码:
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"/>
但是,当我单击按钮时,应用程序会停止。什么可能是错的?