【发布时间】: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 岁以下使用它怎么办。我希望我的应用程序也可以在较低版本上运行。考虑一下