自定义组合件之自定义inputType属性

本篇文章只是讲解在定义组合控件时怎么使用inputType属性的问题,自定义控件的思路不在本文章的讨论范围之内。

 

1、背景

       在开发自定义组合控件时,内部包含一个EditView。希望这个组合控件具备设置EditView的软件盘输入样式的功能。因此,组合控件需要通过自定义属性customInputType去设置EditView的inputType。由于组合控件继承的是RelativeLayout,不能直接使用android:inputType,因此,需要自定义customInputType属性,让它和EditView设置inputType属性一样。

 

2、组合控件的结构如下:

 

两个TextView,一个EditView和一个Button。

 

3、思路

       因为EditView有inputType属性的设置操作,并且提供了setInputType(int type)函数。因此,需要参考EditView源码的实现,看看是怎么自定义inputType属性的?EditView是怎么获取inputType属性并设置setInputType的?

3-1、怎么自定义inputType属性

        inputType属性的定义在frameworks/base/core/res/values/attrs,xml下

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

可以看出,inputType属性是flag,内部定义了各种flag。因此,我们的组合控件也可以按照这种方式,定义一个flag类型的customInputType属性,把inputType属性的枚举拷贝到customInputType属性下即可。如下:

<attr name="customInputType">

<flag name="text" value="0x00000001"/>

<flag name="number" value="0x00000002"/>

<flag name="numberDecimal" value="0x00002002"/>

</attr>

这里只是拷贝了几个自己项目需要的值,其他的其根据自己的需要拷贝。这些flag类型属性会在你编写布局xml时AS自动提示出来

提示

 

3-2、怎么获取inputType属性

flag类型的属性,可以通过TypedArray.getInt函数来获取。因此,在自定义组合控件的构造函数中这样操作:

//获取输入样式inputType的值

final int inputType = attributes.getInt(R.styleable.CustomCombineEditTextSetting_customInputType,

InputType.TYPE_CLASS_TEXT | InputType.TYPE_NUMBER_FLAG_DECIMAL);

mInput.setInputType(inputType);

attributes是TypedArray类型 ,mInput是组合控件中的EditView。

结语

  对于可可编辑控件的输入样式的控制,核心就是通过setInputType(int type)函数去设置,至于怎么获得setInputType(int type)的参数type每个人可能有不同方式,比如可以完全不用android:inputType赋值那种方式,直接可以将自定义的inputType设置成integer型,然后把InputType类的常量值设置进去也是可以的。只是这种可读性不强。
  以上思路仅供参考,如果有什么错误的地方,欢迎大家不吝赐教。如果大家有更好的方法,也请留言,大家一起进步,谢谢!

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值