【问题标题】:generate dynamic id for multiple edittext with button click通过单击按钮为多个edittext生成动态ID
【发布时间】:2013-08-14 23:34:45
【问题描述】:

我有一排是EditText。我的场景是当用户单击一个按钮时,将添加另一行。我以某种方式实现了这一点,但EditText 都具有相同的 ID。那么如何分配EditText的id是动态创建的。我的 EditText 在布局 XML 文件中。是否可以使用 XML 或者我必须以编程方式创建 EditText。 提前致谢。

    private void inflateEditRow(String name) {

    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final View rowView = inflater.inflate(R.layout.row, null);
    final ImageButton deleteButton = (ImageButton) rowView
            .findViewById(R.id.buttonDelete);
    final EditText editText = (EditText) rowView
            .findViewById(R.id.req);

    if (name != null && !name.isEmpty()) {
        editText.setText(name);
    } else {
        mExclusiveEmptyView = rowView;
        deleteButton.setVisibility(View.VISIBLE);
    }

    // A TextWatcher to control the visibility of the "Add new" button and
    // handle the exclusive empty view.
    editText.addTextChangedListener(new TextWatcher() {

        @Override
        public void afterTextChanged(Editable s) {

            if (s.toString().isEmpty()) {
                mAddButton.setVisibility(View.VISIBLE);
                deleteButton.setVisibility(View.VISIBLE);

                if (mExclusiveEmptyView != null
                        && mExclusiveEmptyView != rowView) {
                    mContainerView.removeView(mExclusiveEmptyView);
                }
                mExclusiveEmptyView = rowView;
            } else {

                if (mExclusiveEmptyView == rowView) {
                    mExclusiveEmptyView = null;
                }

                mAddButton.setVisibility(View.VISIBLE);
                deleteButton.setVisibility(View.VISIBLE);
            }
        }


    public void onAddNewClicked(View v) {
    // Inflate a new row and hide the button self.
    inflateEditRow(null);
    v.setVisibility(View.VISIBLE);
}

【问题讨论】:

  • 可以设置标签为edittext并使用get tag
  • 你能给我一些想法如何在这种情况下使用标签..
  • 使用 button.setId(count); count 是静态变量或 Raghunandan 也是正确的,您可以使用 setTag 或 getTag 也可以使用
  • 考虑 API 17 中的 generateViewId()
  • @ArunCThomas 如果我想在 17 岁以下使用它怎么办。我希望我的应用程序也可以在较低版本上运行。考虑一下

标签: android android-edittext


【解决方案1】:

为了动态生成 View Id,请使用表单 API 17

generateViewId()

这将生成一个适合在setId(int) 中使用的值。此值不会与 aapt 在构建时为R.id. 生成的 ID 值冲突

这样

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                EditText editText = new EditText(MainActivity.this);
                editText.setId(editText.generateViewId());
                editText.setHeight(50);
                editText.setWidth(50);
                ll.addView(editText);

            }

【讨论】:

  • 如何获取每个edittext的id?请详细说明。
【解决方案2】:

您可以在资源文件夹中列出可能的ids,例如ids.xml,并在其中放置ids,如下所示;

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

然后在您的 Java 代码中将动态 ID 设置为您的 EditTexts,如下所示;

youreditText1.setId(R.id.edittext1);
youreditText2.setId(R.id.edittext2);
youreditText3.setId(R.id.edittext3);

【讨论】:

  • 我在单行中有 14 个编辑文本。我必须动态添加至少 20 行...你认为这在我的场景中是可能的吗?
  • 你需要他们的身份证吗?将它们全部放在一个 EditText 数组中并通过索引访问它怎么样?
猜你喜欢
  • 1970-01-01
  • 2015-07-02
  • 1970-01-01
  • 2019-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多