简体   繁体   English

单击上一个活动按钮中的按钮后,将动态按钮从android中的一个活动添加到Android中的另一个活动中

[英]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.

相关问题 在android中,如何恢复上一个活动,在第二个活动上单击按钮 - In android, How to resume previous activity , on button click from second activity 从另一个活动中单击按钮后,从活动中删除列表视图项 - Delete a listview item from activity on button click from another activity 从另一个活动的按钮单击调用活动类失败 - Calling an activity class from on a button click, from another activity, fails 单击按钮将项目添加到与当前活动不同的活动中的列表视图 - Adding an item to a listview in a different activity from the current activity on click of a button 如何在android中从单击按钮的第一个活动和从第二个按钮单击的第三个活动中进行第二个活动 - How to go second activity from first activity on button click and Third activity from Second on button click in android 如何将按钮单击时的 int 值从一个活动传递到另一个活动? - How to pass int value on button click from one activity to another? 通过单击按钮从一种活动转到另一种活动需要时间 - taking time when going from one activity to another by button click 从 Android 打开新活动按钮单击 - Open New Activity from Android Button Click 单击按钮可从一个活动转到活动列表中的随机活动 - Button click to go from one activity to random in a list of activity 当我单击后退按钮从当前活动 android 转到上一个活动时 - When I click back button goto previous activity from current activity android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM