我在填充了TextViews皮肤Java类的TableLayout上创建了一个10x10的板。不久之后,我尝试让所有100个TextViews Clickable,但只有TextView的最后一列和最后一行是可点击的。我想知道如何让所有TextView都可以点击。
编辑:将onClick置于循环内部,因此所有TextView都可以单击,但只有最后一个TextView会改变颜色,无论点击什么。有人会有解决方案吗?public class GameAct extends AppCompatActivity {
TableLayout tableLayout;
TableRow tableRow;
TextView textView;
Chronometer chronometer;
long tempoQuandoParado = 0;
boolean isClickPause = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
chronometer = (Chronometer)findViewById(R.id.chronometer);
tableLayout = (TableLayout) findViewById(R.id.tabuleiro);
GradientDrawable gd = new GradientDrawable();
gd.setStroke(2, 0xFFFFFFFF);
gd.setColor(Color.rgb(0, 0, 128));
final GradientDrawable gradientDrawable = new GradientDrawable();
gradientDrawable.setStroke(2, 0xFFFFFFFF);
gradientDrawable.setColor(Color.RED);
tableLayout = (TableLayout) findViewById(R.id.tabuleiro);
for (int i = 0; i < 10; i++){
tableRow= new TableRow(this);
tableRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
for (int j = 0; j < 10; j++) {
textView = new TextView(this);
textView.setBackgroundDrawable(gd);
textView.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView.setBackgroundDrawable(gradientDrawable);
if (isClickPause) {
chronometer.setBase(SystemClock.elapsedRealtime() + tempoQuandoParado);
chronometer.start();
isClickPause = false;
} else {
chronometer.setBase(SystemClock.elapsedRealtime());
chronometer.start();
tempoQuandoParado = 0;
}
}
});
tableRow.addView(textView,35, 35);
}
tableLayout.addView(tableRow, new TableLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
}
}
}
答案 0 :(得分:1)
在循环内的文本视图中添加Click侦听器
FLAG_ACTIVITY_RESET_TASK_IF_NEEDED