Android在LinearLayout中动态添加EditText

2023-12-30

我试图使用按钮在现有的 editText 字段下动态添加 editText 字段。目前按下按钮没有任何作用。这是我的代码:

XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
android:background="#d4cfcd">
tools:context="com.zdowning.decisionmaker.MainActivity">


<LinearLayout
    android:id="@+id/linearLayoutMain"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:gravity="center"
        android:hint="Enter Your Question Here"
        android:layout_marginBottom="20dp"
        android:layout_marginTop="20dp"/>

    <LinearLayout
        android:id="@+id/linearLayoutDecisions"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

    <EditText
        android:id="@+id/editText3"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:gravity="center"
        android:hint="Enter Answer #2" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:gravity="center"
        android:hint="Enter Answer #1" />

    <EditText
        android:id="@+id/editText4"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:gravity="center"
        android:hint="Enter Answer #3" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center">

        <Button
            android:id="@+id/add_button"
            style="?android:attr/borderlessButtonStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            android:text="+"
            android:textColor="@android:color/background_light"
            android:textSize="18sp"/>

        <View
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1" />

        <Button
            android:id="@+id/remove_button"
            style="?android:attr/borderlessButtonStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            android:text="-"
            android:textColor="@android:color/background_light"
            android:textSize="18sp"
            android:layout_margin="10dp"/>
    </LinearLayout>

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
    <Button
        android:id="@+id/button"
        style="?android:attr/borderlessButtonStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        android:text="DECIDE!"
        android:textColor="@android:color/background_light"
        android:textSize="18sp"
        android:layout_centerInParent="true" />
    </RelativeLayout>

    <TextView
        android:id="@+id/textView7"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_margin="20dp"
        android:textColor="@android:color/black"
        android:textSize="36sp" />

</LinearLayout>

Java:

package com.zdowning.decisionmaker;

import android.graphics.Typeface;
import android.support.v7.app.AppCompatActivity;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ScrollView;

import java.lang.*;

import static com.zdowning.decisionmaker.R.layout.activity_main;

public class MainActivity extends AppCompatActivity {
public int numberOfLines = 3;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    setContentView(activity_main);
    final Button button = (Button) findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
             getAnswer();
        }
    });

    final Button Add_button = (Button) findViewById(R.id.add_button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Add_Line();
        }
    });
}


public void Add_Line() {
    LinearLayout ll = (LinearLayout) 
    findViewById(R.id.linearLayoutDecisions);

    // add edittext
    EditText et = new EditText(this);
    et.setId(numberOfLines + 1);
    ll.addView(et);
    numberOfLines++;
}


public void getAnswer() {
    String[] options = new String[3];

    EditText text = (EditText)findViewById(R.id.editText2);
    options[0] = text.getText().toString();

    text = (EditText)findViewById(R.id.editText3);
    options[1] = text.getText().toString();

    text = (EditText)findViewById(R.id.editText4);
    options[2] = text.getText().toString();

    int number = (int)(Math.random() * 3);
    String answer = options[number];

    TextView answerBox = (TextView)findViewById(R.id.textView7);
    answerBox.setText(answer);
}
}

任何建议都会有很大的帮助,因为我是 android studio 的新手。编写上面的代码花了我几个小时。


改变你的Add_line()方法:

 public void Add_Line() {
    LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayoutDecisions);
    // add edittext
    EditText et = new EditText(this);
    LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    et.setLayoutParams(p);
    et.setText("Text");
    et.setId(numberOfLines + 1);
    ll.addView(et);
    numberOfLines++;
}

add the LayoutParams to the Edittext

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android在LinearLayout中动态添加EditText 的相关文章

