【问题标题】:Use declare styleable to set custom component input type使用 declare styleable 设置自定义组件输入类型
【发布时间】:2012-04-01 17:26:43
【问题描述】:

我有一个 CompositeComponent (EditText+ImageButton) 单击按钮时,edittext 内容将被清除。 它工作正常。我的问题是为我的组件设置属性。我正在使用 declare-styleable 为我的组件设置属性。

我成功设置了 minLines、maxLines 和 textColor。

如何通过 xml 将 inputtype 设置为我的组件。

我的属性.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>

以及main_layout.xml中mycomponent的用法:

<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>

普通的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--------> >

我不能在我的 attribute.xml 中使用 ENUM。 如何在我的cet:InputType 中引用android:inputType="numberSigned"

编辑:

这就是我在 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);

您可以看到将 inputType 属性分配给 editText 没有问题。

【问题讨论】:

  • 我没有收到任何错误。它工作正常。我不知道如何分配输入类型??而不是给出原始整数值,我想在android中使用枚举(android:inputtype)。

标签: android custom-component attr


【解决方案1】:

好的,我知道这个问题已经很老了,但我找到了真正的正确答案。

我在here找到了Android使用的attrs.xml文件,每个declare_styleable都在那里,所以从标准属性中你可以找到如下定义的inputType属性

<!-- 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>

所以你有2个选择,如果你需要将inputType的名称更改为另一个名称(即title_inputType),那么你需要将所有这些复制过来,但如果你需要保持相同的名称,那么使用Alexey's answer

【讨论】:

    【解决方案2】:

    在 attr.xml 中使用它作为建议。在使用edittext时,标志的使用在xml中给出建议。 自定义:inputType="text" 它会建议您添加类型。 添加更多标志使用此链接 访问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>
    

    【讨论】:

      【解决方案3】:

      假设您有一个名为 InputView 的自定义视图,它不是 TextView(假设它是 RelativeLayout)。

      在你的 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>
      

      在要包含 InputView 的 xml 布局中:

      <!-- 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>
      

      在您的自定义类中,您可以像往常一样提取属性:

          ...
      
          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();
          ...
      

      【讨论】:

      • 我知道这是一篇很老的帖子,但我想知道是否有办法让 eclipse 像编辑文本一样显示建议?
      • 我无法提出 Eclipse 节目建议。也许它会在 adt 的未来更新中起作用。
      • 此代码有效吗?因为我做了这里写的所有事情,但是在我的自定义类中我无法获得 inputType 属性。无法解决这一行:case R.styleable.InputView_android_inputType:
      • 检查 attrs.xml 在 InputView 元素中是否包含 标记,并且您导入自己的 R 文件,而不是 android.R 文件。
      • 谢谢@Alexey,我有一个小错误
      【解决方案4】:

      我设法用原始 int 值做到了: 我认为这不是好的做法。 我可以像这样分配原始值:cet:InputType="2"

      2 代表数字(参考值:http://developer.android.com/reference/android/text/InputType.html#TYPE_CLASS_NUMBERhttp://developer.android.com/reference/android/R.styleable.html#TextView_inputType

      我相信&lt;attr name="InputType" format="reference" /&gt;可以帮到你,但不知道怎么用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-08-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-24
        • 2011-09-22
        • 1970-01-01
        相关资源
        最近更新 更多