我的android活动中有这个editText
<EditText
android:id="@+id/payment_expiration"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/payment_expiration_label"
android:layout_centerHorizontal="true"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:clickable="true"
android:onClick="update_expiration_date"
android:editable="false"
android:layout_marginTop="-4dp"
android:cursorVisible="false"
android:maxLength="7"
android:padding="10dp"
android:textSize="13dp" />
正如您在用户点击时所看到的那样
我调用这个方法来启动datePickerDialog:
public void update_expiration_date(View v){
Log.i("","cliqué");
picker.show();
can_update_expiration_date = true;
}
我遇到的问题是:在我第一次打开此活动时,用户必须单击两次才能启动对话框
但在那之后,只需点击一下即可
我该如何解决这个问题
您可以尝试可聚焦的解决方案,或者只是添加状态跟踪变量......
public class YourActivity extends Activity {
private boolean clicked;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
clicked = false;
}
public void update_expiration_date(View v){
if(clicked == false){
clicked = true;
return;
} else {
Log.i("","cliqué");
picker.show();
can_update_expiration_date = true;
}
}
}
begiPass解决方案有效,但有一个更好的。
如果焦点为false,则用户永远无法看到选择了哪个edittext。
更好的解决方案是使用onTouchListener。我的示例代码:
edit.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
edit.setText("");
v.setOnTouchListener(null);
return false;
}
});
别忘了将OnTouchListener设置为null。我们只想清除提示(只有一次)。
问候!
使用OnTouchListener处理它:
when (view) {
edtName -> {
if (motionEvent.action == MotionEvent.ACTION_UP) {
edtName.requestFocus()
//TODO
}
}
edtPassword -> {
if (motionEvent.action == MotionEvent.ACTION_UP) {
edtPassword.requestFocus()
//TODO
}
}
}
return false
}```