对于我来说,没有任何一个答案能够产生完整的解决scheme。
指导(noob友好) ,请注意在代码中的几个意见。
为名为EditTextDispatched.java的自定义EditText创build一个Java类
import android.content.Context; import android.util.AttributeSet; import android.view.MotionEvent; import android.widget.RelativeLayout; /** * @author Martin Pfeffer * @see https://celox.io */ public class EditTextDispatched extends android.support.v7.widget.AppCompatEditText { private static final String TAG = "DispatchingEditText"; private boolean editable = true; public EditTextDispatched(Context context) { super(context); } public EditTextDispatched(Context context, AttributeSet attrs) { super(context, attrs); } public EditTextDispatched(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } // get the current state public boolean isEditable() { return editable; } // set your desired behaviour public void setEditable(boolean editable) { this.editable = editable; } @Override public boolean dispatchTouchEvent(MotionEvent motionEvent) { if (editable) { // default behaviour of an EditText super.dispatchTouchEvent(motionEvent); return true; } // achieve the click-behaviour of a TextView (it's cuztom) ((RelativeLayout) getParent()).onTouchEvent(motionEvent); return true; } }
在你的xml中引用EditTextDispatched (你将不得不改变pgk-name):
调用:
private void changeEditTextBehaviour(EditTextDispatched value, boolean editable) { value.setEditable(editable); value.setEnabled(editable); }