Android 动态生成多个控件并实现点击

本文介绍了如何在Android中动态生成多个按钮,并为这些按钮设置点击事件。通过示例代码展示了实现过程,包括界面布局和活动的交互。完整示例可在优快云下载。

我们来看下动态效果图,如下:


首先这个按钮是根据所填的数动态生成的,然后还要设置他的点击事件。这个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.youkuaiyun.com/detail/aa_chao/9684921

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值