如何根据用户输入自动创建复选框

huangapple go评论108阅读模式
英文:

How to Create a Checkbox Automatically on user input

问题

在我的活动中,我有一个编辑文本和一个按钮。当用户输入后,点击按钮"添加"时,希望自动创建一个带有用户在编辑文本中输入的名称的待办事项清单。

如何在Java程序中根据用户输入创建复选框。

当我点击"添加"按钮时,列表没有更新(用户输入没有转换为复选框,也没有在底部显示名称)。

以下是我的XML代码:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".MainActivity">
  8. <EditText
  9. android:layout_width="match_parent"
  10. android:layout_height="wrap_content"
  11. android:hint="输入要添加到列表中的名称"
  12. android:id="@+id/ed_name"
  13. android:layout_marginTop="15dp"
  14. />
  15. <Button
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content"
  18. android:text="添加"
  19. android:id="@+id/btn_add"
  20. android:layout_below="@+id/ed_name"
  21. android:layout_centerHorizontal="true"
  22. android:layout_marginTop="15dp"
  23. />
  24. </RelativeLayout>

以下是我的Java代码:

  1. public class MainActivity extends AppCompatActivity {
  2. EditText uinput;
  3. Button btnadd;
  4. ScrollView sv;
  5. RelativeLayout ll;
  6. @Override
  7. protected void onCreate(Bundle savedInstanceState) {
  8. super.onCreate(savedInstanceState);
  9. setContentView(R.layout.activity_main);
  10. sv = new ScrollView(this);
  11. ll = new RelativeLayout(this);
  12. sv.addView(ll);
  13. uinput = findViewById(R.id.ed_name);
  14. btnadd = findViewById(R.id.btn_add);
  15. btnadd.setOnClickListener(new View.OnClickListener() {
  16. @Override
  17. public void onClick(View view) {
  18. String Name = uinput.getText().toString();
  19. CheckBox cb = new CheckBox(getApplicationContext());
  20. cb.setText(Name);
  21. ll.addView(cb);
  22. }
  23. });
  24. }
  25. }

请注意,上述代码在点击"添加"按钮时会创建一个复选框,其文本为用户输入的名称,并将其添加到RelativeLayout布局中。但要确保你已经在布局文件中正确地设置了EditText和Button的ID。

英文:

In My Activity i have a Edit Text and Button When the user enter the button " Add " the checklist want to create Automatically with the name of the user entered in the edit text

how to create a checkbox in java program based on the user input

如何根据用户输入自动创建复选框

when i click the Add Button the list was not updated ( the userinput was not converted into checkbox and it doesnot display the name in bottom )

Here is My XML Code

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  2. &lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  3. xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
  4. xmlns:tools=&quot;http://schemas.android.com/tools&quot;
  5. android:layout_width=&quot;match_parent&quot;
  6. android:layout_height=&quot;match_parent&quot;
  7. tools:context=&quot;.MainActivity&quot;&gt;
  8. &lt;EditText
  9. android:layout_width=&quot;match_parent&quot;
  10. android:layout_height=&quot;wrap_content&quot;
  11. android:hint=&quot;Enter Name to Add in the list&quot;
  12. android:id=&quot;@+id/ed_name&quot;
  13. android:layout_marginTop=&quot;15dp&quot;
  14. /&gt;
  15. &lt;Button
  16. android:layout_width=&quot;wrap_content&quot;
  17. android:layout_height=&quot;wrap_content&quot;
  18. android:text=&quot;Add&quot;
  19. android:id=&quot;@+id/btn_add&quot;
  20. android:layout_below=&quot;@+id/ed_name&quot;
  21. android:layout_centerHorizontal=&quot;true&quot;
  22. android:layout_marginTop=&quot;15dp&quot;
  23. /&gt;
  24. &lt;/RelativeLayout&gt;

