gpt4 book ai didi

java - 按钮文本需要根据 EditText 输入的数据进行更改

转载 作者:行者123 更新时间:2023-12-02 00:49:54 25 4
gpt4 key购买 nike

我想将 4 个按钮的文本更改为按下第五个按钮时在 4 个 EditText 字段中输入的文本。

我已经为字符串和按钮创建了 ArrayList。当我从 EditText 手动将组件添加到 String ArrayList,然后将其添加到 Button ArrayList 的 setText() 时,它工作得很好。

当我为 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 (int j=0;j<typeText.size();j++){
answers.add(j,typeText.get(j).getText().toString());
}

而不是

for(j=0;j<3;j++)      /*creates problem when this loop is run */
{
answers.add(j,typeText.get(j).getText().toString());
}

关于java - 按钮文本需要根据 EditText 输入的数据进行更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57868529/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com