2015-03-25 36 views
0

這實際上是我家庭作業的一部分,我的朋友也做了類似的事情,但問題只出現在我身上。這是我的XML代碼:爲什麼我的EditText框允許使用加號

<EditText 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:inputType="numberDecimal|numberSigned" 
    android:id="@+id/sheepNumBox"/> 

出於某種原因,我可以鍵入「+」到的EditText框,同時它不應該,任何想法,爲什麼?

+0

明顯,因爲 '+' 是一個數字符號... – Selvin 2015-03-25 10:09:06

+0

從這個網站閱讀回答其他問題我瞭解它不應該啓用。我的朋友也有幾乎相同的XML代碼,只有我的允許'+'。 – tba 2015-03-25 10:17:51

+1

然後你明白它錯了...不信任android:inputType ... AFAIK它只是一個鍵盤提示(因爲你知道android用戶可以使用自定義鍵盤)不是過濾器...所以它取決於鍵盤使用用戶和一些鍵盤的實現可以認真對待它,有些不是... – Selvin 2015-03-25 10:21:25

回答

1

添加該代碼..

final EditText editText = findViewById(R.id.sheepNumBox); 
    editText.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) { 
      String text = editText.getText().toString().trim(); 
      if (text.contains("+")) { 
       text.replace("+", ""); 
       editText.settext(text); 
      } 
     } 

     @Override 
     public void afterTextChanged(Editable s) { 

     } 
    }); 
+0

首先即使這個解決方案的工作原理我想知道什麼可能是它在第一個地方啓用,而它不應該。 其次,這不起作用,因爲你沒有改變edittext中的實際文本,如果你這樣做,它可能會導致無限循環(這可以避免,但無論如何,這不是我正在尋找)。 – tba 2015-03-25 10:35:35

+0

查看更新的答案 – Sjd 2015-03-25 10:37:55

+0

更新的代碼導致無限循環和堆棧溢出。 – tba 2015-03-25 10:44:15

0

試試這個..

<EditText 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:inputType="numberDecimal" 
android:id="@+id/sheepNumBox"/> 
+0

這不是一個選項,因爲我需要能夠輸入負數。我希望它保持不變,但不能鍵入'+'。從閱讀本網站上的其他問題,這不應該默認啓用,我不知道它爲什麼在我的應用程序啓用。 – tba 2015-03-25 10:11:51

相關問題