我有一个动态膨胀的布局。
<TableLayout>
<!-- Two more tablelayout here -->
<ScrollView>
<TableLayout>
<!-- Here a tablerow is added using inflate layout -->
<TableLayout>
</ScrollView>
</TableLayout>
Run Code Online (Sandbox Code Playgroud)
表格行的布局如下所示。
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
style="@style/savedItemRow">
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
style="@style/textviewSaved"
android:id="@+id/text_item"/>
<TextView
android:id="@+id/text_item_responce"
style="@style/textviewSavedSub" />
</TableLayout>
</TableRow>
Run Code Online (Sandbox Code Playgroud)
我想听一下单击text_item id的情况(请记住,我已经在运行时动态创建了表行)。
我是一个Android菜鸟。我不确定我是否以正确的方式问了这个问题。我想在运行时以某种方式获取单击项的属性,以便可以对其进行操作。谢谢。
在TexView布局中添加android:clickable以使TextView Clickable:
android:clickable="true"
Run Code Online (Sandbox Code Playgroud)
在代码部分中:
TextView text = new TextView(this);
text.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
text.setText("YOUR TEXT");
text.setId(5111);
text.setClickable(true);
text.setOnClickListener(handler);
View.OnClickListener handler =new View.OnClickListener() {
public void onClick(View v) {
switch (v.getId()) {
case R.id.mytextviewone: // doStuff
break;
case R.id.mytextviewone: // doStuff
break;
}
}
Run Code Online (Sandbox Code Playgroud)
}