here is My java code

  1. public class MainActivity extends AppCompatActivity {
  2. EditText uinput;
  3. Button btnadd;
  4. CheckBox cbname;
  5. ScrollView sv;
  6. RelativeLayout ll;
  7. @Override
  8. protected void onCreate(Bundle savedInstanceState) {
  9. super.onCreate(savedInstanceState);
  10. setContentView(R.layout.activity_main);
  11. sv = new ScrollView(this);
  12. ll = new RelativeLayout(this);
  13. // ll.setOrientation(LinearLayout.VERTICAL);
  14. sv.addView(ll);
  15. uinput = findViewById(R.id.ed_name);
  16. btnadd = findViewById(R.id.btn_add);
  17. btnadd.setOnClickListener(new View.OnClickListener() {
  18. @Override
  19. public void onClick(View view) {
  20. String Name = uinput.getText().toString();
  21. CheckBox cb = new CheckBox(getApplicationContext());
  22. cb.setText(Name);
  23. ll.addView(cb);
  24. }
  25. });
  26. }

答案1

得分: 2

你没有将 ScrollView 添加到你的布局中,所以 R.layout.activity_main 不知道你添加的视图。

因此,请将你布局的根 ViewGroup 充气(inflate),将 ScrollView 添加到其中,并根据你的根 ViewGroup 的类型设置约束/属性。

为根布局添加一个id

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:id="@+id/root"
  8. tools:context=".MainActivity">

ScrollView 内部的 RelativeLayout 替换为 LinearLayout,并将 ScrollView 添加到 btn_add 下方的属性参数 RelativeLayout.BELOW

  1. ScrollView sv = new ScrollView(this);
  2. LinearLayout ll = new LinearLayout(this);
  3. ll.setOrientation(LinearLayout.VERTICAL);
  4. sv.addView(ll);
  5. EditText uinput = findViewById(R.id.ed_name);
  6. Button btnadd = findViewById(R.id.btn_add);
  7. RelativeLayout rootLayout = findViewById(R.id.root);
  8. // 设置 sv 的约束/属性
  9. RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
  10. params.addRule(RelativeLayout.BELOW, btnadd.getId());
  11. rootLayout.addView(sv, params);
  12. btnadd.setOnClickListener(new View.OnClickListener() {
  13. @Override
  14. public void onClick(View view) {
  15. String Name = uinput.getText().toString();
  16. CheckBox cb = new CheckBox(getApplicationContext());
  17. cb.setText(Name);
  18. ll.addView(cb);
  19. }
  20. });
英文:

You didn't add the ScrollView to your layout, So the R.layout.activity_main know nothing about your added views.

So, inflate your root ViewGroup of your layout, add the ScrollView to it and set constraints/attributes according to the type of your root ViewGroup.

Add an id to the root layout

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  2. &lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  3. xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
  4. xmlns:tools=&quot;http://schemas.android.com/tools&quot;
  5. android:layout_width=&quot;match_parent&quot;
  6. android:layout_height=&quot;match_parent&quot;
  7. android:id=&quot;@+id/root&quot;
  8. tools:context=&quot;.MainActivity&quot;&gt;

Replace the ScrollView inner RelativeLayout with a LinearLayout and add the ScrollView with an attribute param RelativeLayout.BELOW to be at the bottom of the btn_add

  1. ScrollView sv = new ScrollView(this);
  2. LinearLayout ll = new LinearLayout(this);
  3. ll.setOrientation(LinearLayout.VERTICAL);
  4. // ll.setOrientation(LinearLayout.VERTICAL);
  5. sv.addView(ll);
  6. EditText uinput = findViewById(R.id.ed_name);
  7. Button btnadd = findViewById(R.id.btn_add);
  8. RelativeLayout rootLayout = findViewById(R.id.root);
  9. // Set constraints/attributes to the sv
  10. RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
  11. params.addRule(RelativeLayout.BELOW, btnadd.getId());
  12. rootLayout.addView(sv, params);
  13. btnadd.setOnClickListener(new View.OnClickListener() {
  14. @Override
  15. public void onClick(View view) {
  16. String Name = uinput.getText().toString();
  17. CheckBox cb = new CheckBox(getApplicationContext());
  18. cb.setText(Name);
  19. ll.addView(cb);
  20. }
  21. });

答案2

得分: 2

以下是已翻译的内容:

我建议您将activity_main.xml布局文件中的所有静态内容都添加为:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".MainActivity2">
  8. <EditText
  9. android:id="@+id/ed_name"
  10. android:layout_width="0dp"
  11. android:layout_height="wrap_content"
  12. android:layout_marginStart="8dp"
  13. android:layout_marginTop="8dp"
  14. android:layout_marginEnd="8dp"
  15. android:ems="10"
  16. android:hint="在列表中输入要添加的名称"
  17. android:inputType="textPersonName"
  18. app:layout_constraintEnd_toEndOf="parent"
  19. app:layout_constraintStart_toStartOf="parent"
  20. app:layout_constraintTop_toTopOf="parent" />
  21. <Button
  22. android:id="@+id/btn_add"
  23. android:layout_width="wrap_content"
  24. android:layout_height="wrap_content"
  25. android:layout_marginStart="8dp"
  26. android:layout_marginTop="8dp"
  27. android:layout_marginEnd="8dp"
  28. android:text="添加"
  29. app:layout_constraintEnd_toEndOf="parent"
  30. app:layout_constraintStart_toStartOf="parent"
  31. app:layout_constraintTop_toBottomOf="@+id/ed_name" />
  32. <ScrollView
  33. android:id="@+id/scrollview"
  34. android:layout_width="409dp"
  35. android:layout_height="612dp"
  36. android:layout_marginTop="8dp"
  37. app:layout_constraintBottom_toBottomOf="parent"
  38. app:layout_constraintEnd_toEndOf="parent"
  39. app:layout_constraintStart_toStartOf="parent"
  40. app:layout_constraintTop_toBottomOf="@+id/btn_add">
  41. <LinearLayout
  42. android:id="@+id/linear_layout"
  43. android:layout_width="match_parent"
  44. android:layout_height="wrap_content"
  45. android:orientation="vertical" />
  46. </ScrollView>
  47. </androidx.constraintlayout.widget.ConstraintLayout>

现在,Checkbox 是我们将以编程方式生成并将其添加到ScrollView的唯一子项的元素。以下是执行此操作的MainActivity示例代码:

  1. public class MainActivity extends AppCompatActivity {
  2. // 声明所需的视图变量
  3. private EditText uInput;
  4. private LinearLayout linearLayout;
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.activity_main2);
  9. // 初始化所需的视图变量
  10. uInput = findViewById(R.id.ed_name);
  11. linearLayout = findViewById(R.id.linear_layout);
  12. Button btnAdd = findViewById(R.id.btn_add);
  13. btnAdd.setOnClickListener(new View.OnClickListener() {
  14. @Override
  15. public void onClick(View v) {
  16. String name = uInput.getText().toString();
  17. if (!name.isEmpty()) {
  18. CheckBox checkBox = new CheckBox(MainActivity.this);
  19. checkBox.setText(name);
  20. linearLayout.addView(checkBox);
  21. } else
  22. Toast.makeText(MainActivity.this, "名称不能为空!", Toast.LENGTH_LONG).show();
  23. }
  24. });
  25. }
  26. }

