我写过这样的东西......
TableRow tr1;
TableRow tr2;
TextView txt9;
...
TableLayout tl = (TableLayout)findViewById(R.id.myTableLayout);
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(width, LayoutParams.FILL_PARENT);
for (int i = 0; i < strarr.length-1; i++)
{
strinarr = fun1.split(strarr[i].trim(),"^");
tr1 = (TableRow) new TableRow(this);
txt1=new TextView(this);
txt9.setText(strinarr[0]);
txt9.setBackgroundColor(intblue);
txt9.setTextColor(intwhite);
txt9.setClickable(true);
txt9.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Log.i("pagename",strpagename);
String currenttext = txt9.getText().toString());
}
}
});
tr1.addView(txt9);
tl.addView(tr1,new TableLayout.LayoutParams(layoutParams));
}
我能够在点击时获取文本,但愚蠢的是我在所有 textview 单击事件中获取表格中最后一个 textview 的文本...如果有人能告诉我如何在焦点或触摸上捕捉 textview 的文本这真的很有帮助...
最佳答案
将 String currenttext = txt9.getText().toString());
更改为 String currenttext = ((TextView)v).getText().toString());
关于java - 如何在动态生成的表中获取 TextView 的文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5537239/