gpt4 book ai didi

java - onClickListener 如何跟踪要删除的 View

转载 作者:行者123 更新时间:2023-11-30 01:44:18 25 4
gpt4 key购买 nike

我正在学习教程 here - 现在我没有添加 textView,而是使用以下方法作为替代:

public void addView(LinearLayout container, String[] spin_array, String hint, int inputType, int tagger) {

LayoutInflater layoutInflater = (LayoutInflater) getActivity().getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

final View addView = layoutInflater.inflate(R.layout.add_company_fragment_two_dynamic_content, null);

final Spinner spin_dynamic = (Spinner) addView.findViewById(R.id.email_spinner1);
EditText edt_dynamic = (EditText) addView.findViewById(R.id.edittext_email1);
ImageView remove_dynamic = (ImageView) addView.findViewById(R.id.btn_remove);


edt_dynamic.setHint(hint);
setUpSpinners(spin_array, spin_dynamic);
edt_dynamic.setTag(container.getTag() + "edt" + tagger);
edt_dynamic.setInputType(inputType);
edt_dynamic.setTypeface(roboto_condenced_light);
spin_dynamic.setTag(container.getTag() + "spin" + tagger);

idsMap.put(spin_dynamic.getTag().toString(), edt_dynamic.getTag().toString());

// get height from dimens
int height = (int) getResources().getDimension(R.dimen.lin_height);
// set this height
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, height);

// we are only concerned about top margin here.
layoutParams.setMargins(0, (int) getResources().getDimension(R.dimen.topMargin), 0, 0);
container.addView(addView, 1, layoutParams);

remove_dynamic.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Remove View (LinearLayout) and also remove the key from idsMap so that we dont get a NullPointer later
((LinearLayout) addView.getParent()).removeView(addView);
idsMap.remove(spin_dynamic.getTag().toString());
}
});
}

这里的问题是我可以多次调用上述函数,以在 LinearLayout 中插入一个 View 行。

当我们尝试从容器中删除 View 时,我无法理解如何跟踪 addView 变量:

remove_dynamic.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Remove View (LinearLayout) and also remove the key from idsMap so that we dont get a NullPointer later
((LinearLayout) addView.getParent()).removeView(addView);
idsMap.remove(spin_dynamic.getTag().toString());
}
});

onClickListener 如何知道要在此处删除的 addView 实例,如果我插入四个 View ,onClick 如何准确知道要删除哪一个?

没有错误,一切正常。但它为什么有效?

以下是表示此行为的图像:

enter image description here

最佳答案

你必须在删除 View 时使用相同的 View 对象,例如你在 ViewGroup 中添加了三个 View

addView1
addView2
addView3

所以当你想删除 fistview 即 addView1 时,你必须使用该对象 ((LinearLayout) addView.getParent()).removeView(addView1);

关于java - onClickListener 如何跟踪要删除的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33954695/

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