我创建了一个按钮数组,当我将背景图像设置为每个按钮时,它变得无法点击(禁用)..为什么会发生这种情况..请任何人建议我..我正在发送我的代码
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);
}
}
}
答案 0 :(得分:1)
没有代码向按钮添加侦听器。这可能就是问题所在。
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
答案 1 :(得分: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.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();
}
});
}
}
答案 2 :(得分:0)
非常愚蠢的错误..你忘了添加
btnNAme.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0)
{
//some code
}
});
答案 3 :(得分:0)
这是为按钮数组生成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
}
}