gpt4 book ai didi

java - 点击时无法触发

转载 作者:行者123 更新时间:2023-12-02 11:28:01 25 4
gpt4 key购买 nike

无法在 Click 监听器上触发

我试过了

setClickable(true);

但仍然无法工作

minimum sdk is 15

代码没有 xml 文件,我需要以编程方式修复它

完整代码:

public class MainActivity extends AppCompatActivity {

private static char CHAR = 'a';

private Context context;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = this;
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);

int rowNo = ROWS;
int colNo = (screenWidth().widthPixels * ROWS /screenWidth().heightPixels) ;
for (int i = 0; i < 5; i++) {
LinearLayout row = new LinearLayout(this);
row.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1.0f));
for (int j = 0; j < 5; j++) {
String txt = String.valueOf(CHAR);
Cell cell = new Cell(this, txt, 50, 50);
cell.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
System.out.println("value is " + txt);

}
});
row.addView(cell);
CHAR += 1;
}
layout.addView(row);
}
setContentView(layout);
}

private class Cell extends LinearLayout{

public Cell(Context context, final String value, int width, int height) {
super(context);
LinearLayout.LayoutParams cellParams = new LinearLayout.LayoutParams(width, height);
setLayoutParams(cellParams);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
setBackground(getGradientDrawable());
}else{
setBackgroundDrawable(getGradientDrawable());
}

}
private GradientDrawable getGradientDrawable() {
GradientDrawable shape = new GradientDrawable();
shape.setShape(GradientDrawable.RECTANGLE);
shape.setColor(Color.TRANSPARENT);
shape.setStroke(2, Color.BLACK);
return shape;
}
}
}

那么,为什么以及如何解决它。单击时将打印关联的文本

最佳答案

在循环中尝试 onClickListener

 Cell cell = new Cell(this, txt, 50, 50);
cell.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
System.out.println("value is " + value);

}
});

关于java - 点击时无法触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49478677/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com