如何在Android中删除/删除动态生成的按钮?
所以我生成了一些按钮。它取决于用户的数字(单击按钮时,创建一个新按钮) 我就是这样做的:如何在Android中删除/删除动态生成的按钮?,android,button,dynamically-generated,Android,Button,Dynamically Generated,所以我生成了一些按钮。它取决于用户的数字(单击按钮时,创建一个新按钮) 我就是这样做的: RelativeLayout layout = (RelativeLayout) findViewById(R.id.layoutcprompt); RelativeLayout.LayoutParams OBJ = new RelativeLayout.LayoutParams (140,80); if ((commandsi%6)==0) {adjust=adjust+86; co
RelativeLayout layout = (RelativeLayout) findViewById(R.id.layoutcprompt);
RelativeLayout.LayoutParams OBJ = new RelativeLayout.LayoutParams (140,80);
if ((commandsi%6)==0) {adjust=adjust+86; commandsi=1;}
OBJ.leftMargin =(140*(commandsi-1))+10;
OBJ.topMargin =250+adjust;
Button command = new Button(this);
command.setLayoutParams(OBJ);
command.setId(ID);
command.setText(edittxt.getText().toString());
edittxt.setText("");
command.setBackgroundResource(R.drawable.costum_button);
command.setTextColor(Color.WHITE);
command.setTextSize(14);
layout.addView(command);
command.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Button b = (Button)view;
scommand=b.getText().toString();
}
});
command.setVisibility(View.VISIBLE);
我想删除/删除它们,但我不知道如何。。。。我给了它们一个唯一的id,但我仍然不知道如何删除它们:/使命令成为全局变量。然后您可以稍后访问它,并调用
command.setVisibility(View.GONE)代码>
因此,在类文件的顶部,您将声明全局变量:
Button command;
command = new Button(this);
然后删除重新定义,改为分配给全局变量:
Button command;
command = new Button(this);
然后,如果要隐藏它,请调用:
command.setVisibility(View.GONE);
使命令成为全局变量。然后您可以稍后访问它,并调用command.setVisibility(View.GONE)代码>
因此,在类文件的顶部,您将声明全局变量:
Button command;
command = new Button(this);
然后删除重新定义,改为分配给全局变量:
Button command;
command = new Button(this);
然后,如果要隐藏它,请调用:
command.setVisibility(View.GONE);
试着使用文档,5秒钟的研究可以引导你找到答案
更新
如果此行出现空指针异常,则表示布局
为空,而不是命令
。使您的布局
变量成为该类的全局变量
另外,请确保为每个创建的按钮保留不同的变量。如果您有一个全局变量,并使用同一个变量创建10个按钮,那么您将只引用上一个创建的按钮。如果你解释清楚你想什么时候取下按钮,我们可能会进一步帮助你
例如,如果要在用户单击按钮时删除该按钮,可以更改clickListener:
command.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Button b = (Button)view;
scommand=b.getText().toString();
layout.removeView(view);
}
});
试着使用文档,5秒钟的研究可以引导你找到答案
更新
如果此行出现空指针异常,则表示布局
为空,而不是命令
。使您的布局
变量成为该类的全局变量
另外,请确保为每个创建的按钮保留不同的变量。如果您有一个全局变量,并使用同一个变量创建10个按钮,那么您将只引用上一个创建的按钮。如果你解释清楚你想什么时候取下按钮,我们可能会进一步帮助你
例如,如果要在用户单击按钮时删除该按钮,可以更改clickListener:
command.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Button b = (Button)view;
scommand=b.getText().toString();
layout.removeView(view);
}
});
出于安全目的,您也可以这样做:
ViewGroup layout = (ViewGroup) command.getParent();
if(null!=layout) //for safety only as you are doing onClick
layout.removeView(command);
出于安全目的,您也可以这样做:
ViewGroup layout = (ViewGroup) command.getParent();
if(null!=layout) //for safety only as you are doing onClick
layout.removeView(command);
我无法评论其他帖子,但使用
command = new Button(this)
可能涉及此
上的隐式内存泄漏!(可以是活动
)。而是使用上下文
对象。或者至少取下按钮
然后,因为您有按钮的父项。只需删除它:
ViewGroup layout = (ViewGroup) findViewById(R.id.layoutcprompt);
View command = layout.findViewById(ID);
layout.removeView(command);
我无法评论其他帖子,但使用
command = new Button(this)
可能涉及此
上的隐式内存泄漏!(可以是活动
)。而是使用上下文
对象。或者至少取下按钮
然后,因为您有按钮的父项。只需删除它:
ViewGroup layout = (ViewGroup) findViewById(R.id.layoutcprompt);
View command = layout.findViewById(ID);
layout.removeView(command);
另一个可能帮助查看此线程的其他实现是,可以考虑删除父布局中的所有子元素。一旦获得视图的父元素(我假设它是一个布局容器),就可以删除所有子元素
command.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
ViewGroup vg = (ViewGroup)view.getParent();
vg.removeAllViews();
}
});
另一个可能帮助查看此线程的其他实现是,可以考虑删除父布局中的所有子元素。一旦获得视图的父元素(我假设它是一个布局容器),就可以删除所有子元素
command.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
ViewGroup vg = (ViewGroup)view.getParent();
vg.removeAllViews();
}
});
你也可以用这个狙击手
用于添加按钮
LinearLayout dynamicview = (LinearLayout)findViewById(R.id.buttonlayout);
LinearLayout.LayoutParams lprams = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
Button btn = new Button(this);
btn.setId(count);
final int id_ = btn.getId();
btn.setText("Capture Image" + id_);
btn.setTextColor(Color.WHITE);
btn.setBackgroundColor(Color.rgb(70, 80, 90));
dynamicview.addView(btn, lprams);
btn = ((Button) findViewById(id_));
btn.setOnClickListener(this);
用于卸下按钮
ViewGroup layout = (ViewGroup) findViewById(R.id.buttonlayout);
View command = layout.findViewById(count);
layout.removeView(command);
你也可以用这个狙击手
用于添加按钮
LinearLayout dynamicview = (LinearLayout)findViewById(R.id.buttonlayout);
LinearLayout.LayoutParams lprams = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
Button btn = new Button(this);
btn.setId(count);
final int id_ = btn.getId();
btn.setText("Capture Image" + id_);
btn.setTextColor(Color.WHITE);
btn.setBackgroundColor(Color.rgb(70, 80, 90));
dynamicview.addView(btn, lprams);
btn = ((Button) findViewById(id_));
btn.setOnClickListener(this);
用于卸下按钮
ViewGroup layout = (ViewGroup) findViewById(R.id.buttonlayout);
View command = layout.findViewById(count);
layout.removeView(command);
此方法调用空指针异常。如果用户想要创建20个按钮,而我创建了一个全局变量,而我不调用“new button(this)”,我如何才能达到我的目标?我需要很多按钮,我不知道有多少,所有的按钮都有唯一的文本和唯一的id。我可以创建它们,但我不知道如何删除它们。。。而GONE方法并不好,因为我会在那之后制作新按钮,(数字也取决于用户)试着给它们分配一个动态id。然后按id查找视图,然后删除。您需要将ID存储在列表中。或者只是将全局按钮定义存储在列表中是的,我刚刚做了。我给了他们一个动态ID-s,并存储了最后一个ID-s,所以我每秒钟都知道所有的ID-s。按ID-s查找视图,并在一段时间内删除它们,然后完成。此方法调用空指针异常。如果用户想要创建20个按钮,而我创建了一个全局变量,而我不调用“new button(this)”,我如何才能达到我的目标?我需要很多按钮,我不知道有多少,所有的按钮都有唯一的文本和唯一的id。我可以创建它们,但我不知道如何删除它们。。。而GONE方法并不好,因为我会在那之后制作新按钮,(数字也取决于用户)试着给它们分配一个动态id。然后按id查找视图,然后删除。您需要将ID存储在列表中。或者只是将全局按钮定义存储在列表中是的,我刚刚做了。我给了他们一个动态ID-s,并存储了最后一个ID-s,所以我每秒钟都知道所有的ID-s。按ID-s查找视图并在一段时间内删除它们,然后完成。是的,我知道我尝试过。。。NullPointerException,我创建了一个命令全局变量:“Button command;”,在调用removeView之前我创建了这行:“if(command!=null)layout.removeView(command);”但仍然是NullPointerException…是的,我知道我尝试过了。。。NullPointerException,我创建了一个命令全局变量:“Button command;”,在调用removeView之前我创建了这行:“if(command!=null)layout.removeView(command);”但仍然是NullPointerException…Gre