Domanda

I have an issue where I am trying to create an app where a bill is split amongst a number of people. That being said I must also say that I am a total beginner and the code I have written may need tightening up. I have utilized the TextWatcher method for the watching of the two (2) EditText fields. This seems to only work in one direction where for the two (2) fields Billtotal and Number of People. The logic only seems to work in one direction where the Billtotal needs to be last for this to work. If I select the Billtotal field first to enter into the logic does not seems to work. I find this strange as I use the && operator in the Boolean logic:

    private void enablecalifready() {
    boolean isready= (numofpep.getText().length() > 0) && (billtotal.getText().length() > 0);
    if(isready){
            calcbtn.setEnabled(true);
    }else {
            calcbtn.setEnabled(false);
          }
    }

The above code is selected from the following piece of code:

    billtotal.addTextChangedListener(new TextWatcher(){

@Override
public void afterTextChanged(Editable s) {
    enablecalifready();
}

@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 count,int after) {
}
    });

now my app starts with the button disabled and what I would like is when the logic finds that both the fields are populated with items no matter what the order they are entered that the button is enabled once both fields are populated.

I am open to any logic as I am new to the Android coding world and is willing to accept any help.

È stato utile?

Soluzione 2

You're only adding the listener to the billTotal EditText so your method will only be called after you've editing the text in that field. You should put the watcher code into a new variable and assign it as the text change listener for both EditText fields like so:

TextWatcher textWatcher = new TextWatcher() {
    @Override public void afterTextChanged(Editable s) {
        enablecalifready();
    }

    @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {  }
    @Override public void onTextChanged(CharSequence s, int start, int count,int after) {  }
}

numofpep.addTextChangedListener(textWatcher);
billtotal.addTextChangedListener(textWatcher);

Also think about approaching enabling the button the way Robin says as they are right - it is cleaner

Altri suggerimenti

    boolean isready= (numofpep.getText().length() > 0) && (billtotal.getText().length() > 0);

should be:

if(numofpep.getText().length() > 0) && (billtotal.getText().length() > 0){
    isready = true;
}else{
    isready = false;
}

I would use a methode for this:

private boolean isReady(){
    if(numofpep.getText().length() > 0) && (billtotal.getText().length() > 0){
        return true;
    }else{
        return false;
    }
}

And in the afte TextChanged or the onCheckChanged methode i would do this:

calcbtn.setEnabled(isReady());
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top