[Android Studio] I want to set restrictions on the values registered in EditText [Java]

Introduction

Three images are an application that makes a note of blood pressure that I made. The value entered in EditText is saved in the database. At that time, I wanted to avoid out-of-range values and nulls, so I investigated various things and wrote them with if statements and logical operators. By the way, with this code, if you try to register data continuously, the application will crash. Click here for an article about continuous registration failure (jump to another article I wrote).

Sample code

MainActivity.java DatePicker, TimePicker, and custom font code are omitted. Blog that I referred to when introducing DatePicker and TimePicker. Click here for an article on custom fonts (jump to another article I wrote). Write the if statement inside the onClick method. (Although I put the code in it, the insertData method is unstable code, so please refrain from using it as it is)

MainActivity.java


public class MainActivity extends AppCompatActivity {
    EditText getMaxBP;
    EditText getMinBP;
    EditText getPulse;
    Button btEntry;
    Button btNext;
    DatabaseHelper helper;
    SQLiteDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        helper = new DatabaseHelper(MainActivity.this);
        db = helper.getWritableDatabase();

        btEntry = findViewById(R.id.btEntry);
        btEntry.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getMaxBP = findViewById(R.id.etUpperBloodPressure);
                String maxBP = (getMaxBP.getText().toString());
                getMinBP = findViewById(R.id.etLowerBloodPressure);
                String minBP = (getMinBP.getText().toString());
                getPulse = findViewById(R.id.etPulse);
                String pulse = (getPulse.getText().toString());

                String nullMsg = "please fill in the value";
                if(maxBP.length()==0){
                    getMaxBP.setError(nullMsg);
                    return;
                } else if(minBP.length()==0){
                    getMinBP.setError(nullMsg);
                    return;
                } else if(pulse.length()==0){
                    getPulse.setError(nullMsg);
                    return;
                }
                String errorMsg = "This value cannot be entered";
                if(!(80 <= Integer.parseInt(maxBP) && Integer.parseInt(maxBP) <= 180)){
                    getMaxBP.setError(errorMsg);
                    return;
                } else if(!(50 <= Integer.parseInt(minBP) && Integer.parseInt(minBP) <= 140)){
                    getMinBP.setError(errorMsg);
                    return;
                } else if(!(40 <= Integer.parseInt(pulse) && Integer.parseInt(pulse) <= 120)){
                    getPulse.setError(errorMsg);
                    return;
                }
                //Method to register value in database
                insertData(db, maxBP, minBP, pulse);
                //If you succeed in registration, you will get a toast
                Toast toast = Toast.makeText(BloodPressureAdditionActivity.this, "Has registered", Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.show();
            }
        });
        btNext = findViewById(R.id.btNext);
        btNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, SubActivity.class);
                startActivity(intent);
            }
        });
    }
public void insertData(SQLiteDatabase db, String maxBP, String minBP, String pulse){
        ContentValues values = new ContentValues();
        try {
            values.put("_maxBP", maxBP);
            values.put("_minBP", minBP);
            values.put("_pulse", pulse);
            db.insert("_BPtable", null, values);
        } finally {
            db.close();
    }
}

The explanation of the insertDate method, The value of the argument is passed to the columns _maxBP, _minBP, _pulse and registered in table (here _BPtable) by the insert method. I think this site is easy to understand for the explanation of the ContentValues class. Add Let's programming key and value insert and close are methods of SQLiteDatabase. Developer: SQLiteDatabase class setError If you use setError, the style of red surprise mark and black background white text like the image is automatically output. I wrote two if statements, The first is null The second is out-of-range input It corresponds to each. I'm glad if you can use it as a reference.

Recommended Posts

[Android Studio] I want to set restrictions on the values registered in EditText [Java]
[Android Studio] I want to use Maven library on Android
I want to simplify the log output on Android
I stumbled on the Java version in Android Studio, so I will summarize it
I want to set the conditions to be displayed in collection_check_boxes
Refer to C ++ in the Android Studio module (Java / kotlin)
I want to simplify the conditional if-else statement in Java
[Java] I want to perform distinct with the key in the object
[Android] I want to get the listener from the button in ListView
Java to C and C to Java in Android Studio
I want to get the IP address when connecting to Wi-Fi in Java
I want to send an email in Java.
3 ways to import the library in Android Studio
rsync4j --I want to touch rsync in Java.
I want to get the value in Ruby
I want to play a GIF image on the Andorid app (Java, Kotlin)
I want to do something like "cls" in Java
[Java] I want to calculate the difference from the date
I want to embed any TraceId in the log
I want to use ES2015 in Java too! → (´ ・ ω ・ `)
[Android Studio] [Java] How to fix the screen vertically
I tried to implement the Euclidean algorithm in Java
Rspec: I want to test the post-execution state when I set a method on subject
I want to place RadioButtons in the same RadioGroup at any position on the screen.
# 1_JAVA I want to get the index number by specifying one character in the character string.
I want to hit the API with Rails on multiple docker-composes set up locally
I want to use screen sharing on the login screen on Ubuntu 18
[Android, Java] Convenient method to calculate the difference in days
I want to use the Java 8 DateTime API slowly (now)
Even in Java, I want to output true with a == 1 && a == 2 && a == 3
I want to put the JDK on my Mac PC
How to switch Java in the OpenJDK era on Mac
I want to transition to the same screen in the saved state
I want to return multiple return values for the input argument
[Ruby on Rails] I want to get the URL of the image saved in Active Storage
In Java, I want to trim multiple specified characters from only the beginning and end.
I want to use Java Applet easily on the command line without using an IDE
Use JLine when you want to handle keystrokes on the console character by character in Java
When you want to implement Java library testing in Spock with multi-module in Gradle in Android Studio 3
I made the "Sunshine Ikezaki game" I saw on Twitter in Java.
I tried to create a simple map app in Android Studio
I just want to write Java using Eclipse on my Mac
I want to return to the previous screen with kotlin and java!
[Java] I want to check that the elements in the list are null or empty [Collection Utils]
[Wire Mock] I want to set up a stub / mock server in Java and perform E2E tests.
[Android Studio] Set an arbitrary image for the application background [Java]
Source to display character array with numberPicker in Android studio (Java)
The milliseconds to set in /lib/calendars.properties of Java jre is UTC
I want to add the disabled option to f.radio_button depending on the condition
I want to display the images under assets/images in the production environment
I want to remove the top margin in Grouped UITableView (swift)
I tried to display the calendar on the Eclipse console using Java.
I want to change the value of Attribute in Selenium of Ruby
The first thing to do when you want to be happy with Heroku on GitHub with Eclipse in Java
I examined the file created when selecting Empty Activity in Android Studio
I want to write JSP in Emacs more easily than the default.
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (PowerMockito edition)
(Limited to Java 7 or later) I want you to compare objects in Objects.equals
What wasn't fair use in the diversion of Java APIs on Android
I called the COTOHA API parser 100 times in Java to measure performance.
I want to display an error message when registering in the database