[英]I want to create a checkbox when button is clicked
I want to make it so that every time someone types something in the editText
and clicks the button, the text comes up.我想让它每次有人在editText
中输入editText
并单击按钮时,文本就会出现。 It does it in my code, but I also want to add a checkbox next to it.它在我的代码中完成,但我还想在它旁边添加一个复选框。 How can I add the checkbox every time the button is pressed?每次按下按钮时如何添加复选框?
This is how it looks in the emulator right now after I typed test in the editText:这是我在 editText 中键入 test 后,它现在在模拟器中的样子:
https://gyazo.com/6b9a050976ecd2c4b509220263bbdce1 https://gyazo.com/6b9a050976ecd2c4b509220263bbdce1
The second problem is: when I write a second todo, it overwrites the last one so right now I can only have 1 todo.第二个问题是:当我写第二个 todo 时,它会覆盖最后一个,所以现在我只能有 1 个 todo。 Why?为什么?
Code:代码:
final TextView textViewPrint;
Button btn_print;
final EditText editTextType;
textViewPrint = findViewById(R.id.print_text);
btn_print = findViewById(R.id.button_add);
editTextType = findViewById(R.id.test_textEdit);
btn_print.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
textViewPrint.setText(editTextType.getText().toString()) ;
editTextType.setText("");
}
});
You can add to your layout programmatically after it has been inflated.您可以在布局膨胀后以编程方式将其添加到布局中。 Add an id to your LinearLayout:给你的 LinearLayout 添加一个 id:
android:id ="@+id/layout"
Then in OnCreate (after your existing code), get a reference to it and add controls as you wish.然后在 OnCreate 中(在您现有的代码之后),获取对它的引用并根据需要添加控件。 And add these lines in OnCreate, For example:并在 OnCreate 中添加这些行,例如:
LinearLayout l = (LinearLayout)findViewById(R.id.layout);
CheckBox cb = new CheckBox(this);
int lHeight = LinearLayout.LayoutParams.MATCH_PARENT;
int lWidth = LinearLayout.LayoutParams.WRAP_CONTENT;
l.addView(cb, new LinearLayout.LayoutParams(lHeight, lWidth));
setContentView(l);
LinearLayout l = (LinearLayout)findViewById(R.id.layout);
btn_print.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
CheckBox cb = new CheckBox(this);
LayoutParams lparams = new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
cb.setLayoutParams(lparams);
l.addView(cb);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.