2016-07-05 48 views
-2

我有一個編輯文本,其中用戶輸入金額,我想比較不少於10的金額,如果發現少於10,那麼我想顯示吐司消息。我怎樣才能做到這一點?如何比較在android中edittext不少於10的字符串?

m_szAmount = m_InputAmount.getText().toString().trim(); 
        if (m_szAmount.equals(10)) { 
         m_InputAmount.setError("Please enter the amount between Rs. 10 and Rs. 1100"); 

        } else { 
         confirmationDialog(); 
        } 
+1

'INT m_szAmount =的Integer.parseInt(。m_InputAmount.getText()的toString()修剪());' 我希望你知道如何比較整數。 –

+0

首先從editext中獲取文本,然後將其轉換爲int,然後比較輸入的文本是否小於或等於10 –

+0

可能出現[如何在android中比較大於字符串的字符串]的副本(http://stackoverflow.com/問題/ 4092161 /如何,可以任您比較串功能於Android的使用,大於) –

回答

0

試試下面的代碼:

m_szAmount = m_InputAmount.getText().toString().trim(); 

if (Integer.valueOf(m_szAmount) <= 10){ 
    m_InputAmount.setError("Please enter the amount between Rs. 10 and Rs. 1100"); 

} else { 
    confirmationDialog(); 
} 
0

試試這個:

m_szAmount = m_InputAmount.getText().toString().trim(); 
try 
{ 
       if (Integer.parseInt(m_szAmount)>10) { 
        m_InputAmount.setError("Please enter the amount between Rs. 10 and Rs. 1100"); 

       } else { 
        confirmationDialog(); 
       } 
    } 
    catch (NumberFormatException e) { 
    m_InputAmount.setError("Please enter Number"); 
    } 

,如果有可能的用戶在你的EDITTEXT輸入不是數字,重要的是要使用嘗試捕捉而使用Integer.parseInt(),因爲如果您將其用於非數字字符串,它可能會引發異常。

0

嘗試:

m_szAmount = Integer.parseInt(m_InputAmount.getText().toString()); 
if(m_szAmount <= 10) 
{ 

//show yout Toast 
} 
else 
{ 

//do your work 
} 
0

嘗試:

String m_szAmount = m_InputAmount.getText().toString(); 
    if(m_szAmount.replace(" ","").length() > 0) { 
     int inputAmount = Integer.valueOf(m_szAmount); 
     if (inputAmount >= 10){ 
      m_InputAmount.setError("Please enter the amount between Rs. 10 and Rs. 1100"); 
      m_InputAmount.requestFocus(); 
     }else{ 
      confirmationDialog();  
     } 
    } 
0
TextWatcher textWatcher = 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) { 
        if(String.valueOf(s.length()) <= 10){ 
         Toast.makeText(getContext(), "your text",Toast.LENGTH_LONG).show(); 
        } 

       } 

       @Override 
       public void afterTextChanged(Editable s) { 

       } 
      }; 
      edit_explain.addTextChangedListener(textWatcher);