2012-11-22 50 views
3

我已覆蓋EditText,如this link中給出的。獲取提示屬性來自AttributeSet的值

現在同時宣佈內部佈局這一領域,我使用

<com.and.ab1209.ClearableEditText 
android:id=」@+id/edit_text_clearable」 
android:layout_width=」fill_parent」 
android:hint="My Hint Goes here" 
android:layout_height=」wrap_content」 /> 

如何取回內的任何這些構造的這個提示值。

public ClearableEditText(Context context, AttributeSet attrs, int defStyle){...} 
public ClearableEditText(Context context, AttributeSet attrs){...} 

我該怎麼做?

回答

1

您無法訪問「android」屬性。調用super()構造函數後,您可以使用getHint()。 如果你想創建你自己的屬性,請按照this tutorial

+0

由於ClearableEditText擴展了RelativeLayout,我沒有getHint()方法可用。 –

+0

比你應該創建自己的屬性和使用它 – Jin35

+0

這是相當長的過程和更多的代碼,我不想跟隨,所以只有我問這個事情。 –

1

使用this.getHint()您的構造函數中

+0

由於ClearableEditText擴展了RelativeLayout,我沒有getHint()方法可用。 –

+0

哦,你必須擴展'EditText'而不是 –

+0

這是不可能的,因爲我將使用'RelativeLayout'作爲最外層'佈局'。如果是這種情況,那麼我爲什麼要自定義'EditText' –

4

您可以訪問標準的XML屬性做你的看法構造函數中執行以下操作:

final String xmlns="http://schemas.android.com/apk/res/android"; 
//If you had a background attribute this is the resource id 
int backgroundResource = attrs.getAttributeResourceValue(xmlns, "background", -1); 
//This is your views hint 
String hint = attrs.getAttributeValue(xmlns, "hint"); 

它不一樣,如果你認爲從TextView的繼承,如果你指定的事使用android:hint可以在您的自定義視圖中訪問該提示。