zoukankan      html  css  js  c++  java
  • Android实现动态添加控件

    xml文件:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/activity_student_sign_up"
    android:background="@android:color/darker_gray"
    tools:context="com.example.myapplication.student_signUp" >
    
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="none" >
    
        <LinearLayout
            android:id="@+id/content_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:isScrollContainer="true"
            android:orientation="vertical"
            android:padding="10.0dip" >
    
            <LinearLayout
                android:id="@+id/ll_one"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dip"
                android:background="#FFA2CD5A"
                android:orientation="vertical"
                android:padding="5dip" >
    
                <EditText
                    android:id="@+id/et_content1"
                    android:layout_width="match_parent"
                    android:layout_height="80dip"
                    android:background="#FFFFFFFF"
                    android:gravity="left"
                    android:inputType="textMultiLine"
                    android:paddingLeft="5dip"
                    android:textSize="16sp" />
    
                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="5dip" >
    
                    <ImageButton
                        android:id="@+id/ibn_add1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentRight="true"
                        android:background="@drawable/add" />
                    <!--
                                        <ImageButton
                                            android:id="@+id/ibn_del1"
                                            android:layout_width="wrap_content"
                                            android:layout_height="wrap_content"
                                            android:layout_marginRight="10dip"
                                            android:layout_toLeftOf="@id/ibn_add1"
                                            android:background="@drawable/ic_delete" />
                     -->
                </RelativeLayout>
            </LinearLayout>
        </LinearLayout>
    </ScrollView>
    
    </RelativeLayout>

    java文件

    package com.example.myapplication;
    
    import android.graphics.Color;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.text.InputType;
    import android.view.Gravity;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.ImageButton;
    import android.widget.LinearLayout;
    import android.widget.RelativeLayout;
    import android.widget.TextView;
    import android.widget.Toast;
    
    import java.util.LinkedList;
    import java.util.concurrent.atomic.AtomicInteger;
    
    public class student_signUp extends AppCompatActivity {
    
        // 外围的LinearLayout容器
        private LinearLayout llContentView;
    
        private EditText etContent1;
    
        // “+”按钮控件List
        private LinkedList<ImageButton> listIBTNAdd;
        // “+”按钮ID索引
        private int btnIDIndex = 1000;
        // “-”按钮控件List
        private LinkedList<ImageButton> listIBTNDel;
    
        private int iETContentHeight = 0;   // EditText控件高度
        private float fDimRatio = 1.0f; // 尺寸比例(实际尺寸/xml文件里尺寸)
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_student_sign_up);
    
            initCtrl();
    
    
        }
    
        /**
         * 初始化控件
         */
        private void initCtrl()
        {
            llContentView = (LinearLayout) this.findViewById(R.id.content_view);
            etContent1 = (EditText) this.findViewById(R.id.et_content1);
            listIBTNAdd = new LinkedList<ImageButton>();
            listIBTNDel = new LinkedList<ImageButton>();
    
            // “+”按钮(第一个)
            ImageButton ibtnAdd1 = (ImageButton) this.findViewById(R.id.ibn_add1);
            ibtnAdd1.setOnClickListener(new View.OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    // 获取尺寸变化比例
                    iETContentHeight = etContent1.getHeight();
                    fDimRatio = iETContentHeight / 80;
    
                    addContent(v);
                }
            });
    
            listIBTNAdd.add(ibtnAdd1);
            listIBTNDel.add(null);  // 第一组隐藏了“-”按钮,所以为null
        }
    
        /**
         * 添加一组新控件
         * @param v 事件触发控件,其实就是触发添加事件对应的“+”按钮
         */
        private void addContent(View v) {
            if (v == null) {
                return;
            }
    
            // 判断第几个“+”按钮触发了事件
            int iIndex = -1;
            for (int i = 0; i < listIBTNAdd.size(); i++) {
                if (listIBTNAdd.get(i) == v) {
                    iIndex = i;
                    break;
                }
            }
    
            if (iIndex >= 0) {
                // 控件实际添加位置为当前触发位置点下一位
                iIndex += 1;
                Toast.makeText(this,""+iIndex,Toast.LENGTH_LONG).show();
                // 开始添加控件
    
                // 1.创建外围LinearLayout控件
                LinearLayout layout = new LinearLayout(student_signUp.this);
                LinearLayout.LayoutParams lLayoutlayoutParams = new LinearLayout.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT);
                // 设置margin
                lLayoutlayoutParams.setMargins(0, (int) (fDimRatio * 5), 0, 0);
                layout.setLayoutParams(lLayoutlayoutParams);
                // 设置属性
                layout.setBackgroundColor(Color.argb(255, 162, 205, 90));   // #FFA2CD5A
                layout.setPadding((int) (fDimRatio * 5), (int) (fDimRatio * 5),
                        (int) (fDimRatio * 5), (int) (fDimRatio * 5));
                layout.setOrientation(LinearLayout.VERTICAL);
    
                // 2.创建内部EditText控件
                EditText etContent = new EditText(student_signUp.this);
                LinearLayout.LayoutParams etParam = new LinearLayout.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT, iETContentHeight);
                etContent.setLayoutParams(etParam);
                // 设置属性
                etContent.setBackgroundColor(Color.argb(255, 255, 255, 255));   // #FFFFFFFF
                etContent.setGravity(Gravity.LEFT);
                etContent.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
                etContent.setPadding((int) (fDimRatio * 5), 0, 0, 0);
                etContent.setTextSize(16);
                etContent.setId(generateViewId());
                System.out.println("etContent"+etContent.getId());
                // 将EditText放到LinearLayout里
                //etContent.getId();
                layout.addView(etContent);
    
                // 2.创建内部EditText控件
                EditText etContent1 = new EditText(student_signUp.this);
                LinearLayout.LayoutParams etParam1 = new LinearLayout.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT, iETContentHeight);
                etContent1.setLayoutParams(etParam1);
                // 设置属性
                etContent1.setBackgroundColor(Color.argb(255, 0, 255, 0));   // #FFFFFFFF
                etContent1.setGravity(Gravity.LEFT);
                etContent1.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
                etContent1.setPadding((int) (fDimRatio * 5), 0, 0, 0);
                etContent1.setTextSize(16);
                // 将EditText放到LinearLayout里
                etContent1.setId(generateViewId());
                System.out.println("etContent1"+etContent.getId());
                Toast.makeText(v.getContext(),"text-id;"+etContent1.getId(),Toast.LENGTH_LONG).show();;
                layout.addView(etContent1);
    
                // 3.创建“+”和“-”按钮外围控件RelativeLayout
                RelativeLayout rlBtn = new RelativeLayout(student_signUp.this);
                RelativeLayout.LayoutParams rlParam = new RelativeLayout.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT);
    //          rlParam.setMargins(0, (int) (fDimRatio * 5), 0, 0);
                rlBtn.setPadding(0, (int) (fDimRatio * 5), 0, 0);
                rlBtn.setLayoutParams(rlParam);
    
                // 4.创建“+”按钮
                ImageButton btnAdd = new ImageButton(student_signUp.this);
                RelativeLayout.LayoutParams btnAddParam = new RelativeLayout.LayoutParams(
                        ViewGroup.LayoutParams.WRAP_CONTENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT);
                // 靠右放置
                btnAddParam.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                btnAdd.setLayoutParams(btnAddParam);
                // 设置属性
                btnAdd.setBackgroundResource(R.drawable.add);
    
                // 设置点击操作
                btnAdd.setOnClickListener(new View.OnClickListener() {
    
                    @Override
                    public void onClick(View v) {
                        addContent(v);
                    }
                });
                // 将“+”按钮放到RelativeLayout里
                rlBtn.addView(btnAdd);
                listIBTNAdd.add(iIndex, btnAdd);
    
                // 5.创建“-”按钮
                ImageButton btnDelete = new ImageButton(student_signUp.this);
                btnDelete.setBackgroundResource(R.drawable.delete);
                RelativeLayout.LayoutParams btnDeleteAddParam = new RelativeLayout.LayoutParams(
                        ViewGroup.LayoutParams.WRAP_CONTENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT);
                btnDeleteAddParam.setMargins(0, 0, (int) (fDimRatio * 5), 0);
                // “-”按钮放在“+”按钮左侧
                btnDeleteAddParam.addRule(RelativeLayout.LEFT_OF, btnIDIndex);
                btnDelete.setOnClickListener(new View.OnClickListener() {
    
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(v.getContext(),"该插件id;"+v.getId(),Toast.LENGTH_LONG).show();
                        deleteContent(v);
                    }
                });
                // 将“-”按钮放到RelativeLayout里
                rlBtn.addView(btnDelete, btnDeleteAddParam);
                listIBTNDel.add(iIndex, btnDelete);
    
                // 6.将RelativeLayout放到LinearLayout里
                layout.addView(rlBtn);
    
                // 7.将layout同它内部的所有控件加到最外围的llContentView容器里
                llContentView.addView(layout, iIndex);
    
                btnIDIndex++;
            }
        }
    
        /**
         * 删除一组控件
         * @param v 事件触发控件,其实就是触发删除事件对应的“-”按钮
         */
        private void deleteContent(View v) {
            if (v == null) {
                return;
            }
    
            // 判断第几个“-”按钮触发了事件
            int iIndex = -1;
            for (int i = 0; i < listIBTNDel.size(); i++) {
                if (listIBTNDel.get(i) == v) {
                    iIndex = i;
                    break;
                }
            }
            if (iIndex >= 0) {
                listIBTNAdd.remove(iIndex);
                listIBTNDel.remove(iIndex);
    
                // 从外围llContentView容器里删除第iIndex控件
                llContentView.removeViewAt(iIndex);
            }
        }
    
        /*@Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.activity_main, menu);
            return true;
        }  */
    
        //为每一个控件设置一个id
        private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
        public static int generateViewId() {
            for (; ; ) {
                final int result = sNextGeneratedId.get();
                // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
                int newValue = result + 1;
                if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
                if (sNextGeneratedId.compareAndSet(result, newValue)) {
                    return result;
                }
            }
        }
    
        public static int generateViewId(int a) {
    
                final int result = a;
                return result;
        }
    
    
    }
  • 相关阅读:
    C#的访问修饰符
    C#的数据类型
    iOS--小结系列八(继续)
    ios--小结系列八 控制器管理
    ios--小结系列八 程序启动原理
    ios--小结系列七 通知机制
    ios--小结系列六继续-UITableViewCell
    ios--小结系列六
    ios--小结系列五
    ios--小结系列四
  • 原文地址:https://www.cnblogs.com/837634902why/p/10876093.html
Copyright © 2011-2022 走看看