Android:以编程方式向布局中添加按钮

Android:以编程方式向布局中添加按钮,android,android-layout,Android,Android Layout,我试图获得一个add按钮,根据按钮左侧的edittext向版面添加另一个按钮。关键是一个人要列出他们家中的房间,然后当他们在每个房间中键入时,会生成一个新按钮,以便他们可以单击该房间,然后开始下一页的工作 我已经完成了一个xml布局,然后我意识到我正在“以编程方式”添加按钮,所以我以编程方式重新编辑布局,然后在开关/案例中(我是这样做的onclicks)为添加按钮,我尝试向视图添加一个按钮,但这变得非常棘手。我想在edittext下面有一个滚动视图并添加按钮,当他们将所有房间添加到他们的房子中时

我试图获得一个add按钮,根据按钮左侧的edittext向版面添加另一个按钮。关键是一个人要列出他们家中的房间,然后当他们在每个房间中键入时,会生成一个新按钮,以便他们可以单击该房间,然后开始下一页的工作

我已经完成了一个xml布局,然后我意识到我正在“以编程方式”添加按钮,所以我以编程方式重新编辑布局,然后在开关/案例中(我是这样做的onclicks)为添加按钮,我尝试向视图添加一个按钮,但这变得非常棘手。我想在edittext下面有一个滚动视图并添加按钮,当他们将所有房间添加到他们的房子中时,最终会为他们的整个家填充一个可滚动的按钮列表。是否有一种方法可以通过编程方式将按钮添加到xml布局中。我想你可以,但我尝试的一切都不起作用

谢谢大家的帮助,如有任何建议,我们将不胜感激

第一次编辑(响应Tanuj的解决方案)

我的XML文件(不确定我们是使用这个还是只使用java):

试试这个:

    //the layout on which you are working
    LinearLayout layout = (LinearLayout) findViewById(R.id.linear_layout_tags);

    //set the properties for button
    Button btnTag = new Button(this);
    btnTag.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    btnTag.setText("Button");
    btnTag.setId(some_random_id);

    //add button to the layout
    layout.addView(btnTag);

我将在您的xml线性布局中添加一个id:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@id/llContainer"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical" >

每个按钮都需要一个onclicklistener来告诉它该做什么。这可以添加到java代码中,在其中声明按钮

