Android 中通过Java代码动态生成EditText视图,并循环遍历EditText取出遍历的ID和文本框的值

  • Post author:
  • Post category:java


博主前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住也分享一下给大家,

👉

点击跳转到网站

首先通过java代码实现界面布局,之后点击提交按钮,获取到动态生成EditText视图中用户输入的值。

具体实现如下:

现在xml文件中设置根布局文件id

activity_generate.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".GenerateActivity"
    android:id="@+id/linear"
    android:orientation="vertical"
    >
</LinearLayout>

之后在Activity中使用java代码生成布局视图,和获取值的功能

public class GenerateActivity extends AppCompatActivity {
    //整个布局的根布局
    private LinearLayout linear;
    //声明一个EditText类型的list集合
    private ArrayList<EditText> list = new ArrayList<>();
    private HashMap<Integer, String> hashMap = new HashMap<>();
    private static final String TAG = "GenerateActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_generate);
        linear = findViewById(R.id.linear);
        for (int i = 0; i < 5; i++) {
            int controlID = 450 * 100 + i;
            String checkName = "数据" + i+":";
            hashMap.put(controlID, "CheckID");
            CreateTextView(controlID, checkName);
        }
        CreateButton();

    }


    private void CreateTextView(int controlID, String checkName) {
        //声明一个线性布局管理器
        LinearLayout linear1 = new LinearLayout(this);
        //设置这个线性布局管理器的宽度和高度
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        linear1.setLayoutParams(params);
        //设置线性布局管理器的布局方式 为水平布局
        linear1.setGravity(LinearLayout.HORIZONTAL);

        TextView textView = new TextView(this);
        LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1.0f);
        textView.setLayoutParams(params1);
        textView.setText(checkName);
        textView.setTextSize(20);
        textView.setGravity(CENTER | RIGHT);
        linear1.addView(textView);

        EditText editText = new EditText(this);
        LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 3.0f);
        editText.setLayoutParams(params2);
        editText.setId(controlID);
        list.add(editText);
        linear1.addView(editText);

        linear.addView(linear1);

    }
    private void CreateButton() {
        Button button = new Button(this);
        button.setText("提交");
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //提交数据
                SubmitData();
            }
        });
        linear.addView(button);

    }


    private void SubmitData() {
        for (int i = 0; i < list.size(); i++) {
        	//依次取出输入框中的值
            String value = list.get(i).getText().toString();
            int id = list.get(i).getId();
            String CheckID = hashMap.get(id);
            Log.e(TAG, "SubmitData: " + value);
        }
    }
}

效果图如下:

在这里插入图片描述

运行之后就可以获取打印日志:

08-07 16:31:42.996 3236-3236/com.example.daydemo E/GenerateActivity: SubmitData: 12
08-07 16:31:42.996 3236-3236/com.example.daydemo E/GenerateActivity: SubmitData: 13
08-07 16:31:42.996 3236-3236/com.example.daydemo E/GenerateActivity: SubmitData: 14
08-07 16:31:42.996 3236-3236/com.example.daydemo E/GenerateActivity: SubmitData: 15
08-07 16:31:42.996 3236-3236/com.example.daydemo E/GenerateActivity: SubmitData: 16

已经把我们输入的值打印了出来,说明已经获取到了其中的值。



版权声明:本文为lu202032原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。