Android 自定义控件之inflate()

来源:互联网 发布:javascript是什么知乎 编辑:程序博客网 时间:2024/06/10 18:03

Android 自定义控件之inflate()

原文地址:http://my.oschina.net/superbigfu/blog/185740

用到的图片文件:

平时都是用简单的控件通过布局来组合一大堆控件,界面复杂起来的话,布局文件就很大,维护起来也很烦。就想把常用的控件组合写成自定义控件,这样维护起来也方便,代码也清爽,下面就以带消除按钮的EditText为例。平时,EditText删除输入的都是按好多下删除键的,看到很多应用的输入框,在输入内容后,会跳出一个清空按钮,点击一下,输入的都会清除,这个用户体验很好。自己尝试了一下,原先是用最简单的控件组合,发现代码量太大,重用性不高,所以想把这个封装成一个自定义控件,直接调用。直接上代码。

1)自定义属性attrs

先想好你想要的属性,并编写xml进行声明,比如这样,

代码

注:在values/attrs.xml中加入

        <declare-styleable name="EditTextWithClearButton">        <attr name="text" format="string" />        <attr name="hint" format="string" />    </declare-styleable>  <span style="font-size:18px;"></span>

2)开始自定义了!

代码:

public class EditTextWithClearButton extends LinearLayout{private EditText et;private Button bt;public EditTextWithClearButton(Context context) {super(context);}public EditTextWithClearButton(Context context,AttributeSet attrs){super(context, attrs);init(context,attrs);}private void init(Context context, AttributeSet attrs) {//注意inflate(,,)方法的使用,要是使用不正确的话,显示不出来的!View view = LayoutInflater.from(context).inflate(R.layout.edittext_clearbutton, this,true);et = (EditText) view.findViewById(R.id.editText);bt = (Button) view.findViewById(R.id.clear_button);et.addTextChangedListener(new TextWatcher() {@Overridepublic void onTextChanged(CharSequence s, int start, int before, int count) {if(s.length() > 0){bt.setVisibility(View.VISIBLE);}else{bt.setVisibility(View.GONE);}}@Overridepublic void beforeTextChanged(CharSequence s, int start, int count,int after) {}@Overridepublic void afterTextChanged(Editable s) {}});bt.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {et.setText("");}});TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.EditTextWithClearButton);CharSequence text = a.getText(R.styleable.EditTextWithClearButton_text);CharSequence hint = a.getText(R.styleable.EditTextWithClearButton_hint);if(text != null && !TextUtils.isEmpty(text.toString().trim())){et.setText(text);et.setSelection(text.length());bt.setVisibility(View.VISIBLE);}else if(hint != null && !TextUtils.isEmpty(hint.toString().trim())){et.setHint(hint);}else{bt.setVisibility(View.GONE);}a.recycle();}public CharSequence getText(){return et.getText();}public void setText(String text){et.setText(text);}public CharSequence getHint(){return et.getHint();}public EditText getEditText(){return et;}public Button getClearButton(){return bt;}}
有两点需要注意:

1.Layoutinflater.inflate(,,)的使用,必须为代码中所示,不然控件显示不出来,我就是在这里折了,想了想才觉察这里出的问题,这个方法我还是没太掌握,请大家指点。
2.除了init()方法之外的方法,根据你的需要添加。

3)开始使用

<LinearLayout xmlns:tools="http://schemas.android.com/tools"    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:myview="http://schemas.android.com/apk/res/com.example.androidtest"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context="com.example.androidtest.MainActivity" >    <com.example.androidtest.EditTextWithClearButton        android:id="@+id/editTextWithClearButton1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:background="#ffffffff"        myview:hint="Hello World !" >    </com.example.androidtest.EditTextWithClearButton>    <Button        android:id="@+id/button1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Button" /></LinearLayout>


注:1.在开头的部分要声明命名空间,格式(xxx随意)
xmlns:xxx="http://schemas.android.com/apk/res/包名"
2.在自定义view中使用,格式(xxx即为在开头部分声明的名称):

xxx:hint="Hello World !"

好了,试试,效果怎么样。



0 0