1

私は2つのeditTextを持っており、onTextChangedで両方の入力を処理したいのですが、配列を使用してこれを行うことができますか?配列を使用せずにそれを行う方法がわかりません。OK、これは私が持っているものの更新です。

    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

0

afterTextChanged()コードを のメソッドに移動しようとしましたTextWatcherか?

外部リスナーは、フェーズ中に内部Editorの前に更新されるため、内部でまだ更新されていないため、との読み取り値が期待どおりの結果を返さない場合があります。EditTextonTextChanged()firstText.getText()secondText.getText()

于 2013-06-12T21:36:13.217 に答える