gpt4 book ai didi

android - TextView 由于某种原因无法清除,Android

转载 作者:行者123 更新时间:2023-11-30 03:37:17 25 4
gpt4 key购买 nike

我希望这不是一个愚蠢的问题。我在清除 TextView 时遇到了一些问题。我环顾四周,每个人都在说使用:textView.setText("");在 onCreate 但由于某种原因似乎不起作用。基本上,我的应用程序只接受来自 editText 的数字,然后运行 ​​Fibonacci 数列(当单击按钮时)并在 textView 中显示结果。好吧,序列显示正常,但我希望每次单击按钮时清除 TextView - 到目前为止,它只是不断向已有内容添加更多文本。

我是在放置 textView.setText("");在错误的位置?还是我只是缺少其他一些概念? (我也试过从我的 OnClick 中放置它——这也没有用)。

这是我的代码:

public class MainActivity extends Activity {

// primary widgets
private EditText editText;
private TextView textView;
private Button button1;

static ArrayList<Integer> fibList = new ArrayList<Integer>();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editText1);
textView = (TextView) findViewById(R.id.textView2);
button1 = (Button) findViewById(R.id.button1);

//Attempt to clear TextView
textView.setText("");

button1.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

String input = editText.getText().toString();
int number = Integer.parseInt(input);
int tmp = 0;

// confirm input
if (number < 20) {
Toast.makeText(getApplicationContext(),
"You entered: " + number, Toast.LENGTH_LONG).show();
for (int i = 0; i <= number; i++) {
fibList.add(fib(i));

// sum even numbers
if (fib(i) % 2 == 0) {
tmp += fib(i);

}

}
} else {
Toast.makeText(getApplicationContext(),
"Number is too Large: " + number, Toast.LENGTH_LONG)
.show();
}
String array = fibList.toString();
textView.setText(array);

}

});

}

// run fibonacci sequence
public static int fib(int n) {
if (n < 2) {
return n;
} else {
return fib(n - 1) + fib(n - 2);
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

最佳答案

如果您希望在每次单击按钮时清除 TextView,则必须在 onClick 中加入 .setText。您将 .setText 放在 onCreate 中的原因是在您的 Activity 创建后立即清除文本,但由于您的按钮还没有被按下,所以 setText 什么都不做。此外,由于您的 onCreate 只会为您的 Activity 运行一次,因此它永远不会再次返回到 setText。尝试以下操作:

    public class MainActivity extends Activity {

// primary widgets
private EditText editText;
private TextView textView;
private Button button1;

static ArrayList<Integer> fibList = new ArrayList<Integer>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editText1);
textView = (TextView) findViewById(R.id.textView2);
button1 = (Button) findViewById(R.id.button1);



button1.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

textView.setText(""); //Clear the TextView
fibList.clear(); //Clear your array list before adding new elements

String input = editText.getText().toString();
int number = Integer.parseInt(input);
int tmp = 0;

// confirm input
if (number < 20) {
Toast.makeText(getApplicationContext(),
"You entered: " + number, Toast.LENGTH_LONG).show();
for (int i = 0; i <= number; i++) {
fibList.add(fib(i));

// sum even numbers
if (fib(i) % 2 == 0) {
tmp += fib(i);

}

}
} else {
Toast.makeText(getApplicationContext(),
"Number is too Large: " + number, Toast.LENGTH_LONG)
.show();
}
String array = fibList.toString();
textView.setText(array);

}

});

}

// run fibonacci sequence
public static int fib(int n) {
if (n < 2) {
return n;
} else {
return fib(n - 1) + fib(n - 2);
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

关于android - TextView 由于某种原因无法清除,Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16466916/

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