在Android中的日历视图中显示所选日期
我有三个视图项。1.按钮2。日历视图3。文本框 当我选择日期并单击按钮时,我试图在TextView上显示所选日期。日期只应在单击按钮时显示在文本视图中。这是我的密码:在Android中的日历视图中显示所选日期,android,mobile,calendarview,Android,Mobile,Calendarview,我有三个视图项。1.按钮2。日历视图3。文本框 当我选择日期并单击按钮时,我试图在TextView上显示所选日期。日期只应在单击按钮时显示在文本视图中。这是我的密码: CalendarView cal = null; TextView text = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentV
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"/>
但是,当我点击按钮时,应用程序停止。有什么可能是错误的?您发布的方法的签名是错误的。应该是
public void Touchy(View view) {}
张贴stacktrace。同样,Touchy的正确签名是public void Touchy(视图)。在类I use CaldendarView cal顶部初始化值时,不能将其签名更改为Package;然后在onCreate中做你所做的。@blackbelt谢谢..公开声明解决了这个问题