[英]Check if EditText input matches SimpleDateFormat Android
[英]Android check if text input (time) matches the format
我有兩個輸入時間為(mm:ss)的文本字段,如何創建檢查(使用Toast,我知道如何制作xD),以查看其是否與該格式匹配,如果不匹配則傳遞Toast ? 一直在嘗試,但是什么也沒做。
我不知道您是否還需要以上這些,但是這里是我通過它們的地方:
val time = findViewById<NumberPicker>(R.id.time) as TextView
val distance = findViewById<NumberPicker>(R.id.distance) as TextView
val pace = findViewById<NumberPicker>(R.id.pace) as TextView
calculate.setOnClickListener {
when {
time.text.isEmpty() && (distance.text.isNotEmpty() && pace.text.isNotEmpty()) -> calculatePace(null, distance.text.toString(), pace.text.toString())
distance.text.isEmpty() && (time.text.isNotEmpty() && pace.text.isNotEmpty()) -> calculatePace(time.text.toString(), null, pace.text.toString())
pace.text.isEmpty() && (time.text.isNotEmpty() && distance.text.isNotEmpty()) -> calculatePace(time.text.toString(), distance.text.toString(), null)
else -> {
Toast.makeText(this, "Please check all fields", Toast.LENGTH_SHORT).show()
}
}
}
這是否足夠,還是您需要在我使用它們的地方使用該功能? 謝謝
=========
編輯:
我試圖創建此功能,但是現在如何在這里傳遞我的編輯擴展名呢? 也正確嗎?
fun isTimeValid(time: String?): Boolean {
val expression = "^([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]\$"
val pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE)
val matcher = pattern.matcher(time)
return matcher.matches()
}
if (!Constants.isStringEmpty(str_time)&&isTimeValid(str_time)){
//// TODO: 10/6/2017
}else {
if (Constants.isStringEmpty(str_time)){
edt_email.requestFocus();
Toast.makeText(this, "Enter Time.", Toast.LENGTH_SHORT).show();
}
else if(!isTimeValid(str_time)){
edt_email.requestFocus();
Toast.makeText(this, "Enter Valid Time.", Toast.LENGTH_SHORT).show();
}
}
public static boolean isTimeValid(String time) {
boolean isValid = false;
String expression = "^([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$";
CharSequence inputStr = time;
Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(inputStr);
if (matcher.matches()) {
isValid = true;
}
return isValid;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.