开发者

Android Text validation

Hi I am creating a task reminder app. It works but I would like to create some sort of Toast validation. For example a u开发者_如何学Cser doesn't fill in the title and I'd like an toast saying "title needs to be filled in!" e.t.c But I'm not sure how to do this.

I am using an EditText widget by the way.

This is one method:

  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();
}

and another:

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); 
}

Thanks.


You should check inside your submit button's click listener whether all the necessary fields are filled in properly, and if not, just show up the toast:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