Button createdButton = new Button(this);
createdButton.setOnClickListener(new OnClickListener()
{

code you want implemented

}
请尝试以下代码:

LinearLayout l_layout = (LinearLayout) findViewById(R.id.linear_layout); 
l_layout.setOrientation(LinearLayout.VERTICAL);

Button btn1 = new Button(this);
btn1.setText("Button_text");

l_layout.addView(btn1);

btn1.setOnClickListener(new OnClickListener()
{
     @Override
     public void onClick(View v) {
           // put code on click operation
     }
}
这是一种动态创建按钮和外接程序布局的方法

请记住,当您以编程方式创建按钮时,您只需使用这个而不是类名称。这个
公共类AndroidWalkthroughApp1扩展了活动实现了View.OnClickListener{
public class AndroidWalkthroughApp1 extends Activity implements View.OnClickListener {

    final int TOP_ID = 3;
    final int BOTTOM_ID = 4;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // create two layouts to hold buttons
        LinearLayout top = new LinearLayout(this);
        top.setId(TOP_ID);
        LinearLayout bottom = new LinearLayout(this);
        bottom.setId(BOTTOM_ID);

        // create buttons in a loop
        for (int i = 0; i < 2; i++) {
            Button button = new Button(this);
            button.setText("Button " + i);
            // R.id won't be generated for us, so we need to create one
            button.setId(i);

            // add our event handler (less memory than an anonymous inner class)
            button.setOnClickListener(this);

            // add generated button to view
            if (i == 0) {
                top.addView(button);
            }
            else {
                bottom.addView(button);
            }
        }

        // add generated layouts to root layout view
        LinearLayout root = (LinearLayout)this.findViewById(R.id.root_layout);
        root.addView(top);
        root.addView(bottom);
    }

    @Override
    public void onClick(View v) {
        // show a message with the button's ID
        Toast toast = Toast.makeText(AndroidWalkthroughApp1.this, "You clicked button " + v.getId(), Toast.LENGTH_LONG);
        toast.show();

        // get the parent layout and remove the clicked button
        LinearLayout parentLayout = (LinearLayout)v.getParent();
        parentLayout.removeView(v);
    }
}
最终int TOP_ID=3; 最终int BOTTOM_ID=4; /**在首次创建活动时调用*/ @凌驾 创建时的公共void(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main); //创建两个布局以容纳按钮 LinearLayout top=新的LinearLayout(此); top.setId(top_ID); LinearLayout底部=新的LinearLayout(此); bottom.setId(bottom_ID); //在循环中创建按钮 对于(int i=0;i<2;i++){ 按钮按钮=新按钮(此按钮); 按钮.setText(“按钮”+i); //R.id不会为我们生成,所以我们需要创建一个 按钮。设置ID(i); //添加我们的事件处理程序(内存少于匿名内部类) setOnClickListener(此); //将生成的按钮添加到视图 如果(i==0){ top.addView(按钮); } 否则{ 底部。添加视图(按钮); } } //将生成的布局添加到根布局视图 LinearLayout root=(LinearLayout)this.findViewById(R.id.root\u布局); root.addView(顶部); root.addView(底部); } @凌驾 公共void onClick(视图v){ //显示带有按钮ID的消息 Toast Toast=Toast.makeText(AndroidWalkthroughApp1.this,“您单击了按钮”+v.getId(),Toast.LENGTH\u LONG); toast.show(); //获取父布局并删除单击的按钮 LinearLayout parentLayout=(LinearLayout)v.getParent(); parentLayout.removeView(v); } }
Vladimir,感谢您的关注。我尝试了你上面提供的内容,我同意你的想法,但当按下“添加”按钮时,应用程序似乎没有任何作用。还有其他想法吗?我们错过了什么?onClick着火了吗?在那里添加一个日志输出以了解情况。或者在onClick方法内设置断点并进行调试。我会为这个按钮创建一个点击监听器,这样就不需要使用switch语句了。好吧,所以我试着做一个日志,但它像核爆炸一样把这个东西炸了,所以我显然不知道如何做日志。因此,尽管我想到了一种新的方法来做到这一点。我在想,当我单击add时,我将把房间添加到SQLite数据库中的房间表中,而不是创建一个按钮。然后我将查询SQLite数据库并基于数据库中的数据创建按钮。如果有房间,它会给我按钮,如果没有房间,它会说“没有房间”这可能会有帮助
Button createdButton = new Button(this);
createdButton.setOnClickListener(new OnClickListener()
{

code you want implemented

}
LinearLayout l_layout = (LinearLayout) findViewById(R.id.linear_layout); 
l_layout.setOrientation(LinearLayout.VERTICAL);

Button btn1 = new Button(this);
btn1.setText("Button_text");

l_layout.addView(btn1);

btn1.setOnClickListener(new OnClickListener()
{
     @Override
     public void onClick(View v) {
           // put code on click operation
     }
}
public class AndroidWalkthroughApp1 extends Activity implements View.OnClickListener {

    final int TOP_ID = 3;
    final int BOTTOM_ID = 4;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // create two layouts to hold buttons
        LinearLayout top = new LinearLayout(this);
        top.setId(TOP_ID);
        LinearLayout bottom = new LinearLayout(this);
        bottom.setId(BOTTOM_ID);

        // create buttons in a loop
        for (int i = 0; i < 2; i++) {
            Button button = new Button(this);
            button.setText("Button " + i);
            // R.id won't be generated for us, so we need to create one
            button.setId(i);

            // add our event handler (less memory than an anonymous inner class)
            button.setOnClickListener(this);

            // add generated button to view
            if (i == 0) {
                top.addView(button);
            }
            else {
                bottom.addView(button);
            }
        }

        // add generated layouts to root layout view
        LinearLayout root = (LinearLayout)this.findViewById(R.id.root_layout);
        root.addView(top);
        root.addView(bottom);
    }

    @Override
    public void onClick(View v) {
        // show a message with the button's ID
        Toast toast = Toast.makeText(AndroidWalkthroughApp1.this, "You clicked button " + v.getId(), Toast.LENGTH_LONG);
        toast.show();

        // get the parent layout and remove the clicked button
        LinearLayout parentLayout = (LinearLayout)v.getParent();
        parentLayout.removeView(v);
    }
}