gpt4 book ai didi

java - 无法以编程方式设置 EditText ID

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

我正在创建动态数量的 editText,并希望最终提取每个 editText 的 ID 以在 editText 上调用 .getText()。

但是,我注意到很难以编程方式设置 ID,因此我改用 .setTag() 方法:

private void createAnswerChoice(int answerNumber) {
ViewGroup layout = (ViewGroup) mRootView.findViewById(R.id.create_poll_questions_answer_layout);
EditText editText = new EditText(getActivity());
editText.setHint(getResources().getString(R.string.answer_text) + " " + answerNumber);
editText.setSingleLine(true);
editText.setImeOptions(EditorInfo.IME_ACTION_DONE);
String editTextID = ((getResources().getString(R.string.created_answer_editText_id))+String.valueOf(answerNumber));
editText.setTag(editTextID);
Toast.makeText(getActivity().getApplicationContext(), editTextID, Toast.LENGTH_SHORT).show();
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
editText.setLayoutParams(layoutParams);
TextInputLayout newAnswer = new TextInputLayout(getActivity());
newAnswer.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
newAnswer.addView(editText, layoutParams);
layout.addView(newAnswer);
}

如果我知道标签而不是 ID,如何获取每个 editText 的值?另外,.setTag() 方法的用途是什么(它与 .setID() 有什么关系?)

最佳答案

在res/values/id.xml中创建资源文件(id.xml)

  <?xml version="1.0" encoding="utf-8"?>
<resources>
<item
type="id"
name="edittext_hello" />
</resources>

然后设置,

 editText.setId(R.id.edittext_hello);

关于java - 无法以编程方式设置 EditText ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38257605/

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