2013-09-16 40 views
2

如何爲EditText控件在其inputType爲數字時以編程方式設置文本?當inputType爲數字時,如何爲EditText控件設置文本

<EditText 
    android:id="@+id/WorkingHoursET" 
    android:layout_width="fill_parent" 
    android:textColor="@color/white" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/WorkingHoursTV" 
    android:layout_below="@+id/WorkingHoursTV" 
    android:layout_marginTop="14dp" 
    android:ems="10" 
    android:inputType="number" /> 


    EditText workedHours = (EditText)findViewById(R.id.WorkedHoursET); 
    workedHours.setText(8); //this is not working 
    workedHours.setText("8"); //neither this 
+0

什麼是錯誤? –

+3

我想這是因爲你通過錯誤的ID WorkedHoursET insteaD OF WorkingHoursET –

+0

是的,你是對的:)非常感謝 – Glolita

回答

4

workedHours.setText("8", BufferType.EDITABLE); 

嘗試,如果你檢查文檔的EditText,你會發現一個setText()方法。它需要在StringTextView.BufferType


注:作爲賽倫德拉·辛格Rajawat注意您可能使用的EditText的錯誤ID。你也應該看看。該代碼應該是

EditText workedHours = (EditText)findViewById(R.id.WorkingHoursET);

相關問題