gpt4 book ai didi

java - 表行中的 OnClicklistener

转载 作者:行者123 更新时间:2023-11-30 03:40:35 25 4
gpt4 key购买 nike

我创建了一个包含表格 View 的 Activity 。标题和表格行通过 Activity 动态添加到表格 View 中。一切正常,但我不明白如何将 onClick 监听器添加到除标题之外的所有行,并获取特定行中列的值。

这是 Activity 的完整代码:

public class ReportListActivity extends Activity {

TableLayout report_table;
TableRow tr_data;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_report_list);

report_table=(TableLayout) findViewById(R.id.report_table);


//---------------Table Header-----------------------------------------------
TableRow tr_head = new TableRow(this);
tr_head.setId(10);
tr_head.setBackgroundColor(Color.GRAY);
tr_head.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));


TextView label_sr_no = new TextView(this);
label_sr_no.setId(20);
label_sr_no.setText("S.No.");
label_sr_no.setTextColor(Color.WHITE);
label_sr_no.setPadding(5,5,5,5);
tr_head.addView(label_sr_no);// add the column to the table row here

TextView label_test_name = new TextView(this);
label_test_name.setId(21);// define id that must be unique
label_test_name.setText("Test Name"); // set the text for the header
label_test_name.setTextColor(Color.WHITE); // set the color
label_test_name.setPadding(5,5,5,5); // set the padding (if required)
tr_head.addView(label_test_name); // add the column to the table row here

TextView label_test_date = new TextView(this);
label_test_date.setId(21);// define id that must be unique
label_test_date.setText("Date"); // set the text for the header
label_test_date.setTextColor(Color.WHITE); // set the color
label_test_date.setPadding(5,5,5,5); // set the padding (if required)
tr_head.addView(label_test_date); // add the column to the table row here



report_table.addView(tr_head, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));

//---------------Table Header-----------------------------------------------



//--------------------Table Body---------------------------
for (int i=1; i<=10; i++)
{
tr_data = new TableRow(this);
tr_data.setId(10);
tr_data.setBackgroundColor(Color.TRANSPARENT);
tr_data.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));


TextView sr_no = new TextView(this);
sr_no.setId(20);
sr_no.setText(""+i);
sr_no.setTextColor(Color.BLACK);
sr_no.setPadding(5,5,5,5);
tr_data.addView(sr_no);// add the column to the table row here

TextView test_name = new TextView(this);
test_name.setId(21);// define id that must be unique
test_name.setText("Speed Test 60(min) Demo-ST-01"); // set the text for the header
test_name.setTextColor(Color.BLACK); // set the color
test_name.setPadding(5,5,5,5); // set the padding (if required)
tr_data.addView(test_name); // add the column to the table row here

TextView test_date = new TextView(this);
test_date.setId(21);// define id that must be unique
test_date.setText("12 Mar 2013"); // set the text for the header
test_date.setTextColor(Color.BLACK); // set the color
test_date.setPadding(5,5,5,5); // set the padding (if required)
tr_data.addView(test_date); // add the column to the table row here



report_table.addView(tr_data, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}

//--------------------Table Body---------------------------


}


}

这是布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".ReportListActivity" >

<ScrollView
android:id="@+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<TableLayout
android:shrinkColumns="0,1,2"
android:id="@+id/report_table" android:layout_weight="1" android:layout_height="wrap_content" android:layout_width="match_parent">
</TableLayout>

</RelativeLayout>

</ScrollView>

</RelativeLayout>

除了表头行之外的表格数据,应该如何实现onclick监听,获取该行所有列的数据?

我关注了this教程!

最佳答案

您必须将 setOnClickListener 放在 tr_data 以及所有 TextView final

final TextView test_name = new TextView(this);
test_name.setId(21);// define id that must be unique
test_name.setText("Speed Test 60(min) Demo-ST-01"); // set the text for the header
test_name.setTextColor(Color.BLACK); // set the color
test_name.setPadding(5,5,5,5); // set the padding (if required)
tr_data.addView(test_name); // add the column to the table row here


report_table.addView(tr_data, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));

tr_data.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

String test_name1= test_name.getText().toString();
Toast.makeText(getApplicationContext(), test_name1, Toast.LENGTH_LONG).show();
}
});

关于java - 表行中的 OnClicklistener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15804335/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com