gpt4 book ai didi

java - android edittext最小值和最大值

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:57:01 26 4
gpt4 key购买 nike

我正在开发一个 android 应用程序。实际上我需要为一个编辑文本条目设置最小值和最大值,我的最小值是 18,最大值是 65。我做了这个的确切代码

package com.test;

import android.text.InputFilter;
import android.text.Spanned;

public class InputFilterMinMax implements InputFilter {

private int min, max;

public InputFilterMinMax(int min, int max) {
this.min = min;
this.max = max;
}

public InputFilterMinMax(String min, String max) {
this.min = Integer.parseInt(min);
this.max = Integer.parseInt(max);
}

@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
try {
int input = Integer.parseInt(dest.toString() + source.toString());
if (isInRange(min, max, input))
return null;
} catch (NumberFormatException nfe) { }
return "";
}

private boolean isInRange(int a, int b, int c) {
return b > a ? c >= a && c <= b : c >= b && c <= a;
}
}




EditText et = (EditText) findViewById(R.id.myEditText);
et.setFilters(new InputFilter[]{ new InputFilterMinMax("1", "12")});

我只从这个网站得到这个代码...... Is there a way to define a min and max value for EditText in Android? 实际上,对于 1 到 12 之间的值,对于该值,它工作正常,但是当我将值更改为 18 和 45 时,它不起作用……任何人都可以帮助我……我必须为此做些什么改变。 ..

最佳答案

你应该替换行:

int input = Integer.parseInt(dest.toString() + source.toString());

int input = Integer.parseInt(source.toString());

Can someone help me with the parameters to the Android InputFilter "filter" method? (plus regex)明白为什么

关于java - android edittext最小值和最大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16621210/

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