英文:
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
<?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="Enter Name to Add in the list"
android:id="@+id/ed_name"
android:layout_marginTop="15dp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"
android:id="@+id/btn_add"
android:layout_below="@+id/ed_name"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
/>
</RelativeLayout>
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
<?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">
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 :
<?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="Enter name to add in the list"
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="Add"
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>
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, "The name cannot be empty!", 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);
}
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论