Android开发之动态添加控件
动态添加TextView控件:
一:创建一个Android project项目
activity_main.xml文件:
1、用两个LinearLayout布局分别包裹一对TextView,EditText控件,将orientation设置为水平方向,EditText的hint属性可以实现水印效果,两个EditText用来控制显示(TextView控件数量)的行和列。
2、 用一个LinearLayout布局包裹Button按钮,在EditText控件输入完后,点击button按钮,就会自动生成控件。
3、 用一个TableLayout布局用表格的形式显示生成的控件。
<LinearLayout
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:gravity="center_vertical"
android:layout_height="match_parent"
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="请输入数字!"
android:numeric="decimal" /> </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:gravity="center_vertical"
android:layout_height="match_parent"
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="请输入数字!"
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> <TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/table1"> </TableLayout>

MainActivity.java文件:
1、WC和MP变量分别用来设置自动生成TextView控件的宽高。
2、首先创建两个EditText,一个Button,一TableLayout变量,并且通过FindViewById(R)获取控件相对应的id.
3、Button通过setOnClickListener(New OnClickListener){});方法添加监听事件,实现Onclick()点击按钮触发事件。
4、两个输入宽通过Integer.parseInt(row.getText().toString())的方法获取输入的内容并将其转换为整型,getText()获取输入值。
5、代码创建控件:通过new 控件(MainActivity.this)的方式创建控件,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;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取控件Button
bt1=(Button) findViewById(R.id.button1);
//获取文本输入框控件
row=(EditText) findViewById(R.id.editText1);
column=(EditText) findViewById(R.id.editText2);
//给button按钮绑定单击事件
bt1.setOnClickListener(new 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 = (TableLayout)findViewById(R.id.table1);
//清除表格所有行
tableLayout.removeAllViews();
//全部列自动填充空白处
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,"请输入行和列",1).show();
}
}
});
}
}
6、双重循环,最外面一层用来创建TableRow行数的,里面用来创建列数的。
7、TableLayout表格布局可以通过removeAllViews()方法清除表格数据,防止点击两次出现重复的内容。通过setStretchAllColumns(true)设置全部列自动填充空白处。
8、 TableLayout表格布局中的TableRow相当于table的tr标签,可以通过addView()将tr追加到表中,控件也可以通过这个方法追加到行(TableRow)中.
Android开发之动态添加控件的更多相关文章
- Android 在布局容器中动态添加控件
这里,通过一个小demo,就可以掌握在布局容器中动态添加控件,以动态添加Button控件为例,添加其他控件同样道理. 1.addView 添加控件到布局容器 2.removeView 在布局容器中删掉 ...
- winform导入导出excel,后台动态添加控件
思路: 导入: 1,初始化一个OpenFileDialog类 (OpenFileDialog fileDialog = new OpenFileDialog();) 2, 获取用户选择文件的后缀名(s ...
- VC中动态添加控件
VC中动态添加控件 动态控件是指在需要时由Create()创建的控件,这与预先在对话框中放置的控件是不同的. 一.创建动态控件: 为了对照,我们先来看一下静态控件的创建. 放置静态控件时必须先建立一个 ...
- jQuery EasyUI动态添加控件或者ajax加载页面后不能自动渲染问题的解决方法
博客分类: jquery-easyui jQueryAjax框架HTML 现象: AJAX返回的html无法做到自动渲染为EasyUI的样式.比如:class="easyui-layout ...
- Android开发中目前流行控件和知识点总结
Android开发中目前流行控件和知识点总结 1.SlidingMenu 滑动菜单 应用案例:Facebook . Path 2.0 .人人.网易新闻 下载地址: https://github.c ...
- asp.net动态添加控件学习
看了老师的教程后,自己一点感悟记录下来: 1.在页面提交后,动态生成的控件会丢失, 但如果生成控件的代码在pageload中,就可以,原理是每次生成页面都执行生成. 2.动态按件或页面原来控件, 在页 ...
- Android开发工程师文集-相关控件的讲解,五大布局
前言 大家好,给大家带来Android开发工程师文集-相关控件的讲解,五大布局的概述,希望你们喜欢 TextView控件 TextView控件有哪些属性: android:id->控件的id a ...
- WPF:理解ContentControl——动态添加控件和查找控件
WPF:理解ContentControl--动态添加控件和查找控件 我认为WPF的核心改变之一就是控件模型发生了重要的变化,大的方面说,现在窗口中的控件(大部分)都没有独立的Hwnd了.而且控件可以通 ...
- 怎样在不对控件类型进行硬编码的情况下在 C#vs 中动态添加控件
文章ID: 815780 最近更新: 2004-1-12 这篇文章中的信息适用于: Microsoft Visual C# .NET 2003 标准版 Microsoft Visual C# .NET ...
随机推荐
- hdu 1864 最大报销额 01背包
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s) ...
- NET Core 实战 Dapper 扩展数据访问
NET Core 实战:基于 Dapper 扩展你的数据访问方法 一.前言 在非静态页面的项目开发中,必定会涉及到对于数据库的访问,最开始呢,我们使用 Ado.Net,通过编写 SQL 帮助类帮我们实 ...
- java-抽象类的特点
1.抽象类和抽象方法必须用abstract关键字修饰. - abstract class 类名() - public abstract void eat(); 2.抽象类不一定有抽象方法,有抽象方法的 ...
- PostgreSQL的配置文件
PostgreSQL的配置文件主要有如下3个(postgresql.conf,pg_hba.conf,pg_ident.conf)可以通过如下方式查找:postgres=# select name, ...
- 推荐一些好的linux学习网站
菜鸟教程:这个网站有jsp,php,c,android等等入门教程,很适合入门的新手和想多学一门语言的人 传送门http://www.runoob.com/ linux命令那么多,怎么记,给一个lin ...
- python基础(七)——网络编程
服务端 我们使用 socket 模块的 socket 函数来创建一个 socket 对象.socket 对象可以通过调用其他函数来设置一个 socket 服务. 现在我们可以通过调用 bind(hos ...
- csvn使用入门
在前面我们已经配置好了csvn服务器,直达链接http://blog.csdn.net/qq_34829953/article/details/78285647 现在我们在win10环境下使用我们搭建 ...
- Using Elixir Dynamic Supervisors
转自: https://blog.smartlogic.io/elixir-dynamic-supervisors/ I have been working on my side project Gr ...
- windows下能搭建php-fpm吗 phpstudy
这个Windows和Linux系统是不一样的,因为一般nginx搭配php需要php-fpm中间件,但是Windows下需要第三方编译. 下载的包里有php-cgi.exe 但不是php-fpm如果想 ...
- httpd
http://httpd.apache.org/docs/2.2/logs.html httpd.conf文件 Configuration and logfile names: If the file ...