Android通过代码动态生成textView、EditText、ImageButton,实现人性化新建旅游行程需求

一、项目需求

在新建旅游行程的过程中因为旅游天数是不确定的,所以我们需要提供可选方案给客户。这里我们可以根据需要点击加号按钮生成对应的天数,根据需要删除对应的天数。演示图如下:

20190326003044349.gif

二、xml文件如下:



 
    
 
        
 
        
 
            
 
                
 
                
 
                
 
                
 
            
 
        
 
        
 
            

三、核心思想:

利用linkedList数据结构,每次都把生成的加号按钮、删除按钮和textView控件放进linkedList中,用一个变量index来对应每一个放进去的控件,方便找到点击了哪个按钮然后再写相应逻辑即可。较难之处在于点击了中间的加号按钮之后textView的文本也要相应变化。这里我们可以先判断一下点击的按钮下面是否已经有了按钮,有的话我们可以将index设为linkedList的size即可。这样在点击加号按钮时就不用注意textView的文本了。而点击删除按钮的话我们可以根据linkedList的size来重新赋值即可改变textView的文本了。具体代码如下:

package top.longsh1z.yiyou.ui;
 
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
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.TextView;
import android.widget.Toast;
 
import java.lang.reflect.Field;
import java.util.LinkedList;
 
import top.longsh1z.yiyou.R;
 
public class GuideRouteActivity extends AppCompatActivity {
 
