[英]android edittext minmum and maximum value
我正在開發一個Android應用程序..實際上我需要設置一個editext條目的最小值和最大值我的最小值是18,最大值是65.I做了這個的確切代碼
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")});
我只從這個網站獲得此代碼... 有沒有辦法在Android中為EditText定義最小值和最大值? 實際上這對於1到12之間的值,並且對於該值它工作正常,但是當我改變我的值18和45時它不起作用...任何人都可以幫助我...我必須為此做什么改變。 ..
您應該替換該行:
int input = Integer.parseInt(dest.toString() + source.toString());
同
int input = Integer.parseInt(source.toString());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.