简体   繁体   English

添加第一个按钮后,无法在Android中动态添加按钮

[英]Dynamically adding Buttons in Android doesn't work after the first Button is added

I have a button that I have created in code, which has a listener for Click events. 我有一个用代码创建的按钮,该按钮具有Click事件的侦听器。 Every time that the button is clicked, it should generate another button and add it below the original button. 每次单击该按钮时,它都应生成另一个按钮并将其添加到原始按钮下方。 However, no matter how many times I click the first button, it will only add a dynamic button once, and not add any more. 但是,无论我单击第一个按钮多少次,它只会添加一次动态按钮,而不再添加任何按钮。

Here is my coding: 这是我的编码:

public class DynaminControlActivity extends Activity {
    private RelativeLayout container;
    private int mainIdCnt = 0;
    private int mainId = 100;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        createMainButton();
    }

    public void createMainButton() {
        container = (RelativeLayout) findViewById(R.id.workLayout);
        Button b = new Button(this);
        b.setId(mainIdCnt + mainId);
        CharSequence text = "Main +";
        b.setText(text);
        container.addView(b);
        if (mainId > 0) {
            mainId++;
        }
        b.setOnClickListener((new View.OnClickListener() {
            public void onClick(View v) {
                createDynamicButton();
            }
        }));
    }

    public void createDynamicButton() {
        container = (RelativeLayout) findViewById(R.id.workLayout);
        Button b = new Button(this);
        CharSequence text = "Main +";
        b.setText(text);
        RelativeLayout.LayoutParams relLayout = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        relLayout.addRule(RelativeLayout.BELOW, mainIdCnt + mainId);
        container.addView(b, relLayout);
        if (mainId > 0) {
            mainId++;
        }
    }

A few things... 一些东西...

  1. If your main layout is a LinearLayout, you shouldn't need to add a rule to indicate that the button should appear underneath the existing button - it will automatically be added to the very bottom (vertical alignment) or very right (horizontal alignment) of the layout. 如果您的主要布局是LinearLayout,则无需添加规则来指示该按钮应出现在现有按钮的下方-它会自动添加到的最底部(垂直对齐)或最右侧(水平对齐)布局。

  2. All your buttons have the same text. 您所有的按钮都有相同的文本。 Are you certain that you're clicking the first button each time? 您确定每次都单击第一个按钮吗? I note that only your first button has a listener on it, so if you're accidentally clicking one of the other buttons then nothing will happen. 我注意到,只有第一个按钮上有一个侦听器,因此,如果您不小心单击了其他按钮之一,则不会发生任何事情。

  3. If you're intending to add multiple buttons, it will quickly expand to be larger than the screen size, so you should make sure that your main layout is within a ScrollView so that you can see all the buttons you add 如果您打算添加多个按钮,它将迅速扩展到大于屏幕尺寸,因此您应确保主布局位于ScrollView中,以便可以看到添加的所有按钮。

  4. The call to setId() might be stuffing around with the internal workings of Android. 对setId()的调用可能充斥着Android的内部工作原理。 Rather than setting an ID, you should let Android generate the ID automatically, and just retrieve that value if you need to reference it. 除了设置ID外,您还应该让Android自动生成ID,并在需要引用该值时检索该值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM