以编程方式创建按钮

时间:2014-09-25 10:01:37

标签: android button

可以通过此代码创建一个按钮,但我想点击一个按钮并一次又一次地创建新按钮(或其他任何内容)。怎么办?

Button b = new Button();
tr.setLayoutParams(new TableRow.LayoutParams(
        TableRow.LayoutParams.MATCH_PARENT,
        TableRow.LayoutParams.WRAP_CONTENT));
b.setText("Dynamic Button");
b.setLayoutParams(new TableRow.LayoutParams(
        TableRow.LayoutParams.MATCH_PARENT,
        TableRow.LayoutParams.WRAP_CONTENT));
tr.addView(b,index);
tl.addView(tr, new TableLayout.LayoutParams(
        TableLayout.LayoutParams.MATCH_PARENT,
        TableLayout.LayoutParams.WRAP_CONTENT));

5 个答案:

答案 0 :(得分:1)

将Onclick侦听器添加到按钮并将按钮添加到现有表

 b.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
      // add button in table
       tr.addView(b,index);
    }
})

答案 1 :(得分:0)

Button b = new Button();
...
b.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
    }
})

答案 2 :(得分:0)

Button newButton(){
 Button b = new Button();
            tr.setLayoutParams(new TableRow.LayoutParams(
                    TableRow.LayoutParams.MATCH_PARENT,
                    TableRow.LayoutParams.WRAP_CONTENT));
            b.setText("Dynamic Button");
            b.setLayoutParams(new TableRow.LayoutParams(
                    TableRow.LayoutParams.MATCH_PARENT,
                    TableRow.LayoutParams.WRAP_CONTENT));
            tr.addView(b,index);
            tl.addView(tr, new TableLayout.LayoutParams(
                    TableLayout.LayoutParams.MATCH_PARENT,
                    TableLayout.LayoutParams.WRAP_CONTENT));
}

newButton().setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        newButton();
    }
})

你可以写这样的代码....试试自己......

答案 3 :(得分:0)

如果您想在每次点击任何按钮时创建新按钮,您可以在活动中实施OnClickListener,并覆盖onClick方法。

示例摘录:

public class MainActivity extends Activity implements OnClickListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //your code
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        //create a new button or textview
    }
}

答案 4 :(得分:0)

public class test extends Activity {

    private List<Button> mButtons = new ArrayList<Button>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_teeest);

        final LinearLayout ll = (LinearLayout)findViewById(R.id.linearlayout);


        Button b = new Button(this);
        b.setText("Add Dynamic Button");
        b.setBackgroundColor(Color.GREEN);
        b.setVisibility(View.VISIBLE);
        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Button b1 = new Button(teeest.this);
                b1.setText("DYNAMIC BUTTON" + mButtons.size() + "");
                b1.setWidth(100);
                b1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Toast.makeText(teeest.this, "New Dynamic Button clicked", Toast.LENGTH_SHORT).show();
                    }
                });

                mButtons.add(b1);
                ll.addView(b1);
            }
        });

        Button bb = new Button(this);
        bb.setText("Remove Dynamic Button");
        bb.setBackgroundColor(Color.RED);
        bb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(mButtons.size()>0) {
                    ll.removeViewAt(mButtons.size() + 1);
                    mButtons.remove(mButtons.size() - 1);
                }
            }
        });

        ll.addView(b);
        ll.addView(bb);

    }
}