uopspop

Untitled

Sep 27th, 2016
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.03 KB | None | 0 0
  1. package idv.ron.activitiesdemo;
  2.  
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.view.View;
  7. import android.widget.EditText;
  8.  
  9. public class MainActivity extends AppCompatActivity {
  10.     private EditText etProgramming, etDataStructure, etAlgorithm;
  11.  
  12.     @Override
  13.     protected void onCreate(Bundle savedInstanceState) {
  14.         super.onCreate(savedInstanceState);
  15.         setContentView(R.layout.main_activity);
  16.         findViews();
  17.     }
  18.  
  19.     private void findViews() {
  20.         etProgramming = (EditText) findViewById(R.id.etProgramming);
  21.         etDataStructure = (EditText) findViewById(R.id.etDataStructure);
  22.         etAlgorithm = (EditText) findViewById(R.id.etAlgorithm);
  23.     }
  24.  
  25.     private boolean isValid(EditText editText) {
  26.         String pattern = "1[0]{2}|[0-9]{1,2}";
  27.         String text = editText.getText().toString();
  28.         if (!text.matches(pattern)) {
  29.             editText.setError("0 ~ 100");
  30.             return false;
  31.         } else {
  32.             return true;
  33.         }
  34.     }
  35.  
  36.     public void onSubmitClick(View view) {
  37.         // use "&" not "&&" because even the first isValid() return false,
  38.         // the second isValid() will still be called
  39.         boolean isValid =
  40.                 isValid(etProgramming) & isValid(etDataStructure) & isValid(etAlgorithm);
  41.         if (!isValid) {
  42.             return;
  43.         }
  44.         int programming = Integer.parseInt(etProgramming.getText().toString());
  45.         int dataStructure = Integer.parseInt(etDataStructure.getText().toString());
  46.         int algorithm = Integer.parseInt(etAlgorithm.getText().toString());
  47.         //                          (in, out)
  48.         Intent intent = new Intent(this, ResultActivity.class);
  49.         Bundle bundle = new Bundle();
  50.         bundle.putInt("programming", programming);
  51.         bundle.putInt("dataStructure", dataStructure);
  52.         bundle.putInt("algorithm", algorithm);
  53.         intent.putExtras(bundle);
  54.         startActivity(intent);
  55.     }
  56. }
Add Comment
Please, Sign In to add comment