java - Android 文本验证

标签 java android

您好,我正在创建一个任务提醒应用程序。它有效,但我想创建某种 Toast 验证。例如,用户没有填写标题,我想要一个 toast “需要填写标题!” ETC 但我不知道该怎么做。

顺便说一句,我正在使用 EditText 小部件。

这是一种方法:

  protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


    mDbHelper = new RemindersDbAdapter(this);

    setContentView(R.layout.reminder_edit);

    mCalendar = Calendar.getInstance(); 
    mTitleText = (EditText) findViewById(R.id.title);
    mBodyText = (EditText) findViewById(R.id.body);
    mDateButton = (Button) findViewById(R.id.reminder_date);
    mTimeButton = (Button) findViewById(R.id.reminder_time);

    mConfirmButton = (Button) findViewById(R.id.confirm);

    mRowId = savedInstanceState != null ? savedInstanceState.getLong(RemindersDbAdapter.KEY_ROWID) 
                                        : null;

    registerButtonListenersAndSetDefaultText();
}

另一个:

private void saveState() {
    String title = mTitleText.getText().toString();
    String body = mBodyText.getText().toString();

    SimpleDateFormat dateTimeFormat = new SimpleDateFormat(DATE_TIME_FORMAT); 
    String reminderDateTime = dateTimeFormat.format(mCalendar.getTime());

    if (mRowId == null) {

        long id = mDbHelper.createReminder(title, body, reminderDateTime);
        if (id > 0) {
            mRowId = id;
        }
    } else {
        mDbHelper.updateReminder(mRowId, title, body, reminderDateTime);
    }

    new ReminderManager(this).setReminder(mRowId, mCalendar); 
}

谢谢。

最佳答案

您应该检查提交按钮的点击监听器是否正确填写了所有必需的字段,如果没有,则仅显示 toast:

Toast.makeText(getApplicationContext(), "Title needs to be filled in!", 
    Toast.LENGTH_SHORT).show();

关于java - Android 文本验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5858491/

相关文章:

android - 如何设置 Wearable ListView 项目高度

android - Android 应用程序中的图像太多

android - android如何显示包含200个元素的列表?

java - 如何使用 Asynctask 将下载从主线程移动到后台线程

android - 火力基地 : What is the difference between setPersistenceEnabled and keepSynced?

java - 10 秒后删除 JLabel

java - 在netty中的单例类中使用threadlocal

java - 添加前一个元素在 ArrayList 中被替换

java - 输入流上的 JAX-RS 自定义范围

java - 自定义字体为 Android 中的某些字母组合提供了不正确的字形