0
嗨,我有这个Android的代码,日期选择:Android的日期选择器获取日期值
final TextView tv = (TextView) findViewById(R.id.tv);
Button btnDate = (Button) findViewById(R.id.btnDate);
btnDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar cal = Calendar.getInstance();
DatePickerDialog dpd = new DatePickerDialog(MainActivity.this, MainActivity.this, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));
dpd.show();
}
});
}
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Toast.makeText(MainActivity.this, dayOfMonth + "/" + (monthOfYear + 1) + "/" +year , Toast.LENGTH_SHORT).show();
}
}
我想设置与用户挑选的日期值TextView的,我该怎么设置呢?
但是从onDateSet()的TextView不可访问... –
好的解决了它... tnx! TextView tv =(TextView)findViewById(R.id.tv); tv.setText(getString(R.string.day_month_year,dayOfMonth,monthOfYear + 1,year)); –
你不需要在'onDateSet()'中初始化'TextView'。在这样的任何方法之外声明它 - 'private TextView tv;'并且像这样在'onCreate()'中初始化它''tv =(TextView)findViewById(R.id.tv); ' –