Android 如何使所有EditText不可编辑(反之亦然)?
我有一个应用程序,其中有几个文本字段。我希望能够在编辑和不编辑之间切换。如果不对每个人的id进行硬编码,如何做到这一点 例如,我想要一个替代方案Android 如何使所有EditText不可编辑(反之亦然)?,android,Android,我有一个应用程序,其中有几个文本字段。我希望能够在编辑和不编辑之间切换。如果不对每个人的id进行硬编码,如何做到这一点 例如,我想要一个替代方案 EditText Et = findViewById((R.id.one)); Et.setFocusable(false); Et.setClickable(false); Et = findViewById((R.id.two)); Et.setFocusable(false); Et.setClickable(false); //...conti
EditText Et = findViewById((R.id.one));
Et.setFocusable(false);
Et.setClickable(false);
Et = findViewById((R.id.two));
Et.setFocusable(false);
Et.setClickable(false);
//...continue the pattern
Et = findViewById((R.id.ten));
将它们放入数组:
EditText[] etArray = { findViewById(R.id.one), findViewById(R.id.two), findViewById(R.id.three) };
for (EditText et : etArray) {
et.setFocusable(false);
et.setClickable(false);
}
或者如果名称像EditText1,EditText2,。。对于10个视图: for (int i = 1; i <= 10; i++) {
int id = getResources().getIdentifier("EditText" + i, "id", getPackageName());
EditText et = findViewById(id);
et.setFocusable(false);
et.setClickable(false);
}
这是针对onCreate()的:
是的,不需要硬编码就可以修改编辑文本。假设它们包含在名为
main\u content
的LinearLayout
中,则可以通过以下方式循环它们:
LinearLayout layout = findViewById(R.id.main_content);
for (int i = 0; i < layout.getChildCount(); i++) {
View widget = layout.getChildAt(i);
if (widget instanceof EditText) {
widget.setFocusable(false);
widget.setClickable(false);
}
}
LinearLayout布局=findviewbyd(R.id.main\u内容);
对于(int i=0;i
这是最好的吗?在不知道名称的情况下,无法迭代它们?是的,但您必须将它们的名称更改为EditText1、EditText2等,。。。我将编辑我的答案在下面的答案之后有任何反馈吗?是的,你的答案因为长时间的延迟而不起作用。if
station从来都不是真的,我认为这是因为EditText
在TableRow
中,所以使用TableRow布局=findViewById(R.id.tableRowID)
TableRow是一个线性布局,因此您可以在子视图中循环。不幸的是,EditText
在多个TableRow
中,我仍然认为在执行TableRow布局=findViewById(R.id.tableRow1)时,考虑到包含所有TableRow的父视图,可以进行循环代码>然后它只适用于第一行的编辑文本。
etArrayList = new ArrayList<EditText>();
for (int i = 1; i <= 10; i++) {
int id = getResources().getIdentifier("EditText" + i, "id", getPackageName());
EditText et = findViewById(id);
etArrayList.add(et);
}
for (EditText et : etArrayList) {
et.setFocusable(false);
et.setClickable(false);
}
LinearLayout layout = findViewById(R.id.main_content);
for (int i = 0; i < layout.getChildCount(); i++) {
View widget = layout.getChildAt(i);
if (widget instanceof EditText) {
widget.setFocusable(false);
widget.setClickable(false);
}
}