2016-04-22 11 views
1

Hallo Ich habe diese Verbindung Ansicht, die eine Textansicht enthält (zum Anzeigefehler oder Beratung) und Bearbeiten von Text (für die Eingabe)Android benutzerdefinierte zusammengesetzte Ansichten, wie Attribute wiederverwenden?

<TextView 
     android:id="@+id/guidanceOrError" 
     android:gravity="center" 
     android:padding="10dp" 
     android:text="Please input 6 characters and 1 number" 
     android:layout_marginBottom="10dp" 
     android:background="@drawable/input_guidance_background" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 

    <EditText 
     android:id="@+id/inputField" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@drawable/rectangle_border" 
     android:padding="@dimen/login_editText_padding" 
     tools:hint="@string/user_name"/> 

</merge> 

Und das ist ich es in einem Aktivitäts Layout mit

<com.ersen.test.widgets.ValidationInputField 
       android:id="@+id/password" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="@dimen/login_editText_top_margin" 
       android:hint="@string/password" 
       android:inputType="textPassword" /> 

Mein Problem ist, dass Attribute wie Hinweis und inputType ignoriert werden. Dies liegt daran, in meiner init(AttributeSet attrs) Methode, die ich bin nicht die Attribute aus immer

if(attrs != null){ 
      TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.???); 
      a.recycle(); 
     } 

Meine Frage ist, wie kann ich Attribute verwenden, die bereits existieren? Ich möchte nicht, neu erstellen sie

helfen Sie mit mir und vielen Dank für das Lesen

Edit 1 Ansicht Meine Verbindung erstreckt Linearlayout

Antwort

1

Raten Sie sprechen von einem CustomView.

Allerdings sollten Sie declare-styleable in attrs.xml und es wie folgt verwendet werden:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <declare-styleable name="ValidationInputField"> 
     <attr name="android:hint"/> 
     <attr name="android:inputType"/>    
    </declare-styleable> 
</resources> 

So bearbeiten Sie Ihre init Methode wie folgt:

if(attrs != null){ 
       TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.ValidationInputField); 
       String hint = a.getString(R.styleable.ValidationInputField_android_hint); 
       int inputType = a.getInt(R.styleable.ValidationInputField_android_inputType,0); 
       // set these two values in your EditText programmatically 
       EditText editText = (EditText) findViewById(R.id.inputField); 
       editText.setHint(hint); 
       editText.setInputType(inputType); 
       a.recycle(); 
       } 
Verwandte Themen