Android動態(tài)生成控件

MainActivity:

public class MainActivity extends AppCompatActivity {
    private final int WC = ViewGroup.LayoutParams.WRAP_CONTENT;
    private final int MP = ViewGroup.LayoutParams.MATCH_PARENT;
    private EditText row;
    private EditText column;
    private Button bt1;
    private TableLayout tableLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bt1 = (Button) findViewById(R.id.button1);
        row = (EditText) findViewById(R.id.editText1);
        column = (EditText) findViewById(R.id.editText2);

        bt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (row.getText().length() > 0 && column.getText().length() > 0) {
                    int row_int = Integer.parseInt(row.getText().toString());
                    int col_int = Integer.parseInt(column.getText().toString());

                    tableLayout = (TableLayout) findViewById(R.id.table1);
                    tableLayout.removeAllViews();
                    tableLayout.setStretchAllColumns(true);
                    for (int i = 1; i <= row_int; i++) {
                        TableRow tableRow = new TableRow(MainActivity.this);
                        for(int j = 1; j <= col_int; j++) {
                            TextView tv = new TextView(MainActivity.this);
                            tv.setText("(" + i + "," + j + ")");
                            tableRow.addView(tv);
                        }
                        tableLayout.addView(tableRow, new TableLayout.LayoutParams(MP, WC, 1));
                    }
                }
                else {
                    Toast.makeText(MainActivity.this, "請輸入行和列", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}

activity_main:

<?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="wrap_content"
    android:orientation="vertical">

    <LinearLayout
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center_vertical"
            android:layout_marginLeft="10dp"
            android:textSize="20sp"
            android:text="行"/>

        <EditText
            android:id="@+id/editText1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="請輸入數(shù)字!"
            android:numeric="decimal">
            <requestFocus/>
        </EditText>
    </LinearLayout>

    <LinearLayout
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center_vertical"
            android:layout_marginLeft="10dp"
            android:textSize="20sp"
            android:text="列"/>

        <EditText
            android:id="@+id/editText2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="請輸入數(shù)字醇坝!"
            android:numeric="decimal">

        </EditText>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/button1"
            android:text="生成表格"/>
    </LinearLayout>

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/table1"> </TableLayout>

</LinearLayout>
最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末惦蚊,一起剝皮案震驚了整個濱河市讯嫂,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌欧芽,老刑警劉巖莉掂,帶你破解...
    沈念sama閱讀 218,755評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件憎妙,死亡現(xiàn)場離奇詭異,居然都是意外死亡曲楚,警方通過查閱死者的電腦和手機厘唾,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,305評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來龙誊,“玉大人抚垃,你說我怎么就攤上這事√舜螅” “怎么了鹤树?”我有些...
    開封第一講書人閱讀 165,138評論 0 355
  • 文/不壞的土叔 我叫張陵,是天一觀的道長护昧。 經(jīng)常有香客問我魂迄,道長,這世上最難降的妖魔是什么惋耙? 我笑而不...
    開封第一講書人閱讀 58,791評論 1 295
  • 正文 為了忘掉前任捣炬,我火速辦了婚禮,結果婚禮上绽榛,老公的妹妹穿的比我還像新娘湿酸。我一直安慰自己,他們只是感情好灭美,可當我...
    茶點故事閱讀 67,794評論 6 392
  • 文/花漫 我一把揭開白布推溃。 她就那樣靜靜地躺著,像睡著了一般届腐。 火紅的嫁衣襯著肌膚如雪铁坎。 梳的紋絲不亂的頭發(fā)上蜂奸,一...
    開封第一講書人閱讀 51,631評論 1 305
  • 那天,我揣著相機與錄音硬萍,去河邊找鬼扩所。 笑死,一個胖子當著我的面吹牛朴乖,可吹牛的內(nèi)容都是我干的祖屏。 我是一名探鬼主播,決...
    沈念sama閱讀 40,362評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼买羞,長吁一口氣:“原來是場噩夢啊……” “哼袁勺!你這毒婦竟也來了?” 一聲冷哼從身側響起畜普,我...
    開封第一講書人閱讀 39,264評論 0 276
  • 序言:老撾萬榮一對情侶失蹤期丰,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后漠嵌,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體咐汞,經(jīng)...
    沈念sama閱讀 45,724評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡盖呼,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,900評論 3 336
  • 正文 我和宋清朗相戀三年儒鹿,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片几晤。...
    茶點故事閱讀 40,040評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡约炎,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出蟹瘾,到底是詐尸還是另有隱情圾浅,我是刑警寧澤,帶...
    沈念sama閱讀 35,742評論 5 346
  • 正文 年R本政府宣布憾朴,位于F島的核電站狸捕,受9級特大地震影響众雷,放射性物質發(fā)生泄漏。R本人自食惡果不足惜砾省,卻給世界環(huán)境...
    茶點故事閱讀 41,364評論 3 330
  • 文/蒙蒙 一编兄、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧狠鸳,春花似錦悯嗓、人聲如沸卸察。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,944評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽洪乍。三九已至,卻和暖如春岂贩,著一層夾襖步出監(jiān)牢的瞬間巷波,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,060評論 1 270
  • 我被黑心中介騙來泰國打工锉屈, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留垮耳,地道東北人。 一個月前我還...
    沈念sama閱讀 48,247評論 3 371
  • 正文 我出身青樓俊嗽,卻偏偏與公主長得像铃彰,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子牙捉,可洞房花燭夜當晚...
    茶點故事閱讀 44,979評論 2 355

推薦閱讀更多精彩內(nèi)容