如何动态添加按钮行

时间:2017-10-27 23:38:46

标签: android android-layout button

我在使用java代码添加buttons_dynamically时遇到了一个大问题 我想在我的应用程序中添加按钮,因此数字取决于回答的字符 如果答案包含 4个字符我应该显示4个按钮,下面的代码只是添加一个按钮我想添加更多按钮我需要帮助

main.java

  TableRow tr= new TableRow(this);
        final Button btn = new Button(this);
       TableRow.LayoutParams tl= new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT
               , TableRow.LayoutParams.WRAP_CONTENT);

        btn.setBackgroundColor(Color.GRAY);
             btn.setText("h");
             tl.setMargins(10,10,0,0);
          btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
           btn.setText(" ");
           }
         });


         tr.addView(btn,tl);

         setContentView(tr);

2 个答案:

答案 0 :(得分:0)

执行所需操作的最直接方法是在布局中添加视图,以充当按钮的占位符。我使用了相对布局。我的主要布局有一个用于输入的edittext,一个触发生成新按钮的按钮,以及一个用于保存新按钮的相对布局。默认情况下,相对布局不会滚动,因此根据您的最终目标,可能需要更多工作。下面是主按钮的onClick监听器:

TableRow tr= new TableRow(MainActivity.this);

            for(char c:editText.getText().toString().toCharArray()) {
                final Button btn = new Button(MainActivity.this);
                TableRow.LayoutParams tl = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT
                        , TableRow.LayoutParams.WRAP_CONTENT);

                btn.setBackgroundColor(Color.GRAY);
                btn.setText(String.valueOf(c));
                tl.setMargins(10, 10, 0, 0);
                btn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        btn.setText(" ");
                    }
                });


                tr.addView(btn, tl);
            }

            holder.removeAllViews();
            holder.addView(tr);

答案 1 :(得分:0)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_test"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.slocamo.ucee.Test">

    <EditText
        android:id="@+id/input"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Eneter Text" />

    <Button
        android:id="@+id/done"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/input"
        android:text="Done" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/done">

        <TableLayout
            android:id="@+id/parent"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="16dp"
            android:background="@color/black" />
    </ScrollView>
</RelativeLayout>

关注并使用以下代码

parentLayout = (TableLayout) findViewById(R.id.parent);
        input = (EditText) findViewById(R.id.input);
        findViewById(R.id.done).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String message = input.getText().toString().trim();
                int size = message.length();
                parentLayout.removeAllViews();
                if (size != 0) {
                    int loop = (message.length() / maxRowButtons) + 1;
                    for (int i = 0; i < loop; i++) {
                        TableRow tableRow = new TableRow(Test.this);
                        int buttonCount = size % maxRowButtons == 0 ? maxRowButtons : size % maxRowButtons;
                        for (int j = 0; j < buttonCount; j++) {
                            final Button btn = new Button(Test.this);
                            TableRow.LayoutParams tl = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT
                                    , TableRow.LayoutParams.WRAP_CONTENT);
                            btn.setBackgroundColor(Color.GRAY);
                            btn.setText(" Hi ");
                            tl.setMargins(10, 10, 0, 0);
                            btn.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    btn.setText(" ");
                                }
                            });
                            tableRow.addView(btn);
                        }
                        parentLayout.addView(tableRow);
                    }
                }
            }
        });
    }

希望它对您有用

相关问题