希望这对您有所帮助!

英文:

I suggest you to add everything that is static in the activity_main.xml layout file as :

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  2. &lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  3. xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
  4. xmlns:tools=&quot;http://schemas.android.com/tools&quot;
  5. android:layout_width=&quot;match_parent&quot;
  6. android:layout_height=&quot;match_parent&quot;
  7. tools:context=&quot;.MainActivity2&quot;&gt;
  8. &lt;EditText
  9. android:id=&quot;@+id/ed_name&quot;
  10. android:layout_width=&quot;0dp&quot;
  11. android:layout_height=&quot;wrap_content&quot;
  12. android:layout_marginStart=&quot;8dp&quot;
  13. android:layout_marginTop=&quot;8dp&quot;
  14. android:layout_marginEnd=&quot;8dp&quot;
  15. android:ems=&quot;10&quot;
  16. android:hint=&quot;Enter name to add in the list&quot;
  17. android:inputType=&quot;textPersonName&quot;
  18. app:layout_constraintEnd_toEndOf=&quot;parent&quot;
  19. app:layout_constraintStart_toStartOf=&quot;parent&quot;
  20. app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
  21. &lt;Button
  22. android:id=&quot;@+id/btn_add&quot;
  23. android:layout_width=&quot;wrap_content&quot;
  24. android:layout_height=&quot;wrap_content&quot;
  25. android:layout_marginStart=&quot;8dp&quot;
  26. android:layout_marginTop=&quot;8dp&quot;
  27. android:layout_marginEnd=&quot;8dp&quot;
  28. android:text=&quot;Add&quot;
  29. app:layout_constraintEnd_toEndOf=&quot;parent&quot;
  30. app:layout_constraintStart_toStartOf=&quot;parent&quot;
  31. app:layout_constraintTop_toBottomOf=&quot;@+id/ed_name&quot; /&gt;
  32. &lt;ScrollView
  33. android:id=&quot;@+id/scrollview&quot;
  34. android:layout_width=&quot;409dp&quot;
  35. android:layout_height=&quot;612dp&quot;
  36. android:layout_marginTop=&quot;8dp&quot;
  37. app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
  38. app:layout_constraintEnd_toEndOf=&quot;parent&quot;
  39. app:layout_constraintStart_toStartOf=&quot;parent&quot;
  40. app:layout_constraintTop_toBottomOf=&quot;@+id/btn_add&quot;&gt;
  41. &lt;LinearLayout
  42. android:id=&quot;@+id/linear_layout&quot;
  43. android:layout_width=&quot;match_parent&quot;
  44. android:layout_height=&quot;wrap_content&quot;
  45. android:orientation=&quot;vertical&quot; /&gt;
  46. &lt;/ScrollView&gt;
  47. &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

