[英]Adding and Removing Dynamic Buttons From one activity to another activity in android on click of button from previous activity button click
I want to develop a similar kind of app(link below) How to create Button Dynamically in android? 我想开发一种类似的应用程序(下面的链接) 如何在android中动态创建Button? But at the same time I want to display this in another activity and not in the same activity. 但是同时我想在另一个活动中而不是在同一活动中显示此内容。 there are 2 edit text: 1) Button Name to be Created. 有2个编辑文本:1)要创建的按钮名称。 2) Destination address (for message to be send on creation of new button). 2)目标地址(用于在创建新按钮时发送的消息)。 whose text is getting passed to another activity for the new button creation. 其文本将传递给另一个活动以创建新按钮。
when I wrote 当我写
public void onClick(View v) {
// TODO Auto-generated method stub
final Context context1=this;
if(v.getId()==R.id.button4){
LinearLayout l1 = (LinearLayout) findViewById(R.id.layout);
// R.id.layout is the layout id of the xml file for the 2nd activity.
Intent intent1 = new Intent(context1,PCode.class);
Button b = new Button(this);
l1.addView(b);
startActivity(intent1);
}
The activity is not moving to the 2nd activity and the program is terminating. 活动未移至第二活动,并且程序正在终止。 I am able to create new button when doing in the same activity. 在同一活动中进行操作时,我可以创建新按钮。 Kindly Help . 请帮忙。
In the first activity's onClick send the data using Intent: 在第一个活动的onClick中,使用Intent发送数据:
intent = new Intent(this, PCode.class);
intent.putExtra("EXTRA_BTN_NAME", editText.getText());
intent.putExtra("EXTRA_WHERE", where);
startActivity(intent);
In the new activity you should get the data with 在新活动中,您应该使用
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activty2);
Intent intent = getIntent();
String btnName = intent.getStringExtra("EXTRA_BTN_NAME");
where= intent.getStringExtra("EXTRA_WHERE");
LinearLayout l1 = (LinearLayout) findViewById(R.id.layout);
Intent intent1 = new Intent(context1,PCode.class);
Button b = new Button(this);
b.SetText(btnName);
//TODO - use the "where" parameter
l1.addView(b);
You can pass total 3 message through intent for every button 1) Button Name to be Created. 您可以通过意图为每个按钮传递总共3条消息1)要创建的按钮名称。 2) Destination address (for message to be send on creation of new button). 2)目标地址(用于在创建新按钮时发送的消息)。 3) Button Action (add/remove) While in new activity you handle button action using our 3rd message of intent ie Button Action (add/remove) what action they want to perform. 3)按钮动作(添加/删除)在新活动中,您将使用我们的第三条意图消息来处理按钮动作,即按钮动作(添加/删除)他们想要执行的动作。 While in new activity you can handle using below code 在新活动中,您可以使用以下代码进行处理
boolean isAddButton = getIntent().getBooleanExtra("ButtonAction", false);
if(isAddButton){
Button myButton = new Button(this);
myButton.setText("Add Me");
LinearLayout ll = (LinearLayout)findViewById(R.id.buttonlayout);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
ll.addView(myButton, lp);
}else{
Button myButton = new Button(this);
myButton.setText("Remove Me");
LinearLayout ll = (LinearLayout)findViewById(R.id.buttonlayout);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
ll.removeView(myButton, lp);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.