onClick 更改用户输入显示和新的 textView

onClick changes to user input display and new textView

onClick 我想让问题 2 替换问题 1——而不是两者同时可见——并让用户输入第二个问题进入它适当的文本视图。当我尝试循环数组时出现错误:"questionNumber++".

package com.example.greg;

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

public class menu extends Activity {

Button   mButton;
EditText mEdit;

@Override
protected void  onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mButton = (Button)findViewById(R.id.button);
    mEdit   = (EditText)findViewById(R.id.userAnswereditText);

    final TextView [] myTexts = new TextView[2];
    myTexts[0]=(TextView)findViewById(R.id.varATextView);
    myTexts[1]=(TextView)findViewById(R.id.varBTextView);

    final int questionNumber = 0;
    mButton.setOnClickListener(
    new View.OnClickListener()
       {
            public void onClick(View view)
            {                
                myTexts[questionNumber].setText(mEdit.getText().toString());
                mEdit.setText(null);
                questionNumber++;      
            }
        }); 
    }

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
}
}
public class menu extends Activity {

 Button   mButton;
 EditText mEdit;
 int questionNumber = 0;

@Override
protected void  onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mButton = (Button)findViewById(R.id.button);
mEdit   = (EditText)findViewById(R.id.userAnswereditText);

final TextView [] myTexts = new TextView[2];
myTexts[0]=(TextView)findViewById(R.id.varATextView);
myTexts[1]=(TextView)findViewById(R.id.varBTextView);


mButton.setOnClickListener(
new View.OnClickListener()
   {
        public void onClick(View view)
        {

            myTexts[questionNumber].setText(mEdit.getText().toString());
            mEdit.setText(null);
            myTexts[0].setvisibility(View.GONE); // use this to hide
            myTexts[1].setvisibility(View.VISIBLE); // use this to show
            questionNumber++;

        }
    });

    }

@Override
protected void onPause() {
  // TODO Auto-generated method stub
  super.onPause();
}

只需删除带有 questionNumber 的 final 一词,我所做的就是在 class 级别对其进行初始化,这样我就可以在 onclick 方法中访问它而无需将其声明为 final。希望对你有帮助。

你正在声明 textview[] "final" ...因此在索引 0 上你的值不会改变..所以删除它..也从 int

中删除 "final"
public class menu extends Activity {


Button   mButton;
EditText mEdit;

@Override
protected void  onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mButton = (Button)findViewById(R.id.button);
mEdit   = (EditText)findViewById(R.id.userAnswereditText);

TextView [] myTexts = new TextView[2];
myTexts[0]=(TextView)findViewById(R.id.varATextView);
myTexts[1]=(TextView)findViewById(R.id.varBTextView);

int questionNumber = 0;
mButton.setOnClickListener(
new View.OnClickListener()
   {
        public void onClick(View view)
        {                
            myTexts[questionNumber].setText(mEdit.getText().toString());
            mEdit.setText(null);
            questionNumber++;      
        }
    }); 
}

@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
}