簡體   English   中英

EditText 軟數字鍵盤有時不允許數字

[英]EditText soft numeric keyboard sometimes does not allow digits

以下代碼生成一個 EditText(目標版本 23)。 我已經為此工作了大約 8 個小時,並收到了一些建議,但我認為以前沒有人見過這種情況,所以我仍然被卡住了。

  1. 單擊該字段。
  2. A/N 軟鍵盤打開。
  3. 單擊 123? 左下角的按鈕。 數字軟鍵盤打開。
  4. 按任意數字。 沒發生什么事。
  5. 長按 5,“5/8”被添加到文本字段中。
  6. 按任何特殊字符,例如@。 它可能會添加到該領域。
  7. 清場。 輸入“for”,按123?,現在需要數字。
  8. 清場。 輸入“for?”,按123?,不會取數字。

我添加了一個 TextWatcher。 如果數字沒有發布,TextWatcher 也不會看到它們。

EditText bottomT = new EditText(model);
bottomT.setTextSize(14);
bottomT.setHint("ZIP");  
bottomT.setHintTextColor(Color.BLACK);
bottomT.setBackgroundColor(Color.WHITE);
bottomT.setTextColor(Color.BLACK);
// bottomT.setInputType(InputType.TYPE_CLASS_NUMBER)  Didn't make any difference.
// bottomT.setRawInputType(InputType.TYPE_CLASS_NUMBER)  Didn't make any difference.
// bottomT.setText("", TextView.BufferType.EDITABLE);  DIdn't make a difference
bottomT.setText(""); 

EditText 行為不端,因為在我的自定義 ViewGroup 中我有

 protected void onLayout(boolean changed, int l, int t, int r, int b)
{
....
     child.layout(child.getLeft(), child.getTop(),
                    child.getLeft() + child.getMeasuredWidth(),
                    child.getTop() + child.getMeasuredHeight());

     child.setRight(somevalue);   // CAUSES EDITTEXT PROBLEMS
     child.setBottom(somevalue);  // CAUSES EDITTEXT PROBLEMS

現在很清楚我不能 setRight() 和 setBottom(),但也很清楚 EditText 不應該變得奇怪。

忽略退格鍵。

隨機忽略數字鍵,但接受小數點。

忽略 newLine(Enter) 鍵

忽略或不忽略哪些鍵取決於設備。 三星 Tab 4 或 Nexus 5 API 23 X86 模擬器是查看此內容的好地方。

您必須在 Java 代碼中添加這一行。

bottomT.setInputType(EditorInfo.TYPE_CLASS_NUMBER);

試試這行代碼。

bottomT.setInputType(InputType.TYPE_CLASS_NUMBER |InputType.TYPE_NUMBER_FLAG_DECIMAL);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM