gpt4 book ai didi

android - 如何清除 Android TextView 中的文本?

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:52:40 29 4
gpt4 key购买 nike

所以我有一个像这样的 Android 程序:

package com.example.androiddemo;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

public class AndroidDemo extends Activity {
String[] messages = {"Short Text",
"I want to show some really long text" +
"on the display of the phone. " +
"Having run out of ideas on what to type, " +
"I am adding this text which makes absolutely " +
"no sense."};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

final Button add = (Button) findViewById(R.id.addBtn);
final Button clear = (Button) findViewById(R.id.clrBtn);
final EditText text = (EditText) findViewById(R.id.display);

add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(v == add){
text.setText(messages[new java.util.Random().nextInt(messages.length)]);
}else if(v == clear){
text.setText("");
}
}
});
}
}

将文本添加到 TextView 的按钮工作得很好,但文本永远不会清除。
我认为 TextView 的操作类似于将文本设置为 ""JTextFieldJTextArea清除它。

如何清除文本?

最佳答案

试试这个,

public class AndroidDemo extends Activity implements OnClickListener{
Button add, clear;
EditText text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

add = (Button) findViewById(R.id.addBtn);
clear = (Button) findViewById(R.id.clrBtn);
text = (EditText) findViewById(R.id.display);
add.setOnClickListener(this);
clear.setOnClickListener(this);
}
@Override
public void onClick(View v){
if(v == add){
text.setText(messages[new java.util.Random().nextInt(messages.length)]);
}else if(v == clear){
text.setText("");
}
}
}

关于android - 如何清除 Android TextView 中的文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17464515/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com