TextView无法清除某些原因,Android

时间:2013-05-09 16:49:11

标签: android textview

我希望这不是一个愚蠢的问题。我在清除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;
}

}

3 个答案:

答案 0 :(得分:1)

如果您想要在每个按钮上清除TextView,则.setText必须在onClick。你将.setText放在onCreate中的原因是为了在创建活动后立即清除文本,但由于您的按钮尚未推送,因此您还没有任何要清除的内容setText将无能为力。此外,由于您的onCreate只会针对您的活动运行一次,因此它永远不会再次返回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;
}

}

答案 1 :(得分:0)

在向其添加新结果之前,您需要清除Button Click事件上的textView。将其作为:

    button1.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
        textView.setText("");  //<<<<<< clear TextView on Button Click
        .....

答案 2 :(得分:0)

问题更有可能是未被清除的fibList