2011-05-02 48 views
1

嗨我正在創建一個任務提醒應用程序。它的工作原理,但我想創建一些Toast驗證。例如,用戶不填寫標題,我想要敬酒說:「標題需要填寫!」 e.t.c 但我不知道如何做到這一點。Android文本驗證

我正在使用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); 
} 

感謝。

回答

0

你應該檢查你的提交按鈕的點擊監聽器的所有必要的字段是否填寫正確地在裏面,如果沒有,只顯示了敬酒:

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