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

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

How to Create a Checkbox Automatically on user input

问题

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

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

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

以下是我的XML代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".MainActivity">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="输入要添加到列表中的名称"
        android:id="@+id/ed_name"
        android:layout_marginTop="15dp"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="添加"
        android:id="@+id/btn_add"
        android:layout_below="@+id/ed_name"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="15dp"
        />

</RelativeLayout>

以下是我的Java代码:

public class MainActivity extends AppCompatActivity {

    EditText uinput;
    Button btnadd;
    ScrollView sv;
    RelativeLayout ll;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        sv = new ScrollView(this);
        ll = new RelativeLayout(this);
        sv.addView(ll);

        uinput = findViewById(R.id.ed_name);
        btnadd = findViewById(R.id.btn_add);
        btnadd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String Name = uinput.getText().toString();
                CheckBox cb = new CheckBox(getApplicationContext());
                cb.setText(Name);
                ll.addView(cb);
            }
        });
    }
}

请注意,上述代码在点击"添加"按钮时会创建一个复选框,其文本为用户输入的名称,并将其添加到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

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    tools:context=&quot;.MainActivity&quot;&gt;

  &lt;EditText
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;wrap_content&quot;
      android:hint=&quot;Enter Name to Add in the list&quot;
      android:id=&quot;@+id/ed_name&quot;
      android:layout_marginTop=&quot;15dp&quot;
      /&gt;
    &lt;Button
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:text=&quot;Add&quot;
        android:id=&quot;@+id/btn_add&quot;
        android:layout_below=&quot;@+id/ed_name&quot;
        android:layout_centerHorizontal=&quot;true&quot;
        android:layout_marginTop=&quot;15dp&quot;
        /&gt;
    

&lt;/RelativeLayout&gt;

here is My java code

public class MainActivity extends AppCompatActivity {

EditText uinput;
Button btnadd;
CheckBox cbname;
ScrollView sv;
RelativeLayout ll;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    sv = new ScrollView(this);
    ll = new RelativeLayout(this);
   // ll.setOrientation(LinearLayout.VERTICAL);
    sv.addView(ll);

    uinput = findViewById(R.id.ed_name);
    btnadd = findViewById(R.id.btn_add);
    btnadd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            String Name = uinput.getText().toString();
            CheckBox cb = new CheckBox(getApplicationContext());
            cb.setText(Name);
            ll.addView(cb);

        }
    });
}

答案1

得分: 2

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

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

为根布局添加一个id

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/root"
    tools:context=".MainActivity">

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

ScrollView sv = new ScrollView(this);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
sv.addView(ll);

EditText uinput = findViewById(R.id.ed_name);
Button btnadd = findViewById(R.id.btn_add);

RelativeLayout rootLayout = findViewById(R.id.root);

// 设置 sv 的约束/属性
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, btnadd.getId());
rootLayout.addView(sv, params);

btnadd.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

        String Name = uinput.getText().toString();
        CheckBox cb = new CheckBox(getApplicationContext());
        cb.setText(Name);
        ll.addView(cb);

    }
});
英文:

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

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    android:id=&quot;@+id/root&quot;
    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

ScrollView sv = new ScrollView(this);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
// ll.setOrientation(LinearLayout.VERTICAL);
sv.addView(ll);

EditText uinput = findViewById(R.id.ed_name);
Button btnadd = findViewById(R.id.btn_add);

RelativeLayout rootLayout = findViewById(R.id.root);

// Set constraints/attributes to the sv
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, btnadd.getId());
rootLayout.addView(sv, params);

btnadd.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

        String Name = uinput.getText().toString();
        CheckBox cb = new CheckBox(getApplicationContext());
        cb.setText(Name);
        ll.addView(cb);

    }
});

答案2

得分: 2

以下是已翻译的内容:

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

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
tools:context=".MainActivity2">

<EditText
    android:id="@+id/ed_name"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:ems="10"
    android:hint="在列表中输入要添加的名称"
    android:inputType="textPersonName"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<Button
    android:id="@+id/btn_add"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:text="添加"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/ed_name" />

