[英]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.