动态添加按钮到TableRow

发布于 2024-12-24 16:31:24 字数 979 浏览 2 评论 0原文

您好,我想单击一个按钮,每次单击该按钮时都会向表行添加一个新按钮。将 3 个按钮添加到该行后,我想动态创建一个新的表行并向其添加一个新按钮。

如果单击按钮,我知道如何将带有按钮的行添加到 tableLayout 中。我不知道如何在每次单击时修改表格行,以便添加一个附加按钮。

任何建议都会非常有帮助和感激。

以下是我的代码,但这并不完美,

public class DynamicTableView extends Activity {

    TableLayout mTlayout;
    String[] mTextofButton = { "Dipak", "E", "I", "J", "L",
            "M", "G", "R", "N", "T", "H", "P",
            "K", "Y", "V" };

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


        TableRow tr=new TableRow(this);
        for(int i=0;i<mTextofButton.length;i++){
            Button btn=new Button(this);
            btn.setText(mTextofButton[i]);
            tr.addView(btn);

        }   
        mTlayout.addView(tr);
    }
}

提前致谢。

Hello I want to click a button that will add a new button to a table row every time it is clicked. After 3 buttons have been added to the row, I want to dynamically create a new table row and add a new button to it.

I know how to add a row with a button(s) to a tableLayout if I click a button. I don't know how to modify the table row every time I click so I can add an additional button.

Any advice would be quite helpful and appreciated.

Following is my code but this is not perfect

public class DynamicTableView extends Activity {

    TableLayout mTlayout;
    String[] mTextofButton = { "Dipak", "E", "I", "J", "L",
            "M", "G", "R", "N", "T", "H", "P",
            "K", "Y", "V" };

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


        TableRow tr=new TableRow(this);
        for(int i=0;i<mTextofButton.length;i++){
            Button btn=new Button(this);
            btn.setText(mTextofButton[i]);
            tr.addView(btn);

        }   
        mTlayout.addView(tr);
    }
}

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

迷雾森÷林ヴ 2024-12-31 16:31:24

使用以下代码:-

public class DynamicButtonsActivity extends Activity {

    TableLayout mTlayout;
    TableRow tr;
    String[] mTextofButton = { "D", "E", "I", "J", "L", "M", "G", "R", "N",
            "T", "H", "P", "K", "Y", "V" };

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

        int i = 0;
        while (i < mTextofButton.length) {
            if (i % 3 == 0) {
                tr = new TableRow(this);
                mTlayout.addView(tr);
            }
            Button btn = new Button(this);
            btn.setText(mTextofButton[i]);
            btn.setId(i);
            btn.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    System.out.println("v.getid is:- " + v.getId());
                }
            });
            tr.addView(btn);
            i++;
        }
    }
}

Use following Code:-

public class DynamicButtonsActivity extends Activity {

    TableLayout mTlayout;
    TableRow tr;
    String[] mTextofButton = { "D", "E", "I", "J", "L", "M", "G", "R", "N",
            "T", "H", "P", "K", "Y", "V" };

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

        int i = 0;
        while (i < mTextofButton.length) {
            if (i % 3 == 0) {
                tr = new TableRow(this);
                mTlayout.addView(tr);
            }
            Button btn = new Button(this);
            btn.setText(mTextofButton[i]);
            btn.setId(i);
            btn.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    System.out.println("v.getid is:- " + v.getId());
                }
            });
            tr.addView(btn);
            i++;
        }
    }
}
情场扛把子 2024-12-31 16:31:24
    int i=0;
    while(i<mTextofButton.length){
            if(i%3==0){
                TableRow tr=new TableRow(this);
                mTlayout.addView(tr);
            }
            Button btn=new Button(this);
            btn.setText(mTextofButton[i]);
            tr.addView(btn);
            i++;
    }
    int i=0;
    while(i<mTextofButton.length){
            if(i%3==0){
                TableRow tr=new TableRow(this);
                mTlayout.addView(tr);
            }
            Button btn=new Button(this);
            btn.setText(mTextofButton[i]);
            tr.addView(btn);
            i++;
    }
彻夜缠绵 2024-12-31 16:31:24

他们会使用 ButtononClick(),说 addNewButton() 并执行类似以下操作:

public void  addNewButton(View b)
{
        for(int i=0; i<mTextofButton.length; i+=3){
                TableRow tr=new TableRow(this);

                Button btnOne=new Button(this);
                btnOne.setText(mTextofButton[i]);
                tr.addView(btnOne);

                Button btnTwo =new Button(this);
                btnTwo.setText(mTextofButton[i+1]);
                tr.addView(btnTwo);

                Button btnThree =new Button(this);
                btnThree.setText(mTextofButton[i+2]);
                tr.addView(btnThree);
                mTlayout.addView(tr);
        }
}

但要小心您的 mTextofButton .length,意味着它应该能被3整除,否则你会得到ArrayIndexOutOfBondException。更好的方法是使用 ArrayList

their would a Button's onClick(), say addNewButton() and do something like this:

public void  addNewButton(View b)
{
        for(int i=0; i<mTextofButton.length; i+=3){
                TableRow tr=new TableRow(this);

                Button btnOne=new Button(this);
                btnOne.setText(mTextofButton[i]);
                tr.addView(btnOne);

                Button btnTwo =new Button(this);
                btnTwo.setText(mTextofButton[i+1]);
                tr.addView(btnTwo);

                Button btnThree =new Button(this);
                btnThree.setText(mTextofButton[i+2]);
                tr.addView(btnThree);
                mTlayout.addView(tr);
        }
}

but be careful about your mTextofButton.length, means it should divisable by 3 otherwise you gonna getting ArrayIndexOutOfBondException. better method is to use ArrayList<String>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文