<ScrollView
    android:id="@+id/scrollview"
    android:layout_width="409dp"
    android:layout_height="612dp"
    android:layout_marginTop="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/btn_add">

    <LinearLayout
        android:id="@+id/linear_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" />
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>

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

public class MainActivity extends AppCompatActivity {

    // 声明所需的视图变量
    private EditText uInput;
    private LinearLayout linearLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        // 初始化所需的视图变量
        uInput = findViewById(R.id.ed_name);

        linearLayout = findViewById(R.id.linear_layout);

        Button btnAdd = findViewById(R.id.btn_add);
        btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = uInput.getText().toString();
                if (!name.isEmpty()) {
                    CheckBox checkBox = new CheckBox(MainActivity.this);
                    checkBox.setText(name);
                    linearLayout.addView(checkBox);
                } else
                    Toast.makeText(MainActivity.this, "名称不能为空!", Toast.LENGTH_LONG).show();
            }
        });

    }
}

希望这对您有所帮助!

英文:

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

    &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
    &lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    tools:context=&quot;.MainActivity2&quot;&gt;

    &lt;EditText
        android:id=&quot;@+id/ed_name&quot;
        android:layout_width=&quot;0dp&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_marginStart=&quot;8dp&quot;
        android:layout_marginTop=&quot;8dp&quot;
        android:layout_marginEnd=&quot;8dp&quot;
        android:ems=&quot;10&quot;
        android:hint=&quot;Enter name to add in the list&quot;
        android:inputType=&quot;textPersonName&quot;
        app:layout_constraintEnd_toEndOf=&quot;parent&quot;
        app:layout_constraintStart_toStartOf=&quot;parent&quot;
        app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;

    &lt;Button
        android:id=&quot;@+id/btn_add&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_marginStart=&quot;8dp&quot;
        android:layout_marginTop=&quot;8dp&quot;
        android:layout_marginEnd=&quot;8dp&quot;
        android:text=&quot;Add&quot;
        app:layout_constraintEnd_toEndOf=&quot;parent&quot;
        app:layout_constraintStart_toStartOf=&quot;parent&quot;
        app:layout_constraintTop_toBottomOf=&quot;@+id/ed_name&quot; /&gt;

    &lt;ScrollView
        android:id=&quot;@+id/scrollview&quot;
        android:layout_width=&quot;409dp&quot;
        android:layout_height=&quot;612dp&quot;
        android:layout_marginTop=&quot;8dp&quot;
        app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
        app:layout_constraintEnd_toEndOf=&quot;parent&quot;
        app:layout_constraintStart_toStartOf=&quot;parent&quot;
        app:layout_constraintTop_toBottomOf=&quot;@+id/btn_add&quot;&gt;

        &lt;LinearLayout
            android:id=&quot;@+id/linear_layout&quot;
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:orientation=&quot;vertical&quot; /&gt;
    &lt;/ScrollView&gt;
&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 :

    public class MainActivity extends AppCompatActivity {

    // declare the required views variable
    private EditText uInput;
    private LinearLayout linearLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        // initializes required view variable
        uInput = findViewById(R.id.ed_name);

        linearLayout = findViewById(R.id.linear_layout);

        Button btnAdd = findViewById(R.id.btn_add);
        btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = uInput.getText().toString();
                if (!name.isEmpty()) {
                    CheckBox checkBox = new CheckBox(MainActivity.this);
                    checkBox.setText(name);
                    linearLayout.addView(checkBox);
                } else
                    Toast.makeText(MainActivity.this, &quot;The name cannot be empty!&quot;, Toast.LENGTH_LONG).show();
            }
        });

    }
}

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

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

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

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

ScrollView sv = new ScrollView(this);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
sv.addView(ll);

Button b = new Button(this);

b.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {

        CheckBox cb = new CheckBox(getApplicationContext());
        cb.setText(myTextBoxText.getText());
        ll.addView(cb);

    }
});
英文:

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

ScrollView sv = new ScrollView(this);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
sv.addView(ll);

Button b = new Button(this);

b.setOnClickListener(new OnClickListener() {
    @Override
     public void onClick(View v) {
    
        CheckBox cb = new CheckBox(getApplicationContext());
        cb.setText(myTextBoxText.getText());
        ll.addView(cb);
    
    }
});

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:

确定