Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/190.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 正在寻找清除TextView内容并显示新内容的方法_Java_Android_Textview - Fatal编程技术网

Java 正在寻找清除TextView内容并显示新内容的方法

Java 正在寻找清除TextView内容并显示新内容的方法,java,android,textview,Java,Android,Textview,下面的问题让我头疼。 我创建了一个TextView,它在单击按钮(askButton)后显示一个随机数组。 第二个按钮(rstButton)应该充当重置按钮,从文本中清除所有字段。它成功地清除了所有字段 我的问题是,在清除所述TextView并单击按钮(askButton)后,TextView内容不再是随机的,并反复显示相同的数组。我花了一整天的时间在各种网站上寻找解决方案,但都没有成功 String ansArray[] = { "yes","no", "Surely","Never"};

下面的问题让我头疼。 我创建了一个TextView,它在单击按钮(askButton)后显示一个随机数组。 第二个按钮(rstButton)应该充当重置按钮,从文本中清除所有字段。它成功地清除了所有字段

我的问题是,在清除所述TextView并单击按钮(askButton)后,TextView内容不再是随机的,并反复显示相同的数组。我花了一整天的时间在各种网站上寻找解决方案,但都没有成功

 String ansArray[] = { "yes","no", "Surely","Never"}; 
    final Random random = new Random();                                         
    final int select = random.nextInt(ansArray.length);
    final TextView ansText = (TextView) findViewById(R.id.textView1); 
    Button buttonAsk = (Button) findViewById(R.id.button1); 
    buttonAsk.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View v) {

        ansText.setText(ansArray[select]);   
我认为“final”修饰符会导致无法更改数组,但不将数组声明为final会给我一个错误并要求使用final修饰符。(我认为问题出在这里)

代码的第二部分包括复位按钮:

final EditText questionText = (EditText) findViewById(R.id.editText1);
    ImageButton btnReset = (ImageButton) findViewById(R.id.imageButton1);
    btnReset.setOnClickListener(new ImageButton.OnClickListener(){
        public void onClick(View v){
           questionText.setText("");
            ansText.setText("");

    }
});
那么我的问题是在于最后的修饰语,还是我做错了什么?谢谢你的帮助。提前谢谢

尝试删除“final”修饰符:

final Random random = new Random();                                         
final int select = random.nextInt(ansArray.length);
每次运行应用程序时,这些代码将始终生成一个值


祝你好运^

你只生成了一次这个数字。 去掉onClick侦听器外部的int-select

final TextView ansText = (TextView) findViewById(R.id.textView1); 
Button buttonAsk = (Button) findViewById(R.id.button1); 
buttonAsk.setOnClickListener(new Button.OnClickListener() {
    public void onClick(View v) {
        int select = random.nextInt(ansArray.length);
        ansText.setText(ansArray[select]); 

你以前做的只是得到一次随机数。在onClick中生成随机数后,每次新的单击都会得到一个新的随机数。

能否显示
按钮中的更多代码?sk.setOnClickListener
?我只是想看看每次单击
按钮ask
时,如何在变量
select
中获得一个新的随机整数。如果显示相同的值,则
select
变量的值很可能没有改变<代码>最终变量只能通过初始值设定项或赋值语句初始化一次。您可以尝试记录这些值并查看您自己。老实说,我没有想过每次都要得到一个新的整数。我会调查的。谢谢如果这是问题所在,则声明变量
选择
作为成员变量,并且它不必是
最终的
才能在
侦听器中访问