我有 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;
}
}