Now, Checkbox are the one which we will generate programmatically and add them to the LinearLayout which is the only child of ScrollView(it supports single child only). A sample code for MainActivity doing that is here :

  1. public class MainActivity extends AppCompatActivity {
  2. // declare the required views variable
  3. private EditText uInput;
  4. private LinearLayout linearLayout;
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.activity_main2);
  9. // initializes required view variable
  10. uInput = findViewById(R.id.ed_name);
  11. linearLayout = findViewById(R.id.linear_layout);
  12. Button btnAdd = findViewById(R.id.btn_add);
  13. btnAdd.setOnClickListener(new View.OnClickListener() {
  14. @Override
  15. public void onClick(View v) {
  16. String name = uInput.getText().toString();
  17. if (!name.isEmpty()) {
  18. CheckBox checkBox = new CheckBox(MainActivity.this);
  19. checkBox.setText(name);
  20. linearLayout.addView(checkBox);
  21. } else
  22. Toast.makeText(MainActivity.this, &quot;The name cannot be empty!&quot;, Toast.LENGTH_LONG).show();
  23. }
  24. });
  25. }
  26. }

如何根据用户输入自动创建复选框

It also checks whether or not the text in EditText is empty and if it is generates a suitable Toast message to not permit the user create a empty Checkbox. Hope, this helps!

答案3

得分: 1

这段代码将在你的视图中添加一个复选框。

由于你没有提供任何代码,我会给你一些思路,让你可以自己制定策略或风格。

  1. ScrollView sv = new ScrollView(this);
  2. LinearLayout ll = new LinearLayout(this);
  3. ll.setOrientation(LinearLayout.VERTICAL);
  4. sv.addView(ll);
  5. Button b = new Button(this);
  6. b.setOnClickListener(new OnClickListener() {
  7. @Override
  8. public void onClick(View v) {
  9. CheckBox cb = new CheckBox(getApplicationContext());
  10. cb.setText(myTextBoxText.getText());
  11. ll.addView(cb);
  12. }
  13. });
英文:

This code will add a checkbox in your view

since you don't have any provided code, ill give you an idea to come up on your own strategies or style

  1. ScrollView sv = new ScrollView(this);
  2. LinearLayout ll = new LinearLayout(this);
  3. ll.setOrientation(LinearLayout.VERTICAL);
  4. sv.addView(ll);
  5. Button b = new Button(this);
  6. b.setOnClickListener(new OnClickListener() {
  7. @Override
  8. public void onClick(View v) {
  9. CheckBox cb = new CheckBox(getApplicationContext());
  10. cb.setText(myTextBoxText.getText());
  11. ll.addView(cb);
  12. }
  13. });

huangapple
  • 本文由 发表于 2020年8月2日 09:42:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/63211602.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定