Android 创建复合控件

①自定义复合控件的布局:

clearable_edit_text.xml:

<? xmlversion="1.0"encoding="utf-8" ?>
< LinearLayout
xmlns:android ="http://schemas.android.com/apk/res/android"
android:layout_width
="fill_parent"
android:layout_height
="fill_parent"
android:orientation
="horizontal" >
< EditText
android:id ="@+id/editText"
android:layout_width
="200dp"
android:layout_height
="wrap_content"
/>
< Button
android:id ="@+id/clearButton"
android:layout_width
="100dp"
android:layout_height
="wrap_content"
android:text
="Clear"
/>
</ LinearLayout >

很简单,不废话

②选择适合放置子控件的布局类,并将其扩展

package com.yinger;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

public class MyClearableEditText extends LinearLayout{

EditTexteditText;
ButtonclearButton;

public MyClearableEditText(Contextcontext,AttributeSetattrs){
super (context,attrs);
// TODOAuto-generatedconstructorstub
init();
hookupButton();
}

public MyClearableEditText(Contextcontext){
super (context);
init();
hookupButton();
}

private void init(){
StringinfService
= Context.LAYOUT_INFLATER_SERVICE;
LayoutInflaterli;
li
= (LayoutInflater)getContext().getSystemService(infService);
li.inflate(R.layout.clearable_edit_text,
this , true );
editText
= (EditText)findViewById(R.id.editText);
clearButton
= (Button)findViewById(R.id.clearButton);
}

// privatevoidinit2(){
// setOrientation(LinearLayout.VERTICAL);
// editText=newEditText(getContext());
// clearButton=newButton(getContext());
// clearButton.setText("Clear");
// intlHeight=LayoutParams.WRAP_CONTENT;
// intlWidth=LayoutParams.FILL_PARENT;
//
// addView(editText,newLinearLayout.LayoutParams(lWidth,lHeight));
// addView(clearButton,newLinearLayout.LayoutParams(lWidth,lHeight));
// }

private void hookupButton(){
clearButton.setOnClickListener(
new Button.OnClickListener(){

public void onClick(Viewv){
// TODOAuto-generatedmethodstub
editText.setText( "" );
}
});
}
}

③引用

<? xmlversion="1.0"encoding="utf-8" ?>
< LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android"
android:orientation
="vertical"
android:layout_width
="fill_parent"
android:layout_height
="fill_parent"
>
< TextView
android:layout_width ="fill_parent"
android:layout_height
="wrap_content"
android:text
="@string/hello"
/>
< com .yinger.MyClearableEditText
android:layout_width
="fill_parent"
android:layout_height
="wrap_content"
/>
</ LinearLayout >



更多相关文章

  1. Android(安卓)Studio编译失败:More than one file was found with
  2. Android(安卓)Didn't find class "xxx某个包名xxx" on path: Dex
  3. android   动态改变图片大小
  4. android 代码控制LinearLayout 宽度高度 报错widget.LinearLayou
  5. android使用activity切换动画效果
  6. android camer 图片回显界面照片分享到微博、人人、彩信、蓝牙的
  7. 对RecycleView的多种item布局的封装
  8. ListView嵌套ListView时发生:View too large to fit into drawing
  9. Android(安卓)页面自动切换实现

随机推荐

  1. android TextView多行文本(超过3行)使用e
  2. 【Android】ListView与Button的共存问题
  3. ListActivity中android:id="@+id/android
  4. Android(安卓)可以自定义速度的跑马灯效
  5. Android的简介
  6. Android(安卓)2.2 demos -- Window Featu
  7. Android和Linux kernel发展史
  8. Android(安卓)Camera
  9. Android中线程同步之Mutex与Condtion的用
  10. Android(安卓)读取一个已经安装的包的权