随机推荐

  • SendMessage - 发送区分大小写的按键

    我正在尝试使用 WinAPI 创建一个工作函数 以便在其他应用程序 例如记事本 中逐键写入给定的文本SendMessage功能 我有这样的代码 SendMessage handle WM CHAR 0x41 0 SendMessage ha
  • React Native foreach 循环

    我正在 React Native 中开发一个小应用程序 我正在寻找类似 foreach 函数的东西 我只是找不到 foreach 循环 不在 StackOverflow 上 甚至不在docs https facebook github io
  • OAuth2 中 OTP/2FA 支持的推荐设计

    我正在尝试将 OTP 2FA 支持添加到 OAuth2 中 但是经过大量阅读RFC6749 https www rfc editor org rfc rfc6749 目前还不清楚如何在不违反规范的情况下干净地添加 OTP 2FA 虽然 OT
  • iPhone 的缓存/离线地图?

    我想在我的应用程序中使用地图 以便尽可能减少流量 完美的解决方案是缓存地图切片 我知道谷歌地图 许可证 是不可能的 我查看了 OpenStreetMaps 这似乎是一个很好的解决方案 下一个 SDK 我发现的唯一一个来自 CloudMade
  • 使用 Rails 中的模型数据填充选择

    我觉得有必要为问这样一个简单的问题而道歉 但我对 Rails 指南越来越感到沮丧 我确信他们回答了我的问题 但他们没有提供足够的背景让我真正理解如何apply他们给我什么 谷歌也没有多大帮助 尽管我可能只是搜索了错误的术语 短语 鉴于该免责
  • 可以 Boost Program_options 分隔逗号分隔的参数值

    如果我的命令行是 gt prog mylist a b c Boost 的program options 可以设置为查看三个不同的参数值吗mylist争论 我已将program options配置为 namespace po boost p
  • 显示来自用户输入的部分数组值匹配

    我有一个带有一堆值的 jQuery 数组 我希望用户能够在输入中键入内容 并与屏幕上显示的数组中的任何内容进行部分匹配 到目前为止 我已经知道何时有完整的匹配 并且我可以将其打印到页面上 但我不确定如何进行部分匹配 这是我到目前为止所拥有的
  • 无法启动服务并出现 net.tcp 绑定错误 10049

    我在使用 net tcp 端点启动 WCF 服务时遇到问题 我收到 10049 错误 My app config
  • 从单独的控制器 angularjs 中检索成功后的数据

    我正在编写一个简单的服务 用于上传文件并将其发布到 Spring 控制器 控制器操作这些数据并以 JSON 形式返回多个对象 我正在使用以下 Angular 服务 myApp service fileUpload http function
  • SQL中如何计算运行总计

    我的数据集采用给定的格式 这是每月级别的数据以及每个月的工资 我需要计算每个月末的累计工资 我怎样才能做到这一点 Account Month Salary Running Total a 1 586 586 a 2 928 1514 a 3
  • HTML 时间标签 - 正确的日期格式

    我想使用正确的格式和标准将时间标签放入我的 html 文档中 这是正确的方法吗
  • 通过 php 函数从 WordPress 短代码中删除空

    标签

    寻找 php 函数 非 jQuery 或 wpaautop 修改 方法来删除 p p 从 WordPress 内部 我尝试了这个 但它不起作用 function cleanup shortcode fix content array arr
  • 在 widget 树中使用 const 会提高性能吗?

    创建widget树时 会插入const在静态小部件提高性能之前 ie child const Text This is some text vs child Text This is some text 我知道 使用 Dart 2 cons
  • 使用java servlet在浏览器中显示Pdf

    我的应用程序中有 pdf 文件 我需要在浏览器中显示pdf 我正在将文件作为 fileInputStream 读取 我需要在我的应用程序中的浏览器中显示 pdf 但我没有 pdf 路径 我有文件流 请给我一些建议和例子 我使用ajax来显示
  • 如何在 Ruby 中解析 url 来获取主域?

    我希望能够使用 Ruby 解析任何 URL 以获取域的主要部分 而无需www 只是example com 请注意没有算法方法可以找到可以为特定顶级域注册域的最高级别 每个注册机构的政策有所不同 唯一的方法是创建所有顶级域以及可以注册域的级别
  • ABP 中的区域设置日期时间

    我在弹出模式中有一个 DateTime 字段 如下所示 它应该只显示时间部分 HTML div class input group date div
  • 是什么导致 EventStore 这么容易抛出 ConcurrencyException?

    Using JOliver活动商店 http github com joliver EventStore3 0 并且刚刚开始使用简单的示例 我有一个使用 NServiceBus 的简单发布 订阅 CQRS 实现 客户端在总线上发送命令 域服
  • 使用我自己的语料库而不是 movie_reviews 语料库在 NLTK 中进行分类

    我使用以下代码并得到它的形式在 NLTK Python 中使用电影评论语料库进行分类 https stackoverflow com questions 21107075 classification using movie review
  • 离子电容器删除 Android 构建的 REQUEST_INSTALL_PACKAGES 权限

    我们正在使用命令创建应用程序 ionic build configuration development ionic capacitor copy android no build npx cap open android 现在最新的 An
  • Android在LinearLayout中动态添加EditText

    我试图使用按钮在现有的 editText 字段下动态添加 editText 字段 目前按下按钮没有任何作用 这是我的代码 XML