    private LinearLayout linearLayout;
    private TextView tv_day1;
    private EditText et_day1;
    private ImageButton btn_create;
    private Button btn_save;
    // “+”按钮控件List
    private LinkedList listIBTNAdd;
    //textView控件List
    private LinkedList listTextView;
    // “+”按钮ID索引
    private int btnIDIndex = 1000;
    //edittext控件List
    private LinkedList listEditText;
    // “-”按钮控件List
    private LinkedList listIBTNDel;
    private int iETContentWidth;   // EditText控件宽度
    private int iETContentHeight;  // EditText控件高度
    private int iTVContentWidth;   //textView控件高度
    private int iTVContentHeight;   //textView控件高度
    private int iIBContentWidth;   //imageButton控件边长
    private float fDimRatio = 1.0f; // 尺寸比例(实际尺寸/xml文件里尺寸)
    private Message msg;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide_route);
 
        findAllViews();
 
        listIBTNAdd = new LinkedList();
        listIBTNDel = new LinkedList();
        listTextView = new LinkedList();
        listEditText = new LinkedList();
 
        btn_create.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
 
                iETContentWidth = et_day1.getWidth();   // EditText控件宽度
                iETContentHeight = et_day1.getHeight();  // EditText控件高度
                iTVContentWidth = tv_day1.getWidth();   //textView控件高度
                iTVContentHeight = tv_day1.getHeight();   //textView控件高度
                iIBContentWidth = btn_create.getWidth();
                fDimRatio = iETContentWidth / 150;
 
                addContent(v);
            }
        });
        listIBTNAdd.add(btn_create);
        listIBTNDel.add(null);
        listEditText.add(et_day1);
 
        final Handler handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                if (msg.what == 1) {
                    String content = "";
                    for (int i = 0; i < listEditText.size(); i++) {
                        if (listEditText.get(i).getText().toString() == null ||
                                listEditText.get(i).getText().toString().equals("")) {
                            Toast.makeText(GuideRouteActivity.this, "请填好相关行程!", Toast.LENGTH_SHORT).show();
                            break;
                        } else {
                            content += listEditText.get(i).getText().toString() + "_";
                        }
                    }
                    if (content != null || !content.equals("")) {
                        String[] contens = content.split("_");
                        if (contens.length == listEditText.size())
                            Toast.makeText(GuideRouteActivity.this, content, Toast.LENGTH_LONG).show();
                    }
                }
                return true;
            }
        });
 
        btn_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Thread thread = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        handler.sendEmptyMessage(1);
                    }
                });
                thread.start();
            }
        });
    }
 
    @SuppressWarnings("ResourceType")
    private void addContent(View v) {
        if (v == null)
            return;
 
        int index = -1;
        for (int i = 0; i < listIBTNAdd.size(); i++) {
            if (listIBTNAdd.get(i) == v) {
                index = i;
                break;
            }
        }
 
        if (index >= 0) {
            //判断点击按钮的下方是否已经存在按钮,是则将按钮从最底部插入
            try {
                if (listIBTNAdd.get(index + 1) != null) {
                    index = listIBTNAdd.size();
                }
            } catch (Exception e) {
                index += 1;
            }
 
 
            //开始添加控件
 
            //创建外围linearlayout控件
            LinearLayout layout = new LinearLayout(this);
            LinearLayout.LayoutParams ILayoutlayoutParams = new LinearLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT
            );
            ILayoutlayoutParams.setMargins(0, (int) (fDimRatio * 15), 0, 0);
            layout.setGravity(Gravity.CENTER);
            layout.setLayoutParams(ILayoutlayoutParams);
 
            //创建内部textView控件
            TextView tvContent = new TextView(this);
            LinearLayout.LayoutParams tvParam = new LinearLayout.LayoutParams(
                    iTVContentWidth, iTVContentHeight
            );
            tvContent.setText("Day " + (index + 1));
            layout.addView(tvContent);
            listTextView.add(tvContent);
 
            //创建内部edittext控件
            EditText etContent = new EditText(this);
            LinearLayout.LayoutParams etParam = new LinearLayout.LayoutParams(
                    iETContentWidth, iETContentHeight
            );
            try {
                Field field = TextView.class.getDeclaredField("mCursorDrawableRes");
                field.setAccessible(true);
                field.set(etContent,R.drawable.et_cursor_style);
            }catch (Exception e){
 
            }
            etParam.setMargins((int) (fDimRatio * 20), 0, 0, 0);
            etContent.setBackgroundResource(R.drawable.edit_register_bg);
            etContent.setLayoutParams(etParam);
            layout.addView(etContent);
            listEditText.add(etContent);
 
            //创建“+”按钮
            ImageButton btnAdd = new ImageButton(this);
            LinearLayout.LayoutParams btnAddParam = new LinearLayout.LayoutParams(
                    iIBContentWidth,
                    iIBContentWidth
            );
            btnAddParam.setMargins((int) (fDimRatio * 10), 0, 0, 0);
            btnAdd.setLayoutParams(btnAddParam);
            btnAdd.setBackgroundResource(R.drawable.ibcreatestyle);
            btnAdd.setId(btnIDIndex);
            btnAdd.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    addContent(v);
                }
            });
            layout.addView(btnAdd);
            listIBTNAdd.add(index, btnAdd);
 
            //创建“-”按钮
            ImageButton btnDelete = new ImageButton(this);
            LinearLayout.LayoutParams btnDeleteParam = new LinearLayout.LayoutParams(
                    iIBContentWidth,
                    iIBContentWidth
            );
            btnDeleteParam.setMargins((int) (fDimRatio * 10), 0, 0, 0);
            btnDelete.setLayoutParams(btnDeleteParam);
            btnDelete.setBackgroundResource(R.drawable.ibdeletestyle);
            btnDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    deleteContent(v);
                }
            });
            layout.addView(btnDelete);
            listIBTNDel.add(index, btnDelete);
 
            //将layout同它内部的所有控件都放在最外围的linearLayout容器里
            linearLayout.addView(layout);
 
            btnIDIndex++;
        }
    }
 
    private void deleteContent(View v) {
        if (v == null)
            return;
 
        //判断第几个“-”按钮触发了事件
        int index = -1;
        for (int i = 0; i < listIBTNDel.size(); i++) {
            if (listIBTNDel.get(i) == v) {
                index = i;
                break;
            }
        }
 
        if (index >= 0) {
            listIBTNAdd.remove(index);
            listIBTNDel.remove(index);
            listTextView.remove(index - 1);
            listEditText.remove(index);
 
            linearLayout.removeViewAt(index);
        }
 
        for (int i = 0; i < listTextView.size(); i++) {
            int num = 2 + i;
            listTextView.get(i).setText("Day " + num);
        }
    }
 
    private void findAllViews() {
        linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
        tv_day1 = (TextView) findViewById(R.id.tv_day1);
        et_day1 = (EditText) findViewById(R.id.et_day1);
        btn_create = (ImageButton) findViewById(R.id.btn_create);
        btn_save = (Button) findViewById(R.id.btn_saveroute);
    }
}

你可能感兴趣的:(Android通过代码动态生成textView、EditText、ImageButton,实现人性化新建旅游行程需求)