0

I have a pretty easy problem to solve but I can't see any tutorials on internet to help me. Because this problem is pretty basic I think I don't use the right keywords for my search.

My problem is that I parse an EditText with a method and use this method to feed the values of a graph:

public int getSetPoint(){
    int setPointRequest = Integer.parseInt(txtSetPoint.getText().toString());
    if (setPointRequest > 90 && setPointRequest < 120);
        return setPointRequest;
    // ...
}

I would like to parse the value only if setPointRequest() has the values between 91 and 119 and after the user press Enter on the keypad. When the user is prompted to enter a value only numbers are shown on the keypad already.

Currently all the values that the user inputs even if he doesn't hit Enter are registered on the graph.

Thanks for your help! Math

Mathieu660
  • 61
  • 1
  • 9

2 Answers2

0
public int getSetPoint(){
  int setPointRequest = Integer.parseInt(txtSetPoint.getText().toString());
  if(setPointRequest>90 && setPointRequest<120) //notice the lack of ;
    return setPointRequest;

  return -1;
}

To handle an enter key:

txtSetPoint.setOnEditorActionListener ( new OnEditorActionListener() {
  @Override
  public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
  {
    if (actionId == EditorInfo.IME_NULL)
      getSetPoint();
    return true;
  }
});

Your first issue is the semicolon right after the if. Remove that.

Second issue is that after you fix this issue, you will get a compile time error since you need a default return statement (that can be guaranteed to execute in case your if doesn't). I used -1, you can customize it to your needs. I'd also put the parsing in a try/catch just to be safe from any odd errors, but it's up to you.

In addition, the naming is bad, getSetPoint() is not immediately clear from the name, but the return type and lack of arguments do give a rough idea.

A--C
  • 36,351
  • 10
  • 106
  • 92
  • This solution crash my application and does not help me with the "enter" on the keypad I asked. Could you please elaborate this solution ? – Mathieu660 Mar 05 '13 at 00:25
  • @Mathieu660 then post how you're calling this method. It is most likely crashing since `-1` (the default value) is returned, and you're not revalidating your input. As for handling enter keypresses, you will want to read [this](http://stackoverflow.com/questions/1489852/android-handle-enter-in-an-edittext) answer. However, you should make a button instead, it is more clear *and* it allows you to easily handle the submit. – A--C Mar 05 '13 at 00:28
  • actually I inputed another value because -1 will of course crash my graph. I used 98 but still it crash my app. setPointValue.add(getSetPoint()); it's called there and it's an array – Mathieu660 Mar 05 '13 at 00:36
  • @Mathieu660 Would you like to edit your question to post more code and a stack trace? All I changed with your method was taking out the semicolon and then adding a default return, which is needed to compile. Something else seems amiss. – A--C Mar 05 '13 at 00:41
  • 1
    Will do that for the "enter" part but I fixed the return problem there was something else I was doing that was wrong – Mathieu660 Mar 05 '13 at 00:56
0

Combining what A--C said, Us the setOnKeyListener for your EditText so that it will only get the text when the user hits the enter key.

public int getSetPoint(){
  int setPointRequest = Integer.parseInt(txtSetPoint.getText().toString());
  if(setPointRequest>90 && setPointRequest<120)
    {return setPointRequest;}

  else {return -1;}
}

txtSetPoint.setOnKeyListener(new OnKeyListener(){ //this method waits for the enter key to be hit

@Override
public boolean onKey(View arg0, int arg1, KeyEvent arg2) {

    switch(arg1)
    {
          case KeyEvent.KEYCODE_ENTER:
               getSetPoint(); //when enter key is hit, call the getSetPoint method
               return true;
          default: 
               return false;

    }

   }
});
anthonycr
  • 4,146
  • 1
  • 28
  • 35