SLaks
2013-02-25 02:33
采纳率: 60.5%
浏览 2.8k

如何实现动态创建按钮的监听器?

我使用 RelativeLayout 动态的创建了按钮,现在要实现动态按钮的监听器。同时我创建了多个按钮。如何实现这个功能?
这是我使用的代码:

public class CreateButton extends Activity {

    int top=60,bottom=350, i;
    int s = 1,iddelete = 1,idedit=1;
    RelativeLayout rl;

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

        rl = (RelativeLayout)findViewById(R.id.edit_r);

        // Create Bed Room
        Button Btn_BedRoom = (Button)findViewById(R.id.bed);
        Btn_BedRoom.setOnClickListener(ListenrOf_BedRoom);

    }

 // Listener Of Create Bed Room
    private OnClickListener ListenrOf_BedRoom = new OnClickListener()
    {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            CreateRoom();
            i+=35;
        }
    };


    public void CreateRoom()
    {       
        final Button delete = new Button(this);
        delete.setText("delete");
        delete.setBackgroundResource(R.drawable.cross);

        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);             
        lp.setMargins(120, top+i, 0, bottom-i);  
        delete.setHeight(5);
        delete.setWidth(5);
        delete.setLayoutParams(lp);
              delete.setOnClickListener(new OnClickListener(){
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        CreateRoom();           
    }
    });                   
              rl.addView(delete);


        Button BtnEdit = new Button(this);
        RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);             
        lp1.setMargins(150,top+i,0,bottom-i);
        BtnEdit.setBackgroundResource(R.drawable.edit);
        BtnEdit.setHeight(5);
        BtnEdit.setWidth(5);
        BtnEdit.getRight();
        BtnEdit.setLayoutParams(lp1);
        rl.addView(BtnEdit);

        TextView tv = new TextView(this);
        RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        lp2.setMargins(180,top+i,0,bottom-i);
        tv.setText("Bed Room " + s++);
        tv.setLayoutParams(lp2);
        rl.addView(tv);
    };
}
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • nana4ever 2013-02-25 03:30
    已采纳
    Button bt = new Button(this.getApplicationContext());
    bt.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
          // do something when the button is clicked
        }
    });
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • O_Casper_ne 2013-03-05 07:39
    Button mybutton = new Button(this);
    mybutton.setText("My Button");
    mybutton.setOnClickListener(this);
    

    使用当前的类实现 OnClickListener 方法并且重写 onClick()方法

    评论
    解决 无用
    打赏 举报