用java写一个检验类:1、 第一个edittext最多只能两位小数,且数值大于20不超过600;2、第二个edittext限制输入三位整数大于100且小于240;
时间: 2024-01-22 17:17:23 浏览: 108
可以参考以下代码,其中假设第一个EditText的id是editText1,第二个EditText的id是editText2:
```java
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
public class InputChecker {
public static void checkEditText1(final EditText editText1) {
editText1.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {}
@Override
public void afterTextChanged(Editable s) {
String str = s.toString();
if (str.equals(".") || str.startsWith("00") || str.startsWith("0.") || str.contains(".")) {
// 不允许输入多个小数点或小数点前面有多个0
editText1.setText(str.substring(0, str.length() - 1));
editText1.setSelection(editText1.getText().length());
return;
}
double value = Double.parseDouble(str);
if (value < 20 || value > 600) {
// 数值不在20-600之间
editText1.setText("20.00");
editText1.setSelection(editText1.getText().length());
} else {
// 保留两位小数
editText1.setText(String.format("%.2f", value));
editText1.setSelection(editText1.getText().length());
}
}
});
}
public static void checkEditText2(final EditText editText2) {
editText2.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {}
@Override
public void afterTextChanged(Editable s) {
String str = s.toString();
if (str.startsWith("0") || str.startsWith("1") || str.startsWith("2")) {
// 不允许输入以0、1、2开头的三位数
editText2.setText(str.substring(0, str.length() - 1));
editText2.setSelection(editText2.getText().length());
return;
}
int value = Integer.parseInt(str);
if (value < 100 || value > 240) {
// 数值不在100-240之间
editText2.setText("100");
editText2.setSelection(editText2.getText().length());
} else {
// 保留整数
editText2.setText(String.valueOf(value));
editText2.setSelection(editText2.getText().length());
}
}
});
}
}
```
在使用时,可以在Activity中调用InputChecker.checkEditText1(editText1)和InputChecker.checkEditText2(editText2)分别对两个EditText进行检验。例如:
```java
public class MainActivity extends AppCompatActivity {
private EditText editText1;
private EditText editText2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText1 = findViewById(R.id.editText1);
editText2 = findViewById(R.id.editText2);
InputChecker.checkEditText1(editText1);
InputChecker.checkEditText2(editText2);
}
}
```
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pptx](https://img-home.csdnimg.cn/images/20241231044947.png)