我在填充了TextViews皮膚Java類的TableLayout上創建了一個10x10板。不久之後,我試着讓所有100個TextViews可點擊,但只有TextView的最後一列和最後一行是可點擊的。我想知道如何讓所有的TextViews可點擊。如何讓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));
}
}
}
也許這會幫助你:http://blog.danlew.net/2015/12/14/making -edittexts -with-links-both-clickable-and-editable/ – piotrek1543
因爲你在**外部設置點擊監聽器**環路 –
@ FrankN.Stein是對的。你把聽衆放在錯誤的地方 – piotrek1543