1

我有 2 个 editText,我想用 onTextChanged 处理这两个输入,如果可以的话,我可以用一个数组来做到这一点吗?我不明白如果不使用数组我怎么能做到这一点。好的,这是我所拥有的更新。

    public class AlphaActivity extends Activity {

private static final String TO_BOX = "TO_BOX";
private static final String FROM_BOX = "FROM_BOX";
//  private String updateGuess;
//  private String update_label;

private int guess, theFirst, theLast;
//private int count;

private String update_text;

EditText firstText;
EditText secondText;

TextView updateLabel;

Button tooHighButton;
Button tooLowButton;
Button correctButton;
Button newGameButton;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_alpha);

    if(savedInstanceState == null){
        // Just started
        theFirst = 0;
        theLast = 100; 
    } 
    else
    {
        // App is being restored
        theFirst = savedInstanceState.getInt(TO_BOX);
        theLast = savedInstanceState.getInt(FROM_BOX); 
    }

    //fromBox   = (EditText) findViewById(R.id.firstText);
    //toBox = (EditText) findViewById(R.id.secondText);

    //fromBox.addTextChangedListener(fromBox);
    //toBox.addTextChangedListener(toBox);


    updateLabel = (TextView)findViewById(R.id.updateText);

    firstText   = (EditText)findViewById(R.id.firstText);
    firstText.addTextChangedListener(fromBoxListener);

    secondText  = (EditText)findViewById(R.id.secondText);
    secondText.addTextChangedListener(fromBoxListener);

    tooHighButton = (Button)findViewById(R.id.guiTooHigh);
    tooLowButton = (Button)findViewById(R.id.tooLowGui);
    correctButton = (Button)findViewById(R.id.correctGui);

    setButtonOnClickListeners();

}



private TextWatcher fromBoxListener = new TextWatcher()
{

    @Override
    public void afterTextChanged(Editable arg0) {
        // TODO Auto-generated method stub




    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before,
            int count) {
        // TODO Auto-generated method stub

        try
        {
            //theFirst = Integer.parseInt(s.toString());
            theFirst = Integer.parseInt(firstText.getText().toString());
            theLast = Integer.parseInt(secondText.getText().toString());

            if (theFirst > theLast)
            {
                updateLabel.setText("You must flip your integers"); 
            }
            else if (theFirst < 0)
            {
                updateLabel.setText("You cannot enter a negative number!"); 

            }

            guess = (theFirst + theLast) / 2;
            updateLabel.setText("Did you think of " + guess + " ?");

        } catch (NumberFormatException nfe)
        {
            updateLabel.setText("You must enter an integer! ");
        }

        //updateLabel();

    }

};



private void setButtonOnClickListeners(){

    tooHighButton.setOnClickListener(new OnClickListener(){

        public void onClick(View arg0) {

            theLast = (guess - 1);
            guess = (theFirst + theLast) / 2;

            if (theFirst < theLast)
            {
                secondText.setText("" + theLast);
                updateLabel.setText("Did you think of " + guess + " ?");
                //count++;
            } else if (theFirst > theLast)
            {
                updateLabel.setText("It appears you changed your number!");
            } else
            {
                updateLabel.setText("Did you think of " + guess + " ?");
            }



        }
    });

    tooLowButton.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View arg0) {

            theFirst = (guess + 1);
            guess = (theFirst + theLast) / 2;

            if (theFirst < theLast)
            {
                firstText.setText("" + theFirst);
                updateLabel.setText("Did you think of " + guess + " ?");
                //count++;
            } else if (theFirst > theLast)
            {
                updateLabel.setText("It appears you changed your number!");
            } else
            {
                updateLabel.setText("Did you think of " + guess + " ?");
            }   

        }       
    });


    correctButton.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View arg0) {
            updateLabel.setText("Thank you for playing this game!");
        }

    });

}



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

}

4

2 回答 2

2

要完成我认为您要问的事情,您可以执行以下操作,

editText1.addTextChangedListener(fromBoxListener)
editText2.addTextChangedListener(fromBoxListener)

现在,代码在

 @Override
public void onTextChanged(CharSequence s, int start, int before,
        int count) {
    // TODO Auto-generated method stub

当其中任何一个中的文本发生更改时将运行。我不确定这是否是您真正想要的,因为我不知道您的逻辑是如何工作的,也不知道您最终要完成什么。

此外,由于您正在解析一个可能包含非整数的对象,您可能希望将其包装在 a 中try/catch或进行某种类型的错误检查

于 2013-06-12T20:45:13.813 回答
0

您是否尝试过将代码移动到您的afterTextChanged()方法TextWatcher

外部侦听器在阶段期间的Editor内部侦听器之前更新,因此您的读数可能不会返回您期望的结果,因为它们尚未在内部更新。EditTextonTextChanged()firstText.getText()secondText.getText()

于 2013-06-12T21:36:13.217 回答