简体   繁体   English

设置背景图片使按钮无法点击

[英]Setting background image makes the button unclickable

I have created an array of buttons.我创建了一系列按钮。 When I set the background image to each button it becomes unlockable (disabled).当我将背景图像设置为每个按钮时,它变得可解锁(禁用)。 Why this happen?为什么会这样? Please, anyone, suggest it to me.请任何人向我推荐它。
My code:我的代码:

LinearLayout layoutVertical = (LinearLayout) findViewById(R.id.liVLayout);
    LinearLayout rowLayout=null;
    Button[][] buttons = new Button[6][7]; 

    LayoutParams param = new LinearLayout.LayoutParams(
            LayoutParams.FILL_PARENT,
            LayoutParams.FILL_PARENT,1);

    for (int i = 0; i<6; i++) 
    {
       rowLayout = new LinearLayout(this);
       rowLayout.setWeightSum(7);
       layoutVertical.addView(rowLayout,param);
        for(int j=0;j<7;j++)
        {
            buttons[i][j]=new Button(this);
            buttons[i][j].setText("1");

            buttons[i][j].setBackgroundResource(R.drawable.but_blue_clicked);
            rowLayout.addView(buttons[i][j],param);
            buttons[i][j].setClickable(true);
        }
    }
}

There is no code adding listener to button.没有将侦听器添加到按钮的代码。 This is probably the problem.这大概就是问题所在。

button.setOnClickListener(new View.OnClickListener() {
             public void onClick(View v) {
                 // Perform action on click
             }
         });

Please look below code it helpful to you请看下面对你有帮助的代码

LinearLayout layoutVertical = (LinearLayout) findViewById(R.id.liVLayout);
        LinearLayout rowLayout = null;
        Button[][] buttons = new Button[6][7];

        LayoutParams param = new LinearLayout.LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 1);

        for (int i = 0; i < 6; i++) {
            rowLayout = new LinearLayout(this);
            rowLayout.setWeightSum(7);
            layoutVertical.addView(rowLayout, param);
            for (int j = 0; j < 7; j++) {
                buttons[i][j] = new Button(this);
                buttons[i][j].setText("1");

                buttons[i][j].setBackgroundResource(R.drawable.btn_yes);
                rowLayout.addView(buttons[i][j], param);
                buttons[i][j].setClickable(true);
                buttons[i][j].setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        Toast.makeText(v.getContext(), "Click Button", 5000).show();
                    }
                });
            }
        }

very silly mistake.. you forget to add非常愚蠢的错误..你忘记添加

 btnNAme.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0)
            {
                //some code

            }
        });

this is good practice to make the Listener for array of buttons这是为按钮数组制作Listener的好习惯

    for (int ImgBtnID = 0; ImgBtnID <=8; ImgBtnID++) 
            {
                ImgBtnArray[ImgBtnID].setOnClickListener(myListener);
            }


        OnClickListener myListener = new View.OnClickListener() {
                // @Override
                public void onClick(View v) {//your code
        }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM