自定义表格控件(通过TabLayout+TabRow)获取表格所有数据,并对表格进行相关事件监听
无情
阅读:629
2021-04-01 10:06:33
评论:0
相关知识点:TabLayout布局与TabRow数据行的关系。
效果代码:
重点是:动态生成表格和动态表格的数据获取-------自己先做下笔记,以防以后的项目使用到自定义表格,朋友们可以根据需求自行修改。
相关源代码(布局文件代码):
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#dedcd2"
android:stretchColumns="*" >
<TableRow
android:layout_margin="0.5dip"
android:background="#dedcd2" >
<TextView
android:background="#ffffff"
android:gravity="center"
android:text="年度"
android:textSize="20dip"
android:textStyle="bold" />
<TextView
android:gravity="center"
android:background="#ffffff"
android:text="本金"
android:textSize="20dip"
android:textStyle="bold" />
<TextView
android:gravity="center"
android:background="#ffffff"
android:text="利息"
android:textSize="20dip"
android:textStyle="bold" />
</TableRow>
</TableLayout>
<TableLayout
android:id="@+id/tablelayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#dedcd2"
android:stretchColumns="*" >
</TableLayout>
<Button
android:id="@+id/myButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="动态生成表格" />
<Button
android:id="@+id/data"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="表格数据" />
</LinearLayout>
核心代码:
package com.view.tableview;
import java.util.ArrayList;
import java.util.List;
import com.view.bean.Customer;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.Toast;
public class MainActivity extends Activity {
//相关按钮
private Button add;
private Button data;
//行统计
private int count=0;
TableRow tablerow;
TableLayout table;
//动态添加相关控件
EditText year;
EditText cash;
EditText time;
//相关存储
List<Customer> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tableview);
list=new ArrayList<Customer>();
add=(Button) findViewById(R.id.myButton);
add.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
count++;
addWegit();
}
});
data=(Button) findViewById(R.id.data);
//获取动态组件的值
data.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
for (int i = 0; i < table.getChildCount(); i++) {
Toast.makeText(MainActivity.this,
"总行数" + table.getChildCount(), Toast.LENGTH_LONG)
.show();
//循环遍历表格行
TableRow row = (TableRow) table.getChildAt(i);
//获取行相关单元格的数据信息
EditText years = (EditText) row.getChildAt(0);
EditText times = (EditText) row.getChildAt(1);
EditText cashs = (EditText) row.getChildAt(2);
Customer customer = new Customer();
customer.setMoney("" + years.getText().toString());
customer.setTime("" + times.getText().toString());
customer.setYeat("" + cashs.getText().toString());
list.add(customer);
}
//相关数据测试
for (int i = 0; i < list.size(); i++) {
System.out.println("相关信息:" + list.get(i).getMoney());
}
}
});
}
//添加控件相关方法
public void addWegit() {
table = (TableLayout) findViewById(R.id.tablelayout);
table.setStretchAllColumns(true);
tablerow = new TableRow(MainActivity.this);
tablerow.setBackgroundColor(Color.WHITE);
year = new EditText(MainActivity.this);
cash = new EditText(MainActivity.this);
time = new EditText(MainActivity.this);
year.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "点击第一列", Toast.LENGTH_LONG).show();
}
});
cash.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "点击第二列", Toast.LENGTH_LONG).show();
}
});
time.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "点击第三列", Toast.LENGTH_LONG).show();
}
});
tablerow.addView(year);
tablerow.addView(time);
tablerow.addView(cash);
table.addView(tablerow, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
}
}
实体类相关代码:
package com.view.bean;
import java.io.Serializable;
public class Customer implements Serializable {
/**
* 相关属性和构造方法
*/
private static final long serialVersionUID = 1L;
private String yeat;
private String money;
private String time;
public Customer() {
super();
}
//set和 get方法
public String getYeat() {
return yeat;
}
public void setYeat(String yeat) {
this.yeat = yeat;
}
public String getMoney() {
return money;
}
public void setMoney(String money) {
this.money = money;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
}
实验结果:
声明
1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。