我正在开发一个带有一些复杂表单的 Android 平板电脑应用程序(布局有一个 ListView 和包含表单的布局,所以我必须膨胀 EditText)。我的想法是,如果我单击显示日期的 TextView,DataPicker 将显示在对话框中。
问题是未检测到点击。没有日志。
爪哇:
public static EditText datep;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.admissions_all);
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
View inf = LayoutInflater.from(getApplicationContext()).inflate(R.layout.admissions, null);
datep = (EditText) inf.findViewById(R.id.date_value);
datep.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.i("Date", "You clicked me mate!");
showDatePickerDialog(v);
}
});
}
public void showDatePickerDialog(View v) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getFragmentManager(), "datePicker");
Log.i("Date", "I want to show datePicker");
}
public static class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day= c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
datep.setText(day + "." + month+ "." + year);
}
}
XML:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/general"
android:layout_alignRight="@id/general"
android:layout_below="@+id/general"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp" >
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Data"
android:textAppearance="?android:attr/textAppearanceLarge" />
<View
android:id="@+id/line"
android:layout_width="wrap_content"
android:layout_height="1dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/title"
android:background="#333333" />
<EditText
android:id="@+id/date_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/line"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_toRightOf="@+id/date_desc"
android:ems="9"
android:inputType="date" >
</EditText>
<TextView
android:id="@+id/date_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/date_value"
android:layout_alignBottom="@+id/date_value"
android:layout_alignParentLeft="true"
android:text="Date:"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
我跟着这个:http: //developer.android.com/guide/topics/ui/controls/pickers.html