我想在单击相对布局(DateLayout)时打开日期选择器对话框。 因为我已经这样做了。 每当单击EditText和ImageView时,也打开日期选择器对话框,因为EditText和ImageView都包含在相对布局中,但单击EditText和ImageView然后不打开对话框。请指导我。我的代码中有什么错误?
提前谢谢。
我的代码是,
DateLayout= (RelativeLayout)findViewById(R.id.DateLayout);
DateLayout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
datePickerDialog();
}
});
xml file,
<RelativeLayout
android:id="@+id/DateLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:clickable="true"
android:focusable="true" >
<EditText
android:id="@+id/edtStartDate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:clickable="false"
android:ems="10"
android:enabled="false"
android:focusable="false"
android:hint="strat date"
android:inputType="date"
android:textColor="@color/black" >
</EditText>
<ImageView
android:id="@+id/imgStartDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/edtStartDate"
android:layout_centerVertical="true"
android:clickable="false"
android:layout_marginRight="10dp"
android:contentDescription="@string/content_description"
android:focusable="false"
android:src="@drawable/date" />
</RelativeLayout>
EditText
和ImageView
的clickable
和enabled
应该是真的。
我认为EditText正在消耗触摸。您可以通过创建自定义RelativeLayout
来解决此问题,在该中,您的覆盖onInterceptTouch
并输入一些逻辑来指示何时将触摸事件传递给 EditText 而不是 RelativeLayout。不过,在布局中包含一个按钮来触发DatePicker
可能会更简单,对最终用户来说也更清楚。