我正在尝试使 textview 可点击。我有 50 个文本视图,所以我用 java 代码创建它们。问题是我不知道如何让它们可点击。如果我使用 XMl,那会很容易,因为这个问题被问了很多。
这是我的代码:
package com.example.goo;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
public class Calendrier extends Activity implements OnClickListener{
LinearLayout linear;
TextView[] textViewArray = new TextView[50];
TextView[] textViewArray2 = new TextView[50];
LinearLayout[] layoutArray = new LinearLayout[50];
ScrollView SV;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SV = new ScrollView(this);
linear = new LinearLayout(this);
//Crée et Affiche les 50 textview sur lesquelles on cliquera
for (int i = 0; i < 50; i++) {
textViewArray[i] = new TextView(this);
textViewArray[i].isClickable();
textViewArray[i].setText("Journée" + (i+1));
linear.addView(textViewArray[i]);
}
//Création de 50 textview qui seront cette fois ajoutés dans leur layout perso
for (int i = 0; i < 50; i++) {
textViewArray2[i] = new TextView(this);
textViewArray2[i].setText("Journée" + (i+1));
}
//Création 50 layout
for (int i = 0; i < 50; i++) {
layoutArray[i] = new LinearLayout(this);
}
for (int i = 0; i < 50; i++) {
layoutArray[i].addView(textViewArray2[i]);
}
linear.setOrientation(LinearLayout.VERTICAL);
SV.addView(linear);
setContentView(SV);
}
@Override
public void onClick(DialogInterface dialog, int which) {
//What should I do here ?
//I'd like if I click on the textview n°1, the layout I create before ( layoutArray[i] = new LinearLayout(this); ) appears
if (dialog == textViewArray[0]){
System.out.println("this text never prints :( ");
}
}
}