Advertisement
Guest User

for Sawser

a guest
May 3rd, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.34 KB | None | 0 0
  1. package com.example.igor.myregex;
  2.  
  3. import android.support.v7.app.AppCompatActivity;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.widget.Button;
  7. import android.widget.EditText;
  8. import android.widget.Toast;
  9. /*
  10.     All this "PROGRAM" does is to determin if the userInput was numbers or not!
  11. */
  12. public class MainActivity extends AppCompatActivity
  13. {
  14.     EditText ip;//<-- set up variable for your EditText (that exists in XML)
  15.     Button btn; //<-- set up variable for your button (that exists in the XML)
  16.  
  17.      //in here you will connect your Java variable with the XML tags (using the tags ID's)
  18.     @Override
  19.     protected void onCreate(Bundle savedInstanceState)
  20.     {
  21.         super.onCreate(savedInstanceState);
  22.         setContentView(R.layout.activity_main);
  23.         ip = (EditText)findViewById(R.id.idTextIP);//<--connect EditText Variable with EditText tag ID (that located in the XNL)
  24.         btn = (Button)findViewById(R.id.idBtn); //<-- connect Button Variable with the Button tag ID (that located in the XML)
  25.     }
  26.      //this method will activate the following actions after pressing the button on the screen!
  27.      //NOTE! That if you wish to make this method to activate on button click you need to go to the button TAG in XML
  28.      //and write onClick and in it write THIS (onConfirm) method name!
  29.     public void onConfirm(View v)//<-- do not forget to insert the View v or else Java wont that you wish to use this method
  30.      //on XML Tag
  31.     {
  32.         String str = idText.getText().toString(); //get string from the editText
  33.  
  34.         //just like discribed aboe ^ create String variable inside the method that will contain all of users input in the EditText!
  35.  
  36.         boolean isNumeric;//<-- set boolean Variable inside the method and leave it empty (By Defualt its false [AKA isNumeric=false])
  37.         isNumeric = str.matches("[0-9]{3,}+"); // enter regex here
  38.  
  39.         //now just like described above ^ you equal the isNumeric (boolean) varialbe with the str (String) variable while
  40.         //the str variable is used with .matches(); method that CHECKS if the String input matches the Regex you set!
  41.  
  42.         Toast.makeText(this, "is Numeric "+isNumeric, Toast.LENGTH_SHORT).show();
  43.  
  44.         //now print it all in a toast to see if the isNumeric true or false ( depends on users input in the EditText!)
  45.  
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement