嗨,我正在创建一个任务提醒应用程序。它可以工作,但是我想创建某种Toast验证。例如,用户没有填写标题,而我想吐司说“标题需要填写!”等等
但是我不确定该怎么做。

我正在使用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.makeText(getApplicationContext(), "Title needs to be filled in!",
    Toast.LENGTH_SHORT).show();

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

10-12 00:31
查看更多