2012-06-01 151 views
2

我動態構建EditText。除其他外,我設置了2個屬性:hint(.setHint)和inputType(.setInputType)。我的問題:當我調用setInputType,setHint沒有效果:空白edittexts保持空白,沒有提示。一旦我註釋掉setInputType,我會看到所有提示。我需要輸入類型和提示。該怎麼辦?我的代碼:setHint does not work with setInputType

private EditText buildTextBox(Property property) 
{ 
    EditText control = new EditText(this); 
    control.setInputType(getInputTypeByPropertyInputType(property.getType()));// android.text.InputType. 
    control.setHint(property.getDisplayName()); 
    return control; 
} 

private int getInputTypeByPropertyInputType(String type) 
{ 
    if (type.equals("integer")) 
    { 
     return android.text.InputType.TYPE_CLASS_NUMBER; 
    } 
    else 
    { 
     return android.text.InputType.TYPE_CLASS_TEXT; 
    } 
} 
+1

我調用的方法之一是control.setGravity(Gravity.CENTER) - 一旦我刪除了這個調用,我就能看到所有的提示。我不能解釋它爲什麼會發生,並且我認爲這是非常奇怪的行爲。 –

回答

2

@Eugene 確保您撥打control.SetHint()調用control.setGravity()和control.setInputType()之前;它對我來說很有用!

  column1 = new EditText(this); 
      column1.setId(i); 
      column1.setHint("Value"); 
      column1.setInputType(InputType.TYPE_CLASS_NUMBER); 
      column1.setGravity(Gravity.RIGHT); 
0

我同意尤金。 消除重力(只是不要使用CENTER),提示文本將恢復正常。 很好找!