Android 动态生成多个控件并实现点击
2016-11-16 17:40
441 查看
我们来看下动态效果图,如下:
首先这个按钮是根据所填的数动态生成的,然后还要设置他的点击事件。这个demo需要两个layout和一个activity:
首先来看下这两个布局文件:
1.这个是界面的布局文件
接下来就是activity的代码了
想要完整demo,可以点这里哦!http://download.csdn.net/detail/aa_chao/9684921
首先这个按钮是根据所填的数动态生成的,然后还要设置他的点击事件。这个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
相关文章推荐
- android 动态添加控件并实现每个子控件的点击事件
- android实现动态生成控件(setTag和getTag属性的用法)
- Android 动态生成复杂的布局(View布局的子控件含自己的点击事件)
- Android动态生成控件如何取值
- VC程序中实现控件的动态生成与响应
- VC程序中实现控件的动态生成与响应
- android 动态实现点击ImageButton更换图片
- android控件之Spinner(动态生成下拉内容)
- C++ Builder 实现动态生成窗口、控件,以及处理控件事件
- 内容选择android控件之Spinner(动态生成下拉内容)
- 动态生成服务器控件,点击按钮获取
- 动态添加UIButton控件,通过设置tag值实现点击不同的UIButton控件做出不同的反应
- VC程序中实现控件的动态生成与响应 【转贴】
- VC程序中实现控件的动态生成与响应
- VC程序中实现控件的动态生成与响应
- VC程序中实现控件的动态生成与响应(一)
- Android 动态布局 动态生成 销毁控件 改变控件的位置等
- Android在程序中动态生成控件,动态布局