对我来说,没有一个答案会产生一个完全可行的解决方案。
我的修复展示https://www.youtube.com/watch?v=oUA4Cuxfdqc
指南(noob-friendly),请注意代码中的一些注释。
EditText
为名为EditTextDispatched.java的自定义创建 Java 类
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.RelativeLayout;
/**
* @author Martin Pfeffer
* @see <a href="https://celox.io">https://celox.io</a>
*/
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 名称):
<io.celox.brutus.EditTextDispatched
android:id="@+id/row_field_value"
style="@style/row_field_text_display"
android:layout_alignParentBottom="true"
android:text="@string/sample_field_value" />
调用:
private void changeEditTextBehaviour(EditTextDispatched value, boolean editable) {
value.setEditable(editable);
value.setEnabled(editable);
}