以编程方式创建按钮不会对点击作出反应

时间:2014-11-17 14:35:02

标签: android button

我以编程方式创建按钮(buttonDynamic1)以存在布局。按钮已创建,当我单击此按钮时,它应打开DatePicker但没有任何反应。我不知道哪里出错。

感谢您的帮助。

    //other code

   private Button buttonDynamic1;

//other code


    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //other code

            rLayout = (RelativeLayout)  findViewById(R.id.rlayout);
            lprams = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT);


    buttonDynamic1 = new Button(this);

    //other code

    }

    ///other code

 private OnClickListener l = new OnClickListener() {

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.button1){
            buttonDynamic1.setOnClickListener(this);


            buttonDynamic1.setId(1);
            buttonDynamic1.setText("BUTTON");

            lprams.setMargins(0, 100, 0, 0);
            lprams.addRule(RelativeLayout.BELOW, R.id.button1);
            lprams.addRule(RelativeLayout.ALIGN_LEFT, R.id.view);
            lprams.addRule(RelativeLayout.ALIGN_RIGHT, R.id.button1);
            buttonDynamic1.setLayoutParams(lprams);
            buttonDynamic1.setBackgroundResource(R.drawable.color1);

            rLayout.addView(buttonDynamic1);

            if (v.getId() == buttonDynamic1.getId() ){
                  final Calendar c = Calendar.getInstance();

                mYear = c.get(Calendar.YEAR);
                mMonth = c.get(Calendar.MONTH);
                mDay = c.get(Calendar.DAY_OF_MONTH);

                DatePickerDialog dpd = new DatePickerDialog(MainActivity.this,
                        new DatePickerDialog.OnDateSetListener() {

                            @Override
                            public void onDateSet(DatePicker view, int year,
                                    int monthOfYear, int dayOfMonth) {
                                buttonDynamic1.setText(dayOfMonth + "/"
                                        + (monthOfYear + 1) + "/" + year);

                            }
                        }, mYear, mMonth, mDay);
                dpd.show();

            }
        }

    }
};

4 个答案:

答案 0 :(得分:1)

因为您没有将监听器附加到按钮:

buttonDynamic1 = new Button(this);
buttonDynamic1.setOnClickListener(l); <====

答案 1 :(得分:1)

您没有为您的按钮设置点击监听器。试试这个:

buttonDynamic1.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {





                  final Calendar c = Calendar.getInstance();

                mYear = c.get(Calendar.YEAR);
                mMonth = c.get(Calendar.MONTH);
                mDay = c.get(Calendar.DAY_OF_MONTH);

                DatePickerDialog dpd = new DatePickerDialog(MainActivity.this,
                        new DatePickerDialog.OnDateSetListener() {

                            @Override
                            public void onDateSet(DatePicker view, int year,
                                    int monthOfYear, int dayOfMonth) {
                                buttonDynamic1.setText(dayOfMonth + "/"
                                        + (monthOfYear + 1) + "/" + year);

                            }
                        }, mYear, mMonth, mDay);
                dpd.show();


        }


});

答案 2 :(得分:0)

您必须为您的按钮添加侦听器。像这样:

buttonDynamic1.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        //your code
    }
});

或使用您的l听众:

buttonDynamic1.setOnClickListener(l);

答案 3 :(得分:0)

首先你需要为你的按钮添加一个ID:#

 buttonDynamic1 = new Button(this);
 buttonDynamic1.setId(YourPositiveIntegerValue);

然后将其设置为Click:

 buttonDynamic1.setOnClickListener(l);

不要在其他按钮的onClick中执行此操作。你还没有完成它的方法:

   if (v.getId() == R.id.button1){
        buttonDynamic1.setOnClickListener(this);


        buttonDynamic1.setId(1);
        buttonDynamic1.setText("BUTTON");

        lprams.setMargins(0, 100, 0, 0);
        lprams.addRule(RelativeLayout.BELOW, R.id.button1);
        lprams.addRule(RelativeLayout.ALIGN_LEFT, R.id.view);
        lprams.addRule(RelativeLayout.ALIGN_RIGHT, R.id.button1);
        buttonDynamic1.setLayoutParams(lprams);
        buttonDynamic1.setBackgroundResource(R.drawable.color1);

        rLayout.addView(buttonDynamic1);

       } //You have forgot to close onClick from button1, make a claim
        //You need to finish the onClick from Your other button first...
        if (v.getId() == buttonDynamic1.getId() ){

所以会发生什么,你在onClick from button1中编写这段代码然后永远无法访问它。