简体   繁体   English

按Enter键或按钮时,如何将EditText中的文本打印到textview

[英]How do I print text from EditText to a textview when pressed enter or a button

So I want the text the person wrote in the EditText box to be printed on the screen when pressed enter or clicked on the button. 因此,当按下输入或单击按钮时,我希望人在EditText框中写入的文本打印在屏幕上。

    final TextView txt= findViewById(R.id.empty_text);
    Button   btn =      findViewById(R.id.button_add);
    final EditText tst = findViewById(R.id.test_textEdit);


    final String value = tst.getText().toString();


    btn.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            txt.append(value+"\n");
            tst.setText("");
        }
    });

I think txt.append(value+"\\n"); 我认为txt.append(value +“\\ n”); I think I need something that tells the computer to print the text but I don't find one. 我想我需要一些东西告诉计算机打印文本,但我找不到。

value is only set once, and before you assign your ClickListener . value只设置一次,并且在分配ClickListener之前。 Just pull whatever text is present inside your EditText every time onClick() is called. 每次调用onClick()时,只需拉动EditText存在的任何文本。

btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        txt.setText(tst.getText().toString());
    }
});

Edit: I just realised you were looking to update the TextView on an "Enter" key press as well. 编辑:我刚刚意识到您正在按“Enter”键按下更新TextView。 For that you can try: 为此你可以尝试:

tst.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int id, KeyEvent event) {
        if (id == EditorInfo.IME_NULL) {
            txt.setText(tst.getText().toString());
            return true;
        }
        return false;
   }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 按下按钮时如何从文本字段中获取文本? - How do I get the text from a textfield when a button is pressed? 我的问题是,当按下按钮时,为什么我输入的编辑文本中的文本不会显示在另一个文本视图中? - My question is why won't my inputted text into an edittext show up in another textview when a button is pressed? 从EditText提取文本并在TextView中打印 - Extract Text From EditText and Print in TextView 如何让 TextView 从 EditText 打印值? - How to make TextView print the value from EditText? 在EditText中按Enter键时停止键盘关闭? - Stop keyboard from closing when Enter is pressed in EditText? 单击按钮后,如何将文本从alertdialog的EditText复制并粘贴到活动的EditText中? - How do I copy and paste text from EditText of alertdialog to EditText of my activity, on button click? 如何在按下“ enter”时取消选择“ EditText”元素 - How to have an 'EditText' element deselect itself when 'enter' is pressed 如何显示 EditText 中的文本? - How do I display text from an EditText? 如何从EditText中删除文本 - How do I remove text from EditText EditText 焦点在 android studio 中不起作用,我想在 edittext 焦点改变时隐藏一个 textview,怎么办? - EditText focus is not working in android studio, i want to hide a textview when focus change in edittext, how to do this?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM