![](/img/trans.png)
[英]How to use a regular expression and assign the result to variables in Android?
[英]How to use regular expression in Android
我有一個numberDecimal EditText
,我想用正則表達式驗證。 在驗證中我想要的是:
在小數點之前,我要輸入的最大數字是3,數字不應該像零點2,23,342
等一樣2,23,342
。
小數點后,我要輸入的最大數字是.1
, .3
, .6
等。
所以,我允許用戶輸入的數字就像是2.1
, 32.5
, 444.8
, 564.9
,等等。
但在我的代碼中,會發生什么:
它允許用戶小數點像以前一樣進入超過三位數以上3456
, 4444
, 5555
之后,它不會讓我以后輸入小數點。
它允許我在小數點前輸入0
作為數字的開頭。
那么為什么會發生這種情況,我使用的正則表達式有什么不對嗎? 如果有人知道,請幫我解決這個問題。
我用過的代碼:
weightEditText.addTextChangedListener(new TextWatcher()
{
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s)
{
Pattern mPattern = Pattern.compile("^([1-9][0-9]{0,2})?(\\.[0-9]?)?$");
Matcher matcher = mPattern.matcher(s.toString());
if(!matcher.find())
{
weightEditText.setText(); // Don't know what to place
}
}
});
在InputFilter
單獨檢查dest
是沒有意義的; 這就是現場已經存在的東西。 將正則表達式匹配更改為source
,如果您只想檢查字段中是否接受了某些字符,那么它是合適的。 但是,您要檢查字段格式,而不僅僅是逐個字符地過濾輸入。 這要復雜得多。
每次用戶更改tempEditText
的內容時,系統都會在實際進行更改之前調用過濾器的filter
方法。 它傳遞當前字段內容和建議的更改(可以插入/追加,刪除或替換)。 更改由源CharSequence source
(字符 - 如果有任何 - 要添加到字段),源中的范圍開始和結束索引(范圍不一定是所有source
), Spanned dest
(當前字段)表示更改前的內容)和dest
中的dstart和dend索引,建議由指定的source
范圍替換。
filter
的工作是修改更改(如果需要)並返回CharSequence
以使用(完整地)代替source
(或使用null
來繼續使用source
)。 您需要檢查更改是否會導致可接受的字段,而不是像您現在一樣檢查dest
。 要做到這一點,您將需要更復雜的邏輯。 (特別注意,新字符可能用於插入到最后的某個位置;同樣,當用戶刪除字符以及添加字符時,將調用filter
。)
實現TextWatcher
可能更容易。 在它的beforeTextChanged
方法中,您可以記錄當前內容,在其中使用afterTextChanged
方法,您可以檢查(使用正則表達式)內容是否可接受,如果不是,則恢復更改前的內容。 (但請確保更改前的文本是可以接受的。如果不是,請替換可接受的內容 - 比如清除字段。否則您的代碼將進入無限循環,因為當您使用TextWatcher
將再次調用糾正字段內容。)
您的正則表達式中也有錯誤:它允許前導零。 這是一個修復此問題的改進版本(並刪除了一組不必要的括號):
"^([1-9][0-9]{0,2})?(\\.[0-9]?)?$"
(順便說一句:您可以使用\\\\d
而不是[0-9]
。)
編輯
這是我對您的編輯的編輯:
weightEditText.addTextChangedListener(new TextWatcher()
{
private static final Pattern sPattern
= Pattern.compile("^([1-9][0-9]{0,2})?(\\.[0-9]?)?$");
private CharSequence mText;
private boolean isValid(CharSequence s) {
return sPattern.matcher(s).matches();
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count){
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after){
mText = isValid(s) ? new CharSequence(s) : "";
}
@Override
public void afterTextChanged(Editable s)
{
if (!isValid(s))
{
weightEditText.setText(mText);
}
mText = null;
}
});
也許我的實施會幫助任何人:
etRepeaterCallsign.addTextChangedListener(new TextWatcher() {
private final Pattern sPattern
= Pattern.compile("^([A-Z]{0,2})?(\\d)?([A-Z-]{0,5})"); // ^([1-9][0-9]{0,2})?(\\.[0-9]?)?$
private CharSequence mText;
private boolean isValid(CharSequence s) {
return sPattern.matcher(s).matches();
}
@Override
public void beforeTextChanged(CharSequence r, int start, int count,
int after) {
mText = r.toString();
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
bIsEdit = true;
}
@Override
public void afterTextChanged(Editable s) {
etRepeaterCallsign.removeTextChangedListener(this);
int iCursorPosition = etRepeaterCallsign.getSelectionStart();
etRepeaterCallsign.setText("");
if (isValid(s))
etRepeaterCallsign.append(s);
else
etRepeaterCallsign.append(mText);
etRepeaterCallsign.setSelection(iCursorPosition);
etRepeaterCallsign.addTextChangedListener(this);
}
});
您可以解析數字並檢查它是否<1000並且該10 *數字是整數而num不是。 它可能更具可讀性。
你可以嘗試這樣的事情: ^[1-9][0-9]{0,2}(\\\\.\\\\d)?$
。 這應匹配任何不以0開頭的數字( ^[1-9]
),后跟最多兩個數字( [0-9]{0,2}
)。 正則表達式還允許可選的十進制值( (\\\\.\\\\d)?$
)。
話雖如此,理想情況下,您應該將字符串值解析為double
或float
並使用實際數值進行需要進行的驗證。
要將string
解析為double
值,您需要使用Double.parseDouble(String s),如下所示: double myValue = Double.parseDouble(EditText.getText());
我使用我的代碼嘗試了你的模式如下。 它完美地工作為2.1,32.5,444.8,564.9等。
我的代碼:
public class WebPush extends Activity {
EditText editTxt;
private TextView regresult;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
editTxt = (EditText) findViewById(R.id.editID);
regresult = (TextView) findViewById(R.id.txtID);
String urName = editTxt.getText().toString();
editTxt.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,int arg3) {
}
@Override
public void afterTextChanged(Editable s) {
if (editTxt.getText().toString().matches("(^([0-9]{0,3})?)(\\.[0-9]{0,1})?$"))
{
regresult.setText("");
}
else
{
regresult.setText("invalid number");
}
}
});
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.