我搜索并找到了使单个EditText不可编辑的方法,例如
edittext.setKeyListener(null);
- 将
EditText
设置为android:focusable="false"
但是,这些方法的问题是我需要获取对布局中每个 EditText
View 的引用,并单独实现上述语句。有没有办法使布局中的所有 EditText View 不可编辑?
应用程序是这样的:我有一个“显示和编辑” Activity ,其中向用户显示数据库中的当前记录。此时显示的文本应该是只读的。如果需要,他可以单击编辑按钮,然后 EditText View 将变为可编辑状态。
我尝试在根 ViewGroup
中设置 android:inputType="none"
和 android:focusable="false"
但没有'不工作。有出路吗?
最佳答案
如果您想以编程方式实现此目的,
public void setupUI(View view, boolean editable) {
if (view instanceof EditText) {
((EditText)view).setFocusable(editable);
//Here you can add any other code that needed to be done while changing focus of a particular edit text
return;
}
//If a layout container, iterate over children and seed recursion.
if (view instanceof ViewGroup) {
for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
View innerView = ((ViewGroup) view).getChildAt(i);
setupUI(innerView, editable);
}
}
}
为此方法提供 fragment 或 Activity 的父 View 。
当您单击编辑按钮时,将父 View 和 true(即可编辑)传递给方法,编辑后传递父 View 和 false(即不可编辑)。
关于android - 使所有 EditText 不可一起编辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43630146/