日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > Android >内容正文

Android

Android开发之动态添加控件

發(fā)布時(shí)間:2024/4/15 Android 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android开发之动态添加控件 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

動(dòng)態(tài)添加TextView控件:

一:創(chuàng)建一個(gè)Android project項(xiàng)目

? ? ? ? activity_main.xml文件:

? ? ? ? ? 1、用兩個(gè)LinearLayout布局分別包裹一對(duì)TextView,EditText控件,將orientation設(shè)置為水平方向,EditText的hint屬性可以實(shí)現(xiàn)水印效果,兩個(gè)EditText用來(lái)控制顯示(TextView控件數(shù)量)的行和列。

? ? ? ? ? 2、?用一個(gè)LinearLayout布局包裹Button按鈕,在EditText控件輸入完后,點(diǎn)擊button按鈕,就會(huì)自動(dòng)生成控件。

? ? ? ? ? 3、 用一個(gè)TableLayout布局用表格的形式顯示生成的控件。

<LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical" ><LinearLayoutandroid:layout_weight="1"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal" ><TextView android:layout_width="wrap_content"android:gravity="center_vertical"android:layout_height="match_parent"android:layout_marginLeft="10dp"android:textSize="20sp"android:text="行:"/><EditTextandroid:id="@+id/editText1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:ems="10"android:hint="請(qǐng)輸入數(shù)字!"android:numeric="decimal" /></LinearLayout><LinearLayoutandroid:layout_weight="1"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal" ><TextView android:layout_width="wrap_content"android:gravity="center_vertical"android:layout_height="match_parent"android:layout_marginLeft="10dp"android:textSize="20sp"android:text="列:"/><EditTextandroid:id="@+id/editText2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:ems="10"android:hint="請(qǐng)輸入數(shù)字!"android:numeric="decimal"><requestFocus /></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></LinearLayout><TableLayoutandroid:layout_width="match_parent"android:layout_height="match_parent" android:id="@+id/table1"></TableLayout>

? ?MainActivity.java文件:

? ? ? ? ?1、WC和MP變量分別用來(lái)設(shè)置自動(dòng)生成TextView控件的寬高。

? ? ? ? ?2、首先創(chuàng)建兩個(gè)EditText,一個(gè)Button,一TableLayout變量,并且通過(guò)FindViewById(R)獲取控件相對(duì)應(yīng)的id.

? ? ? ? ?3、Button通過(guò)setOnClickListener(New OnClickListener){});方法添加監(jiān)聽(tīng)事件,實(shí)現(xiàn)Onclick()點(diǎn)擊按鈕觸發(fā)事件。

? ? ? ? ?4、兩個(gè)輸入寬通過(guò)Integer.parseInt(row.getText().toString())的方法獲取輸入的內(nèi)容并將其轉(zhuǎn)換為整型,getText()獲取輸入值。

? ? ? ? ?5、代碼創(chuàng)建控件:通過(guò)new 控件(MainActivity.this)的方式創(chuàng)建控件,MainActity是匿名類。

public class MainActivity extends Activity {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;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//獲取控件Buttonbt1=(Button) findViewById(R.id.button1);//獲取文本輸入框控件row=(EditText) findViewById(R.id.editText1);column=(EditText) findViewById(R.id.editText2);//給button按鈕綁定單擊事件bt1.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {if(row.getText().length()>0&&column.getText().length()>0){//把輸入的行和列轉(zhuǎn)為整形int row_int=Integer.parseInt(row.getText().toString());int col_int=Integer.parseInt(column.getText().toString());//獲取控件tableLayout tableLayout = (TableLayout)findViewById(R.id.table1); //清除表格所有行 tableLayout.removeAllViews();//全部列自動(dòng)填充空白處 tableLayout.setStretchAllColumns(true); //生成X行,Y列的表格 for(int i=1;i<=row_int;i++) { TableRow tableRow=new TableRow(MainActivity.this); for(int j=1;j<=col_int;j++) { //tv用于顯示 TextView tv=new TextView(MainActivity.this);//Button bt=new Button(MainActivity.this);tv.setText("("+i+","+j+")"); tableRow.addView(tv); } //新建的TableRow添加到TableLayout tableLayout.addView(tableRow, new TableLayout.LayoutParams(MP, WC,1)); } }else{Toast.makeText(MainActivity.this,"請(qǐng)輸入行和列",1).show();}}});} }

? ? ? ? ?6、雙重循環(huán),最外面一層用來(lái)創(chuàng)建TableRow行數(shù)的,里面用來(lái)創(chuàng)建列數(shù)的。

? ? ? ? ?7、TableLayout表格布局可以通過(guò)removeAllViews()方法清除表格數(shù)據(jù),防止點(diǎn)擊兩次出現(xiàn)重復(fù)的內(nèi)容。通過(guò)setStretchAllColumns(true)設(shè)置全部列自動(dòng)填充空白處。

? ? ? ? ?8、?TableLayout表格布局中的TableRow相當(dāng)于table的tr標(biāo)簽,可以通過(guò)addView()將tr追加到表中,控件也可以通過(guò)這個(gè)方法追加到行(TableRow)中.

轉(zhuǎn)載于:https://www.cnblogs.com/wdht/p/6095225.html

總結(jié)

以上是生活随笔為你收集整理的Android开发之动态添加控件的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。