简体   繁体   English

我想在单击按钮时创建一个复选框

[英]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);

First You need add to LinearLayout layout首先你需要添加到 LinearLayout 布局

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.

相关问题 我想在单击按钮时更新表 - I want to update table when Button is clicked 单击 JSP 页面中的按钮时,我想调用 Ksh 脚本 - I want to call a Ksh script when clicked on button in JSP page 单击按钮时,如何通过编程方式创建一个新的TextView? - How do I create a New TextView Progmatically when a button is clicked? 我想更改点击的任何按钮的大小 - I want to change the size of any button clicked 单击以启用 Java Android 开发中的按钮时,如何处理两个复选框? - How can I handle two checkbox when clicked to enable a button in Java Android develop? Java:单击按钮时,我想获取TextField值。 怎么做? - Java: I want to get TextField value when I clicked a button. How to do it? 我想在单击X(关闭按钮)时关闭框架 - I want to make my frame close when the X(close button) is clicked 单击Jbutton时,将移动按钮或更改标签文本。 我只想同时执行 - when Jbutton is clicked either button is move or label text is changed. I just want to perform both 单击按钮时将复选框数据传递到另一个活动 - Passing the checkbox data when button is clicked to another activity 在复选框中使用共享的首选项,以便在单击按钮时始终选中 - Use shared preferences in checkbox to make checked for always when button is clicked
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM