web-dev-qa-db-fra.com

Utilisez declare styleable pour définir le type d'entrée de composant personnalisé

J'ai un CompositeComponent (EditText + ImageButton) En cliquant sur le bouton, le contenu d'edittext sera effacé . Il fonctionne bien Mon problème est de définir des attributs pour mon composant. J'utilise declare-styleable pour définir les attributs de mon composant. 

Je réussis à définir minLines, maxLines et textColor.

Comment définir le type d'entrée sur mon composant via XML?.

mes attributs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="CET">
        <attr name="MaxLines" format="integer"/>
        <attr name="MinLines" format="integer"/>
        <attr name="TextColor" format="color"/>
        <attr name="InputType" format="integer" />
        <attr name="Hint" format="string" />
    </declare-styleable>
</resources>

Et utilisation de mycomponent dans main_layout.xml:

<com.test.ui.ClearableEditText
        xmlns:cet="http://schemas.Android.com/apk/res/com.test.ui"
        Android:id="@+id/clearableEditText2"
        Android:layout_width="wrap_content"
        Android:layout_height="wrap_content"
        cet:MaxLines="2"
        cet:MinLines="1"
        cet:TextColor="#0000FF"
        cet:InputType="" <---I cant set this property--------->
        cet:Hint="Clearable EditText Hint">

    </com.test.ui.ClearableEditText>

Utilisation ordinaire d'Edittext:

<EditText
        Android:id="@+id/editText1"
        Android:layout_width="match_parent"
        Android:layout_height="wrap_content"
        Android:inputType="numberSigned" <--------I want to use this property--------> >

Je ne peux pas utiliser ENUM dans mon attribut.xml . Comment faire référence à Android:inputType="numberSigned" dans mon cet:InputType?

MODIFIER:

C’est ainsi que j’assigne des propriétés dans mon fichier ClearableEditText.Java.

TypedArray a = getContext().obtainStyledAttributes(attrs,R.styleable.CET,0, 0);

            int minLines = a.getInt(R.styleable.CET_MinLines, 1);
            int maxLines = a.getInt(R.styleable.CET_MaxLines, 100);
            String hint = a.getString(R.styleable.CET_Hint);
            int textColor = a.getColor(R.styleable.CET_TextColor, Color.BLACK);
            int inputType = a.getInt(R.styleable.CET_InputType, -108);

            Log.i(TAG, "ClearableEditText: Min Line "+minLines +" Max Lines: "+maxLines+" Hint "+hint+" Color: "+textColor+" Input Type: "+inputType);

            edit_text.setMaxLines(maxLines);
            edit_text.setMinLines(minLines);
            edit_text.setTextColor(textColor);
            edit_text.setHint(hint);
            if(inputType != -108)
                edit_text.setInputType(inputType);

Vous pouvez constater qu'il n'y a pas de problème d'affectation de la propriété inputType à editText.

34
mahe madhi

Disons que vous avez une vue personnalisée appelée InputView, qui n'est pas une vue TextView (disons que c'est un RelativeLayout).

Dans votre attrs.xml:

<declare-styleable name="InputView">

    <!-- any custom attributes -->
    <attr name="title" format="string" /> 

    <!-- standart attributes, note Android: prefix and no format attribute -->
    <attr name="Android:imeOptions"/> 
    <attr name="Android:inputType"/>

</declare-styleable>

Dans une mise en page XML où vous souhaitez inclure InputView:

<!-- note xmlns:custom and com.mycompany.myapp -->
<FrameLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
    xmlns:custom="http://schemas.Android.com/apk/res-auto"
    Android:layout_width="fill_parent"
    Android:layout_height="fill_parent">

        <!-- note that you will be using Android: prefix for standart attributes, and not custom: prefix -->
        <!-- also note that you can use standart values: actionNext or textEmailAddress -->
        <com.mycompany.myapp.InputView
        Android:id="@+id/emailField"
        Android:layout_width="fill_parent"
        Android:layout_height="wrap_content"
        custom:title="@string/my_title"
        Android:imeOptions="actionNext|flagNoExtractUi"
        Android:inputType="textEmailAddress" />

</FrameLayout>

Dans votre classe personnalisée, vous pouvez extraire les attributs comme d'habitude:

    ...

    private String title;
    private int inputType;
    private int imeOptions;

    ...
    TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.InputView);

    int n = a.getIndexCount();
    for (int i = 0; i < n; i++) {
        int attr = a.getIndex(i);
        switch (attr) {
        case R.styleable.InputView_title:
            title = a.getString(attr);
            break;
        //note that you are accessing standart attributes using your attrs identifier
        case R.styleable.InputView_Android_inputType:
            inputType = a.getInt(attr, EditorInfo.TYPE_TEXT_VARIATION_NORMAL);
            break;
        case R.styleable.InputView_Android_imeOptions:
            imeOptions = a.getInt(attr, 0);
            break;
        default:
            Log.d("TAG", "Unknown attribute for " + getClass().toString() + ": " + attr);
            break;
        }
    }

    a.recycle();
    ...
73
Alexey

utilisez ceci dans votre attr.xml pour les suggestions. l'utilisation d'indicateurs donne les suggestions au format xml lors de l'utilisation de edittext . custom: inputType = "text" et il vous proposera les types d'addition . pour ajouter d'autres indicateurs utilisez ce lien. https://Android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/res/res/values/attrs.xml

<resources>

<declare-styleable name="ClearableEditText">
    <attr name="hintText" format="string" />
    <attr name="inputType" format="integer">

        <!-- There is no content type. The text is not editable. -->
        <flag name="none" value="0x00000000" />
        <!--
Just plain old text. Corresponds to
{@link Android.text.InputType#TYPE_CLASS_TEXT} |
{@link Android.text.InputType#TYPE_TEXT_VARIATION_NORMAL}.
        -->
        <flag name="text" value="0x00000001" />
        <!--
Can be combined with <var>text</var> and its variations to
request capitalization of all characters. Corresponds to
{@link Android.text.InputType#TYPE_TEXT_FLAG_CAP_CHARACTERS}.
        -->
    </attr>
  </declare-styleable>

</resources>
5
Hemant Shori

J'ai réussi à le faire avec une valeur int brute: Je pense que ce n'est pas une bonne pratique . Je peux attribuer des valeurs brutes comme ceci: cet:InputType="2"

2 pour numéro (référencé pour les valeurs: http://developer.Android.com/reference/Android/text/InputType.html#TYPE_CLASS_NUMBER et http://developer.Android.com/reference/Android/ R.styleable.html # TextView_inputType )

Je crois que <attr name="InputType" format="reference" /> peut aider mais je ne sais pas comment l'utiliser.

0
mahe madhi

Bon, alors je sais que cette question est assez ancienne mais j’ai trouvé la vraie réponse.

J'ai trouvé le fichier attrs.xml dans ici qu'Android utilise et que chaque declare_styleable est présent. Vous pouvez donc trouver l'attribut inputType défini ci-dessous à partir des attributs standard.

<!-- The type of data being placed in a text field used to help an
         input method decide how to let the user enter text.  The constants
         here correspond to those defined by
         {@link Android.text.InputType}.  Generally you can select
         a single value, though some can be combined together as
         indicated.  Setting this attribute to anything besides
         <var>none</var> also implies that the text is editable. -->
    <attr name="inputType">
        <!-- There is no content type.  The text is not editable. -->
        <flag name="none" value="0x00000000" />
        <!-- Just plain old text.  Corresponds to
             {@link Android.text.InputType#TYPE_CLASS_TEXT} |
             {@link Android.text.InputType#TYPE_TEXT_VARIATION_NORMAL}. -->
        <flag name="text" value="0x00000001" />
        <!-- Can be combined with <var>text</var> and its variations to
             request capitalization of all characters.  Corresponds to
             {@link Android.text.InputType#TYPE_TEXT_FLAG_CAP_CHARACTERS}. -->
        <flag name="textCapCharacters" value="0x00001001" />
        <!-- Can be combined with <var>text</var> and its variations to
             request capitalization of the first character of every Word.  Corresponds to
             {@link Android.text.InputType#TYPE_TEXT_FLAG_CAP_WORDS}. -->
        <flag name="textCapWords" value="0x00002001" />
        <!-- Can be combined with <var>text</var> and its variations to
             request capitalization of the first character of every sentence.  Corresponds to
             {@link Android.text.InputType#TYPE_TEXT_FLAG_CAP_SENTENCES}. -->
        <flag name="textCapSentences" value="0x00004001" />
        <!-- Can be combined with <var>text</var> and its variations to
             request auto-correction of text being input.  Corresponds to
             {@link Android.text.InputType#TYPE_TEXT_FLAG_AUTO_CORRECT}. -->
        <flag name="textAutoCorrect" value="0x00008001" />
        <!-- Can be combined with <var>text</var> and its variations to
             specify that this field will be doing its own auto-completion and
             talking with the input method appropriately.  Corresponds to
             {@link Android.text.InputType#TYPE_TEXT_FLAG_AUTO_COMPLETE}. -->
        <flag name="textAutoComplete" value="0x00010001" />
        <!-- Can be combined with <var>text</var> and its variations to
             allow multiple lines of text in the field.  If this flag is not set,
             the text field will be constrained to a single line.  Corresponds to
             {@link Android.text.InputType#TYPE_TEXT_FLAG_MULTI_LINE}. -->
        <flag name="textMultiLine" value="0x00020001" />
        <!-- Can be combined with <var>text</var> and its variations to
             indicate that though the regular text view should not be multiple
             lines, the IME should provide multiple lines if it can.  Corresponds to
             {@link Android.text.InputType#TYPE_TEXT_FLAG_IME_MULTI_LINE}. -->
        <flag name="textImeMultiLine" value="0x00040001" />
        <!-- Can be combined with <var>text</var> and its variations to
             indicate that the IME should not show any
             dictionary-based Word suggestions.  Corresponds to
             {@link Android.text.InputType#TYPE_TEXT_FLAG_NO_SUGGESTIONS}. -->
        <flag name="textNoSuggestions" value="0x00080001" />
        <!-- Text that will be used as a URI.  Corresponds to
             {@link Android.text.InputType#TYPE_CLASS_TEXT} |
             {@link Android.text.InputType#TYPE_TEXT_VARIATION_URI}. -->
        <flag name="textUri" value="0x00000011" />
        <!-- Text that will be used as an e-mail address.  Corresponds to
             {@link Android.text.InputType#TYPE_CLASS_TEXT} |
             {@link Android.text.InputType#TYPE_TEXT_VARIATION_EMAIL_ADDRESS}. -->
        <flag name="textEmailAddress" value="0x00000021" />
        <!-- Text that is being supplied as the subject of an e-mail.  Corresponds to
             {@link Android.text.InputType#TYPE_CLASS_TEXT} |
             {@link Android.text.InputType#TYPE_TEXT_VARIATION_EMAIL_SUBJECT}. -->
        <flag name="textEmailSubject" value="0x00000031" />
        <!-- Text that is the content of a short message.  Corresponds to
             {@link Android.text.InputType#TYPE_CLASS_TEXT} |
             {@link Android.text.InputType#TYPE_TEXT_VARIATION_SHORT_MESSAGE}. -->
        <flag name="textShortMessage" value="0x00000041" />
        <!-- Text that is the content of a long message.  Corresponds to
             {@link Android.text.InputType#TYPE_CLASS_TEXT} |
             {@link Android.text.InputType#TYPE_TEXT_VARIATION_LONG_MESSAGE}. -->
        <flag name="textLongMessage" value="0x00000051" />
        <!-- Text that is the name of a person.  Corresponds to
             {@link Android.text.InputType#TYPE_CLASS_TEXT} |
             {@link Android.text.InputType#TYPE_TEXT_VARIATION_PERSON_NAME}. -->
        <flag name="textPersonName" value="0x00000061" />
        <!-- Text that is being supplied as a postal mailing address.  Corresponds to
             {@link Android.text.InputType#TYPE_CLASS_TEXT} |
             {@link Android.text.InputType#TYPE_TEXT_VARIATION_POSTAL_ADDRESS}. -->
        <flag name="textPostalAddress" value="0x00000071" />
        <!-- Text that is a password.  Corresponds to
             {@link Android.text.InputType#TYPE_CLASS_TEXT} |
             {@link Android.text.InputType#TYPE_TEXT_VARIATION_PASSWORD}. -->
        <flag name="textPassword" value="0x00000081" />
        <!-- Text that is a password that should be visible.  Corresponds to
             {@link Android.text.InputType#TYPE_CLASS_TEXT} |
             {@link Android.text.InputType#TYPE_TEXT_VARIATION_VISIBLE_PASSWORD}. -->
        <flag name="textVisiblePassword" value="0x00000091" />
        <!-- Text that is being supplied as text in a web form.  Corresponds to
             {@link Android.text.InputType#TYPE_CLASS_TEXT} |
             {@link Android.text.InputType#TYPE_TEXT_VARIATION_WEB_EDIT_TEXT}. -->
        <flag name="textWebEditText" value="0x000000a1" />
        <!-- Text that is filtering some other data.  Corresponds to
             {@link Android.text.InputType#TYPE_CLASS_TEXT} |
             {@link Android.text.InputType#TYPE_TEXT_VARIATION_FILTER}. -->
        <flag name="textFilter" value="0x000000b1" />
        <!-- Text that is for phonetic pronunciation, such as a phonetic name
             field in a contact entry.  Corresponds to
             {@link Android.text.InputType#TYPE_CLASS_TEXT} |
             {@link Android.text.InputType#TYPE_TEXT_VARIATION_PHONETIC}. -->
        <flag name="textPhonetic" value="0x000000c1" />
        <!-- Text that will be used as an e-mail address on a web form.  Corresponds to
             {@link Android.text.InputType#TYPE_CLASS_TEXT} |
             {@link Android.text.InputType#TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS}. -->
        <flag name="textWebEmailAddress" value="0x000000d1" />
        <!-- Text that will be used as a password on a web form.  Corresponds to
             {@link Android.text.InputType#TYPE_CLASS_TEXT} |
             {@link Android.text.InputType#TYPE_TEXT_VARIATION_WEB_PASSWORD}. -->
        <flag name="textWebPassword" value="0x000000e1" />
        <!-- A numeric only field.  Corresponds to
             {@link Android.text.InputType#TYPE_CLASS_NUMBER} |
             {@link Android.text.InputType#TYPE_NUMBER_VARIATION_NORMAL}. -->
        <flag name="number" value="0x00000002" />
        <!-- Can be combined with <var>number</var> and its other options to
             allow a signed number.  Corresponds to
             {@link Android.text.InputType#TYPE_CLASS_NUMBER} |
             {@link Android.text.InputType#TYPE_NUMBER_FLAG_SIGNED}. -->
        <flag name="numberSigned" value="0x00001002" />
        <!-- Can be combined with <var>number</var> and its other options to
             allow a decimal (fractional) number.  Corresponds to
             {@link Android.text.InputType#TYPE_CLASS_NUMBER} |
             {@link Android.text.InputType#TYPE_NUMBER_FLAG_DECIMAL}. -->
        <flag name="numberDecimal" value="0x00002002" />
        <!-- A numeric password field.  Corresponds to
             {@link Android.text.InputType#TYPE_CLASS_NUMBER} |
             {@link Android.text.InputType#TYPE_NUMBER_VARIATION_PASSWORD}. -->
        <flag name="numberPassword" value="0x00000012" />
        <!-- For entering a phone number.  Corresponds to
             {@link Android.text.InputType#TYPE_CLASS_PHONE}. -->
        <flag name="phone" value="0x00000003" />
        <!-- For entering a date and time.  Corresponds to
             {@link Android.text.InputType#TYPE_CLASS_DATETIME} |
             {@link Android.text.InputType#TYPE_DATETIME_VARIATION_NORMAL}. -->
        <flag name="datetime" value="0x00000004" />
        <!-- For entering a date.  Corresponds to
             {@link Android.text.InputType#TYPE_CLASS_DATETIME} |
             {@link Android.text.InputType#TYPE_DATETIME_VARIATION_DATE}. -->
        <flag name="date" value="0x00000014" />
        <!-- For entering a time.  Corresponds to
             {@link Android.text.InputType#TYPE_CLASS_DATETIME} |
             {@link Android.text.InputType#TYPE_DATETIME_VARIATION_TIME}. -->
        <flag name="time" value="0x00000024" />
    </attr>

Donc vous avez 2 choix, si vous devez changer le nom de inputType en un autre nom (i.e title_inputType), alors vous devez copier tout cela, mais si vous devez conserver le même nom, utilisez/ Réponse d'Alexey

0
kyay