Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.igor.myregex;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Toast;
- /*
- All this "PROGRAM" does is to determin if the userInput was numbers or not!
- */
- public class MainActivity extends AppCompatActivity
- {
- EditText ip;//<-- set up variable for your EditText (that exists in XML)
- Button btn; //<-- set up variable for your button (that exists in the XML)
- //in here you will connect your Java variable with the XML tags (using the tags ID's)
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- ip = (EditText)findViewById(R.id.idTextIP);//<--connect EditText Variable with EditText tag ID (that located in the XNL)
- btn = (Button)findViewById(R.id.idBtn); //<-- connect Button Variable with the Button tag ID (that located in the XML)
- }
- //this method will activate the following actions after pressing the button on the screen!
- //NOTE! That if you wish to make this method to activate on button click you need to go to the button TAG in XML
- //and write onClick and in it write THIS (onConfirm) method name!
- public void onConfirm(View v)//<-- do not forget to insert the View v or else Java wont that you wish to use this method
- //on XML Tag
- {
- String str = idText.getText().toString(); //get string from the editText
- //just like discribed aboe ^ create String variable inside the method that will contain all of users input in the EditText!
- boolean isNumeric;//<-- set boolean Variable inside the method and leave it empty (By Defualt its false [AKA isNumeric=false])
- isNumeric = str.matches("[0-9]{3,}+"); // enter regex here
- //now just like described above ^ you equal the isNumeric (boolean) varialbe with the str (String) variable while
- //the str variable is used with .matches(); method that CHECKS if the String input matches the Regex you set!
- Toast.makeText(this, "is Numeric "+isNumeric, Toast.LENGTH_SHORT).show();
- //now print it all in a toast to see if the isNumeric true or false ( depends on users input in the EditText!)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement