gpt4 book ai didi

java - Android EditText 检查帮助

转载 作者:行者123 更新时间:2023-12-01 15:24:20 24 4
gpt4 key购买 nike

我有一个 editText 字段,我想检查它是否符合某些条件,基本上它必须包含 2 个整数和 3 个字符串,如何在添加元素之前检查它。

 EditText.getText().toString();

我可以添加一张这样的支票吗

public boolean checkString(String StringPassed) {

String s = StringPassed;
if(s.length == 5){
boolean hasString = false;
boolean hasInt = false;
String letters = s.substring(0, 2);
Pattern p = Pattern.compile("[a-zA-Z]");
Matcher m = p.matcher(letters);
if (m.matches()) {
hasString = true;
}
String numbers=s.substring(2,5);
try {
int num = Integer.parseInt(numbers);
String n = num + "";
if (num > 0 && n.length() == 3)
hasInt = true;
} catch (Exception e) {
}
if (hasInt && hasString) {
return true;
}
}else{
return false;
}
return false;
}

然后我有一个方法会说

public void addString() {

String StringPassed = EditTextName.getString().toString();
checkString(String StringPassed);

if (StringPassed() == false) {
Display Toast;
}
else {
add;
}
}

最佳答案

    String s = edittext.gettext();
if(s.length == 5){
boolean hasString = false;
boolean hasInt = false;
String letters = s.substring(0, 2);
Pattern p = Pattern.compile("^[a-zA-Z]+$");
Matcher m = p.matcher(letters);
if (m.matches()) {
hasString = true;
}
String numbers=s.substring(2,5);
try {
int num = Integer.parseInt(numbers);
String n = num + "";
if (num > 0 && n.length() == 3)
hasInt = true;
} catch (Exception e) {
}
if (hasInt && hasString) {
//success
}
}else{
// incorrect string
}

您还可以在 edittext 上添加一个 TextWatcher 来监听文本输入更改并自动调用您的方法

关于java - Android EditText 检查帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10398445/

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