Java 如何获取多个膨胀视图的ID?
使用膨胀视图,我得到添加和删除按钮。 我得到了多个视图。而onclick按钮只得到了最后一个视图id。我需要在一个循环中获取所有视图id,但我无法获取多个添加视图的id。如何获取多个膨胀视图的idJava 如何获取多个膨胀视图的ID?,java,android,android-layout,gradle,android-adapter,Java,Android,Android Layout,Gradle,Android Adapter,使用膨胀视图,我得到添加和删除按钮。 我得到了多个视图。而onclick按钮只得到了最后一个视图id。我需要在一个循环中获取所有视图id,但我无法获取多个添加视图的id。如何获取多个膨胀视图的id addBuildingSuper.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Layout
addBuildingSuper.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LayoutInflater inflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View addView = inflater.inflate(R.layout.add_edittext, null);
final FrameLayout frame = addView.findViewById(R.id.frame);
btnCustomSuper = addView.findViewById(R.id.btnCustomSuper);
ImageView remove = addView.findViewById(R.id.remove);
remove.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
((LinearLayout) addView.getParent()).removeView(addView);
}
});
customSupersLayout = new LinearLayout(getApplicationContext());
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
customSupersLayout.setLayoutParams(layoutParams);
framer.addView(customSupersLayout);
customSupersLayout.setVisibility(View.GONE);
customSupersLayout.setClickable(true);
View child = getLayoutInflater().inflate(R.layout.supers_dropdown, null);
customSupersLayout.addView(child);
LinearLayout.LayoutParams layoutParams2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
child.setLayoutParams(layoutParams2);
listSupers = child.findViewById(R.id.listSupers);
LinearLayout supers_dropdown=child.findViewById(R.id.spin_super);
supers_dropdown.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
customSupersLayout.setVisibility(View.GONE);
}
});
btnCustomSuper.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (customSupersLayout.getVisibility()==View.GONE){
btnCustomSuper = (Button) v;
try {
for (int i=0;i<jsonArray1.length();i++){
JSONObject jsonObject = jsonArray1.getJSONObject(i);
Log.i("supersadd",jsonObject.getString("name"));
msupers.add(jsonObject.getString("name"));
sArrayAdapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.text_dropdown, msupers);
synchronized (sArrayAdapter){
sArrayAdapter.notifyDataSetChanged();
//sArrayAdapter.notifyAll();
listSupers.invalidate();
}
listSupers.setAdapter(sArrayAdapter);
}
}catch (Exception e){
e.printStackTrace();
}
customSupersLayout.setVisibility(View.VISIBLE);
}else {
customSupersLayout.setVisibility(View.GONE);
}
}
});
listSupers.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
listSupers = (ListView)parent;
synchronized (sArrayAdapter){
sArrayAdapter.notifyDataSetChanged();
sArrayAdapter.notifyAll();
listSupers.invalidate();
}
btnCustomSuper.setText(msupers.get(position));
listSupers.setTag(position);
JSONObject json = null;
try {
json = jsonArray1.getJSONObject(position);
getSuperId=json.getString("id");
superId.add(getSuperId);
Log.i("msg","btnCustomSupersID"+superId);
jsonObject = jsonArray1.getJSONObject(position);
Log.i("msg","listID"+listID);
} catch (JSONException e) {
e.printStackTrace();
}
Log.i("msg","btnCustomSupers"+btnCustomSuper.getText().toString());
customSupersLayout.setVisibility(View.GONE);
}
});
layout.addView(addView);
}
});
addBuildingSuper.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
LayoutFlater充气器=(LayoutFlater)getBaseContext().getSystemService(Context.LAYOUT\u充气器\u服务);
最终视图addView=充气机。充气(R.layout.add_edittext,null);
最终FrameLayout frame=addView.findviewbyd(R.id.frame);
btnCustomSuper=addView.findviewbyd(R.id.btnCustomSuper);
ImageView-remove=addView.findviewbyd(R.id.remove);
remove.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
((LinearLayout)addView.getParent()).removeView(addView);
}
});
customSupersLayout=新建LinearLayout(getApplicationContext());
LinearLayout.LayoutParams LayoutParams=新的LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_父级,ViewGroup.LayoutParams.MATCH_父级);
customSupersLayout.setLayoutParams(layoutParams);
framer.addView(自定义布局);
customSupersLayout.setVisibility(View.GONE);
customSupersLayout.setClickable(真);
View child=GetLayoutFlater().充气(R.layout.super\u下拉列表,空);
customSupersLayout.addView(子级);
LinearLayout.LayoutParams LayoutParams 2=新的LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_父级,ViewGroup.LayoutParams.MATCH_父级);
setLayoutParams(layoutParams2);
listSupers=child.findViewById(R.id.listSupers);
LinearLayout super\u下拉菜单=child.findViewById(R.id.spin\u super);
supersu下拉列表.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
customSupersLayout.setVisibility(View.GONE);
}
});
btnCustomSuper.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
if(customSupersLayout.getVisibility()==View.GONE){
btnCustomSuper=(按钮)v;
试一试{
对于(int i=0;i在添加rootView
addView.setTag(counter);
rootView.addView(addView);
并使用相同的标记获取视图
View addView = rootView.findViewWithTag(counter)
对于所有视图,计数器都可以是不同的
编辑:
在获取删除图像视图之后:
counter++;
ImageView remove = addView.findViewById(R.id.remove);
remove.setTag("r"+counter);
remove.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String tag = v.getTag();
View viewToRemove = layout.findViewWithTag(tag.replace("r",""));
layout.removeView(viewToRemove);
}
});
在将addView添加到布局之前,请执行以下操作:
addView.setTag(counter);
layout.addView(addView);
请提供一段代码,说明您正在尝试执行的操作。这将有助于让您的问题更清楚。使用下面的代码,我正在膨胀视图LayoutFlater inflater=(LayoutFlater)getBaseContext().getSystemService(Context.LAYOUT\u INFLATE final View addView=inflater.INFLATE(R.LAYOUT.add\u edittext,null);R\u SERVICE);请编辑原始问题来添加代码!我在我的问题中添加了代码片段,我需要一个问题的解决方案我是Android和java的新手。我需要更多的解释来解决我的问题。请我只获取我添加的最后一项的标签,但我需要获取我添加的所有按钮ID。