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); ' –