假设我有一个文本输出,例如“这是一个 EditText”(textToUse)。
我想将同样的文本输出显示到多个 EditText 字段中,而不是 1 个。
如果我有 3 个 EditText id:edit1、edit2 和 edit3
而不是仅仅使用:
private EditText mEditText;
private String textToUse;
mEditText = (EditText)findViewById(R.id.edit1);
mEditText.setText(textToUse);
它只会显示输出到 EditText“edit1”的文本,如何更改代码以使其同时显示在“edit2”和“edit3”上?
我试过:
mEditText = (EditText)findViewById(R.id.edit1);
mEditText = (EditText)findViewById(R.id.edit2);
mEditText = (EditText)findViewById(R.id.edit3);
和
mEditText = (EditText)findViewById(R.id.edit1, R.id.edit2, R.id.edit3);
和
mEditText = (EditText)findViewById(R.id.edit1, edit2, edit3);
但它们都不起作用。 如果这是一个愚蠢的问题,我深表歉意,我刚刚开始 Android 开发。谢谢~
最佳答案
您需要在代码中声明三个不同的 EditText,非数组对象只能保存一 (1) 个值。所以:
mEditText = (EditText)findViewById(R.id.edit1);
mEditText = (EditText)findViewById(R.id.edit2);
mEditText = (EditText)findViewById(R.id.edit3);
将简单地覆盖它的值。 (在上面的代码中,mEditText 只保存了 R.id.edit3)
尝试
mEditText1 = (EditText)findViewById(R.id.edit1);
mEditText2 = (EditText)findViewById(R.id.edit2);
mEditText3 = (EditText)findViewById(R.id.edit3);
mEditText1.setText(textToUse);
mEditText2.setText(textToUse);
mEditText3.setText(textToUse);
或者像@Huy Nguyen 在他的回答中所做的那样,为您的 EditText 使用一个数组。
关于Android:将相同的文本输出显示到多个 EditText,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34890534/