IconEditText提供一个可复用的带图标的EditText:GitHub。
使用简单,只需克隆这个 repo并把它作为一个Library添加到项目中。
添加了library之后,你就可以如下添加一个IconEditText了:
<!-- Note the declaration of the `widget` namespace. --> <LinearLayout ... xmlns:widget="http://schemas.android.com/apk/res-auto"> <com.kylewbanks.android.iconedittext.IconEditText android:layout_width="match_parent" android:layout_height="wrap_content" widget:isPassword="false" widget:hint="@string/username" widget:iconSrc="@drawable/username_icon" /> </LinearLayout>
IconEditText目前支持以下属性:
isPassword {Boolean}: 如果为真,输入将隐藏。
hint {String}: 要显示的提示文字,如果有的话。
iconSrc {Drawable}: 图标资源。
在Java中,你可以像任意View一样的引用IconEditText,举个例子,从Activity中:
IconEditText iconEditText = (IconEditText) findViewById(...);
你还可以像下面这样去获取控件里面的EditText和ImageView:
EditText editText = iconEditText.getEditText(); ImageView imageView = iconEditText.getImageView();
获取EditText的Editable的方法:
Editable editable = iconEditText.getText();