Android - 向输入字段添加验证

Android - adding validation to input fields

我正在制作一个 Android 应用,它使用 sqlite database 存储应用数据,activity class 编辑/插入数据,DbHelper 来管理数据库的创建和版本,以及 content provider 来管理对数据库的访问。

我已经设法从数据库中获取要查询、插入、删除和编辑的数据。但是,我不太确定如何向用户添加视觉反馈。我尝试在放置 IllegalArgumentException 的地方添加 Toast,但该应用程序只会将内容添加到数据库中。

如果我省略名称,应用程序将触发定义的 IllegalArgumentException 然后使应用程序崩溃。

这是 content provider

insert 方法的片段
@Override
public Uri insert(Uri uri, ContentValues contentValues) {
    final int match = sUriMatcher.match(uri);
    switch (match) {
        case PATIENT:
            return insertPatient(uri, contentValues);
        default:
            throw new IllegalArgumentException("Insertion is not supported for " + uri);
    }
}

/**
 * Insert a patient into the database with the given content values.
 */
private Uri insertPatient(Uri uri, ContentValues values) {
    String name = values.getAsString(PatientEntry.COLUMN_PATIENT_NAME);
    if (name == null || name.length()==0) {
        //Toast.makeText(getContext(), "Patient requires a name", Toast.LENGTH_SHORT).show();
        throw new IllegalArgumentException("Patient requires a name");
    }

    Integer weight = values.getAsInteger(PatientEntry.COLUMN_PATIENT_WEIGHT);
    if (weight != null && weight < 0) {
        throw new IllegalArgumentException("Patient requires valid weight");
    }

    SQLiteDatabase database = mDbHelper.getWritableDatabase();

    long id = database.insert(PatientEntry.TABLE_NAME, null, values);
    if (id == -1) {
        Log.e(LOG_TAG, "Failed to insert row for " + uri);
        return null;
    }

    getContext().getContentResolver().notifyChange(uri, null);
    return ContentUris.withAppendedId(uri, id);
}

这是 activity 文件中的一个片段

private void savePatient() {
    String nameString = mNameEditText.getText().toString().trim();
    String weightString = mWeightEditText.getText().toString().trim();

    if (mCurrentPatientUri == null &&
            TextUtils.isEmpty(nameString) && TextUtils.isEmpty(weightString) {
            Toast.makeText(this, "Data was not saved", Toast.LENGTH_SHORT).show();
        return;
    }

    ContentValues values = new ContentValues();
    values.put(PatientEntry.COLUMN_PATIENT_NAME, nameString);
    int weight = 0;
    if (!TextUtils.isEmpty(weightString)) {
        weight = Integer.parseInt(weightString);
    }
    values.put(PatientEntry.COLUMN_PATIENT_WEIGHT, weight);

    // Determine if this is a new or existing Patient by checking if mCurrentPatientUri is null or not
    if (mCurrentPatientUri == null) {
        // This is a NEW patient
        Uri newUri = getContentResolver().insert(PatientEntry.CONTENT_URI, values);

        if (newUri == null) {
            Toast.makeText(this, getString(R.string.editor_insert_patient_failed),
                    Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, getString(R.string.editor_insert_patient_successful),
                    Toast.LENGTH_SHORT).show();
        }
    } else {
        // Otherwise this is an EXISTING patient
        int rowsAffected = getContentResolver().update(mCurrentPatientUri, values, null, null);

        if (rowsAffected == 0) {
            Toast.makeText(this, getString(R.string.editor_update_patient_failed),
                    Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, getString(R.string.editor_update_patient_successful),
                    Toast.LENGTH_SHORT).show();
        }
    }
}

有人可以帮忙吗?

................................................ .....................

编辑 1:

这是 activity 中的菜单,调用 savePatient:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // User clicked on a menu option in the app bar overflow menu
    switch (item.getItemId()) {
        case R.id.action_save:
            savePatient();
            finish();
            return true;
        case R.id.action_delete:
            showDeleteConfirmationDialog();
            return true;
        case android.R.id.home:
            if (!mPatientHasChanged) {
                NavUtils.navigateUpFromSameTask(EditorActivity.this);
                return true;
            }

            // Otherwise if there are unsaved changes, setup a dialog to warn the user.
            DialogInterface.OnClickListener discardButtonClickListener =
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            NavUtils.navigateUpFromSameTask(EditorActivity.this);
                        }
                    };

            showUnsavedChangesDialog(discardButtonClickListener);
            return true;
    }
    return super.onOptionsItemSelected(item);
}

对于视觉反馈验证,应尽早完成。 例如,在您对某些用户操作调用 save patient 之前,只需调用 validatePatient() ,如果失败则不会调用 save。

对于视觉反馈,您可以在字段下方显示错误文本,仅当与该字段相关的验证失败时才会显示这些文本。