gpt4 book ai didi

java - Android 文本验证

转载 作者:行者123 更新时间:2023-12-01 05:46:16 27 4
gpt4 key购买 nike

您好,我正在创建一个任务提醒应用程序。它有效,但我想创建某种 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com