【发布时间】:2020-01-12 01:51:46
【问题描述】:
我想在按下第五个按钮时将 4 个按钮的文本更改为在 4 个 EditText 字段中输入的文本。
我为字符串和按钮创建了 ArrayList。当我从 EditText 手动将组件添加到 String ArrayList,然后将其添加到 setText() 到 Button ArrayList 时,它工作得很好。
当我为 EditText 字段引入另一个 ArrayList 并尝试将其组件循环添加到 String ArrayList 时,应用程序崩溃。
public class MainActivity extends AppCompatActivity {
Button A,C,D,B;
ArrayList<Button> options = new ArrayList<Button>();
EditText P,Q,L,S;
ArrayList<String> answers = new ArrayList<String>();
ArrayList<EditText> typeText = new ArrayList<EditText>();
public void changeButtonText(View view)
{
/* answers.add(P.getText().toString()); /* works fine when
answers.add(Q.getText().toString()); this code is
answers.add(L.getText().toString()); run*/
answers.add(S.getText().toString());*/
int j=0;
for(j=0;j<3;j++) /*creates problem when this loop is run */
{
answers.add(j,typeText.get(j).getText().toString());
}
j=0;
for(Button i:options)
{
i.setText((answers.get(j)));
j++;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
P=(EditText) findViewById(R.id.editText1);
Other EditText fields are declared the same way
A=(Button) findViewById(R.id.button1);
Other Buttons declare the same wat
options.add(A);
options.add(B); //Add buttons to Button ArrayList
options.add(C);
options.add(D);
typeText.add(P);
typeText.add(Q); //Add EditTexts to the ArrayList
typeText.add(L);
typeText.add(S);
}
}
EditText ArrayList 时按钮的文本发生变化
【问题讨论】:
-
请添加崩溃日志
-
使用
for (j=0;j<typeText.size();j++) -
你能发布正确的错误代码吗? answers.add(S.getText().toString());*/ ------ 这是不可编译的
标签: java android button arraylist android-edittext