-3

I am trying to create a simple Android app by Android Studio (v2.2.3). I am new to it and hope someone can help me answer the following questions:

  1. My app needs to enter some inputs-->click button-->show outputs. So, from the widgets part, I should select "EditText" for inputs, "TextView" for outputs and "button" for button, right?

  2. Every inputs has its own constraints. Some of them need to be positive integer (do not include 0) and some of them need to be greater than 3. How could I set these constraints? If users violate the constraints, how could I pop up alert?

Thanks in advance.

zoeylong
  • 77
  • 1
  • 9

2 Answers2

0

You can use some basic java logic here..!!

addTextChangeListener() to your edit text like

yourEditText.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

            // 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 afterTextChanged(Editable s) {

            // Here make your logics
        }
    });

And then do the calculation in the afterTextChanged()

Something like..

int enteredValue = Integer.parseInt(s.toString());
if(enteredValue<0){
//Value is negative
//TODO Alert your user and Empty the EditText
}

You can make any constraints in this and pop up alert if violation occur.

ato
  • 11
  • 3
0

My app needs to enter some inputs-->click the button-->show outputs. So, from the widgets part, I should select "EditText" for inputs, "TextView" for outputs and "button" for the button, right?

Correct

Some of them need to be a positive integer (do not include 0) and some of them need to be greater than 3. How could I set these constraints? If users violate the constraints, how could I pop up an alert?

Declare 2 EditText > Get value from it > parse String to int > check if valid or not:

EditText one = (EditText) findViewById(R.id.your_id);
EditText two = (EditText) findViewById(R.id.your_id);

String oneValue = one.getText().toString();
String twoValue = two.getText().toString();
int intOne = Integer.parseInt(oneValue);
int intTwo = Integer.parseInt(twoValue);

if(TextUtils.isEmpty(oneValue) || intOne < 1){       //this block checks int < 1 
   one.setError("Error with value ! Bla bla");
   return;
}

if(TextUtils.isEmpty(twoValue) || intTwo < 3){       //this block checks int < 3
   two.setError("Error with value two ! Bla bla");
   return;
}
W4R10CK
  • 5,502
  • 2
  • 19
  • 30