zoukankan      html  css  js  c++  java
  • android_demo之自动生成动态表格

    今天我们学习了如何更好的利用Android 的 layout 布局。

    接下来是个简单的栗子去了解这个自动生成的动态的控件(自动生成表格)

    这是我们的layout 页面

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
        <LinearLayout 
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="6">
            <TableLayout 
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center_vertical">
                <TextView 
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="请输入要生成表格的行号列"
                    android:textSize="15sp"/>
                <TableRow 
                    android:gravity="center"
                    android:layout_marginTop="5dp"
                    >
                     <TextView 
                        android:id="@+id/tv1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="行"
                        android:textSize="15sp"/>
                <EditText 
                    android:id="@+id/et1"
                    android:layout_width="100dp"
                    android:layout_height="wrap_content"/>
                </TableRow>
                <TableRow 
                    android:gravity="center"
                    android:layout_marginTop="5dp"
                    >
    
                    <TextView
                        android:id="@+id/tv2"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="列"
                        android:textSize="15sp" />
    
                <EditText 
                    android:id="@+id/et2"
                    android:layout_width="100dp"
                    android:layout_height="wrap_content"
                    android:numeric="decimal"/>
                <!-- decimal 只能输入数字,大于0的数字-->
                </TableRow>
                <TableRow
                    android:gravity="center"
                    >
                    <Button 
                        android:id="@+id/btnadd"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_span="2"
                        android:text="确定"
                        
                        />
                </TableRow>
            </TableLayout>
         </LinearLayout>
        <TableLayout 
            android:id="@+id/table"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="3"
            android:stretchColumns="*"
            android:shrinkColumns="*">
            
        </TableLayout>
    </LinearLayout>

    再看下我们的 mainActivity.class

    package com.example.counter;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TableLayout;
    import android.widget.TableRow;
    
    public class MainActivity extends Activity implements OnClickListener {
    
        private Button submit;
        private EditText row;
        private EditText cloumn;
        private TableLayout table;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.canchange);
            submit = (Button) findViewById(R.id.btnadd);
            
            row = (EditText) findViewById(R.id.et1);
            cloumn = (EditText) findViewById(R.id.et2);
            table = (TableLayout) findViewById(R.id.table);
            
            submit.setOnClickListener(this);
    
        }
    
        @Override
        public void onClick(View v) {
            int c = Integer.parseInt(row.getText() + "");
            int r = Integer.parseInt(cloumn.getText() + "");
            table.removeAllViews();
            for (int i = 0; i < c; i++) {
                TableRow tr = new TableRow(this);
    
                for (int j = 0; j < r; j++) {
                    Button b = new Button(this);
    
                    tr.addView(b);
                }
                table.addView(tr);
            }
        }
    
    }

    运行结果:

    可以根据自己的需求去动态生成表格,在这里我们要了解这个动态的概念。

    大家可以将代码拷贝去自行实现一番。(调皮~)

  • 相关阅读:
    PHP常用数组函数介绍
    php字符串处理函数大全
    Laravel框架数据库CURD操作、连贯操作使用方法
    yii gii自动生成的curd添加批量删除实例
    负载均衡的几种实现方式
    php导出excel
    php导入excel文件
    html+php实现无刷新上传文件
    python3.3 基础 新特性
    二叉树的遍历
  • 原文地址:https://www.cnblogs.com/heyhhz/p/6095335.html
Copyright © 2011-2022 走看看