Android 动态生成多个控件并实现点击
来源:互联网 发布:js中不等于空 编辑:程序博客网 时间:2024/04/29 19:47
我们来看下动态效果图,如下:
首先这个按钮是根据所填的数动态生成的,然后还要设置他的点击事件。这个demo需要两个layout和一个activity:
首先来看下这两个布局文件:
1.这个是界面的布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.cumnication_album.active_create_btn.MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="20dp" android:text="请输入title标签的个数" /> <EditText android:id="@+id/num_et" android:layout_width="50dp" android:layout_height="wrap_content" android:layout_marginRight="20dp" android:inputType="number" android:maxLength="1" /> <Button android:id="@+id/num_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#455178" android:textColor="#fff" android:text="确定" /> </LinearLayout> <LinearLayout android:id="@+id/titles_ll" android:layout_marginTop="20dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#455178" android:orientation="horizontal"></LinearLayout> <TextView android:id="@+id/shownum_tv" android:layout_marginTop="20dp" android:layout_width="match_parent" android:layout_height="wrap_content" /></LinearLayout>2.第二个layout就是加载的button的layout了
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="0dp" android:layout_height="match_parent" android:gravity="center" android:orientation="horizontal" android:padding="5dp"> <Button android:id="@+id/top_one" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:background="@drawable/bg_titlegroup" android:gravity="center" android:textColor="@drawable/bg_item_btn" android:textSize="15sp" /></LinearLayout>
接下来就是activity的代码了
public class MainActivity extends AppCompatActivity { private EditText num_et; private Button num_btn; private LinearLayout titles_ll; private TextView shownum_tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } private void init(){ num_et=(EditText)findViewById(R.id.num_et); num_btn=(Button)findViewById(R.id.num_btn); titles_ll=(LinearLayout)findViewById(R.id.titles_ll); shownum_tv=(TextView)findViewById(R.id.shownum_tv); num_btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(TextUtils.isEmpty(num_et.getText().toString())){ Toast.makeText(MainActivity.this, "请先填个数", Toast.LENGTH_SHORT).show(); return; } if(num_et.getText().toString().equals("0")){ Toast.makeText(MainActivity.this, "不能填0", Toast.LENGTH_SHORT).show(); return; } int num=Integer.parseInt(num_et.getText().toString()); titles_ll.removeAllViews(); for (int i=0;i<num;i++){ final LinearLayout ll= (LinearLayout) LayoutInflater.from(MainActivity.this).inflate(R.layout.combin_item_button,null); ll.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1));//此处设置权重 final Button btn=(Button)ll.findViewById(R.id.top_one); int m=i+1; btn.setText("第"+m+"个按钮"); titles_ll.addView(ll); } for(int j=0;j<titles_ll.getChildCount();j++){ final Button bt=(Button) titles_ll.getChildAt(j).findViewById(R.id.top_one); final int finalJ = j; bt.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { for(int m=0;m<titles_ll.getChildCount();m++){ titles_ll.getChildAt(m).findViewById(R.id.top_one).setEnabled(true); } bt.setEnabled(false); shownum_tv.setText("您点击了"+bt.getText().toString()); } }); } titles_ll.getChildAt(0).findViewById(R.id.top_one).performClick(); titles_ll.getChildAt(0).findViewById(R.id.top_one).setEnabled(false); } }); }}以上就是这个demo的代码。
想要完整demo,可以点这里哦!http://download.csdn.net/detail/aa_chao/9684921
2 0
- Android 动态生成多个控件并实现点击
- android 动态添加控件并实现每个子控件的点击事件
- 动态生成多个VBE按钮控件并操作它们的属性
- 动态生成多个相同的控件
- 动态生成控件并定位
- Android实现动态添加tab并点击移动tab背景
- Android 动态生成控件
- Android 动态生成控件
- 动态生成服务器控件,点击按钮获取
- 如何动态生成多个相同的控件
- Android 动态生成复杂的布局(View布局的子控件含自己的点击事件)
- 动态生成控件 并捕捉事件
- 动态生成控件并取值
- vb.net动态生成控件并排版
- 动态生成控件并设置布局参数
- android 防止对控件的重复点击和同时点击多个控件
- 动态添加未知个View,并动态设置点击事件
- Android防止同时点击多个控件
- RCNN & SPP-net & Fast-RCNN & Faster-RCNN
- hbase启动后HMaster自动关闭
- SWT Jface
- 【BZOJ 1087】【SCOI 2005】互不侵犯King 【状压DP】
- Oracl常用SQL[创建表空间 创建用户 创建表]
- Android 动态生成多个控件并实现点击
- LogStash实践日志分析二:收集数据、入库、数据分析和kibana展示
- h5特殊字符(参考)
- 封装处理表单删除操作的实现方法(Angular js+Mybits)
- 创新的原则(《创新与企业家精神》彼得·德鲁克)
- 对XMl数据操作帮助类
- Android中BluetoothAdapter.startDiscovery方法搜索蓝牙设备不起作用的解决办法
- mycat中间件(三)windows版本mycat1.6版本安装
- 什么是JIT?