设置背景图片使按钮不可点击

发布于 2024-11-17 09:54:46 字数 877 浏览 3 评论 0原文

我创建了一系列按钮。当我为每个按钮设置背景图像时,它就变得可解锁(禁用)。为什么会发生这种情况?请任何人向我推荐一下。
我的代码:

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);
        }
    }
}

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);
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

半岛未凉 2024-11-24 09:54:46

没有代码向按钮添加侦听器。这可能就是问题所在。

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

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
             }
         });
牵你手 2024-11-24 09:54:46

请看下面的代码对你有帮助

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();
                    }
                });
            }
        }

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();
                    }
                });
            }
        }
可爱咩 2024-11-24 09:54:46

非常愚蠢的错误..你忘记添加

 btnNAme.setOnClickListener(new View.OnClickListener() {

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

            }
        });

very silly mistake.. you forget to add

 btnNAme.setOnClickListener(new View.OnClickListener() {

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

            }
        });
夏雨凉 2024-11-24 09:54:46

这是为按钮数组创建监听器的好习惯

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


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

this is good practice to make the Listener for array of buttons

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


        OnClickListener myListener = new View.OnClickListener() {
                // @Override
                public void onClick(View v) {//your code
        }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文