如何在Android中动态创建按钮? [英] How to create Button Dynamically in android?

查看:313
本文介绍了如何在Android中动态创建按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要创建这样的页面。这7个按钮都已经存在,但如果用户想添加更多类别(按钮),那么他就可以用做 + 按钮和删除使用 - 按钮。 任何想法或教程制作呢?

I want to create a page like this. these 7 buttons are already exist but if user want to add more categories (button) then he can do using + button and delete using - button. Any idea or tutorial for making this?

推荐答案

创建/删除按钮的onClick +按钮 - 按钮如下:

Create/Remove button onClick of + button and - button as below:

  public void onClick(View v) {

     switch(v.getId()){
     case (R.id.plusbutton):
                 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);
                 break;.
     case (R.id.minusbutton):
                 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);
                 break;
           }
         }

这篇关于如何在Android中动态创建按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