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

时间:2015-02-02 04:52:52

标签: android arrays onclick android-edittext textview

onClick我想问问题2替换问题1 - 而不是两者同时可见 - 并让用户输入第二个问题进入它的相应textView。当我尝试循环数组时出现错误:" 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();
}
}

2 个答案:

答案 0 :(得分:0)

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,我所做的就是在类级别初始化它,这样我就可以在onclick方法中访问它而不会将其声明为final。希望它有所帮助。

答案 1 :(得分:0)

你正在声明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();
}
}