最近在学习Android开发,在编写程序的过程中,发现EditText setOnClickListener事件响应中,只有获取焦点的时候才会响应,
如当焦点在别的控件上时,只能先点击获取焦点,第二次点击才会响应,解决办法改用setOnTouchListener监听,
android.view.View下的OnTouchListener接口是只要触摸(touch)状态改变,就会触发该事件。先记录下来,分享一下
代码如下:
woman_SDloadText.setOnTouchListener(new View.OnTouchListener() { //按住和松开的标识 int touch_flag=0; @Override public boolean onTouch(View v, MotionEvent event) { touch_flag++; if(touch_flag==2){ //自己业务 } return false; } });
上面错误的:
edit.setOnTouchListener(new EditText.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub if (event.getAction() == MotionEvent.ACTION_DOWN) { DatePickerDialog dialog = new DatePickerDialog( getContext(), new OnMyDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { int month = monthOfYear + 1; edit.setText(year + "/" + month + "/" + dayOfMonth); // 传递数值 // date = year + "/" + month + "/" + // dayOfMonth; } }, c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH)); dialog.show(); } return false; } });