安卓開發中,怎麼做到點擊一個區域,然後彈出選擇出生日期,麻煩大神寫的詳細點,包括.xml文件和 java文件。謝謝好心人!
java文件
public class DialogTest extends Activity {
private int mYear, mMonth, mDay;
TextView date;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog);
date = (TextView) findViewById(R.id.date);
date.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showDialog(0);
}
});
}
private final DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
mYear = year;
String mm;
String dd;
mMonth = monthOfYear + 1;
mm = String.valueOf(mMonth);
if (mm.length() < 2)
mm = "0" + mm;
mDay = dayOfMonth;
dd = String.valueOf(mDay);
if (dd.length() < 2)
dd = "0" + dd;
date.setText(String.valueOf(mYear) + "-" + mm + "-" + dd);
}
};
@Override
protected Dialog onCreateDialog(int id) {
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
switch (id) {
case 0:
return new DatePickerDialog(this, mDateSetListener, mYear, mMonth,
mDay);
}
return null;
}
}
xml文件
<?xml version="1.0" encoding="utf-8"?>
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="點我"/>
之前用過的一個,用xml布局datepicker更方便,多看看書,都有介紹