创建复合控件

   

复合控件是原子的、可重复使用的widget,它包含多个子控件,以某种布局方式联系在一起。

 

当你创建一个复合控件的时候,你需要定义布局,外观和它包含的Views间的相互作用。复合控件通过扩展一个ViewGroup创建。为了创建一个复合控件,你需要选择一个最适合放置子控件的layout类来扩展它,如下面的框架代码所示:

 

public class MyCompoundView extends LinearLayout {

public MyCompoundView(Context context) {

super(context);

}

 

public MyCompoundView(Context context, AttributeSet attrs) {

super(context, attrs);

}

}

 

由于要与Activity一起使用,为复合控件创建UI的首选方式是使用layout资源。接下来的代码片段显示了一个layoutXML定义,layout定义了一个简单的widget,由一个EditText和一个Button组成,Button负责清除内容:

 

<?xml version=”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”>

<EditText

android:id=”@+id/editText”

android:layout_width=”fill_parent”

android:layout_height=”wrap_content”

/>

<Button

android:id=”@+id/clearButton”

android:layout_width=”fill_parent”

android:layout_height=”wrap_content”

android:text=”Clear”

/>

</LinearLayout>

 

为了使用新的widgetlayout,需要重写它的构造函数,并使用LayoutInflate系统服务的inflate方法来膨胀layout资源。inflate方法带有layout资源的参数并返回一个膨胀了的View。在这里的情况下,返回的View应该是你正在创建的类,所以你要传入一个父View并设置自动把结果附加到父View上。如下面的代码所示。

 

下面的代码显示了ClearableEditText类。在构造函数里,它膨胀了上面建立的layout资源,并获得它里面包含控件的引用。另外,还调用了hookupButton方法,它用来当Button被按下时连接清除文本的功能。

 

public class ClearableEditText extends LinearLayout {

EditText editText;

Button clearButton;

 

public ClearableEditText(Context context) {

super(context);

 

// Inflate the view from the layout resource.

String infService = Context.LAYOUT_INFLATER_SERVICE;

LayoutInflater li;

li = (LayoutInflater)getContext().getSystemService(infService);

li.inflate(R.layout.clearable_edit_text, this, true);

 

// Get references to the child controls.

editText = (EditText)findViewById(R.id.editText);

clearButton = (Button)findViewById(R.id.clearButton);

 

// Hook up the functionality

hookupButton();

}

}

 

如果你喜欢在代码里构建layout,你可以像你为Activity做的一样去实现。下面的代码片段显示了重写ClearableEditText构造函数来创建和XML中一样的UI

 

public ClearableEditText(Context context) {

super(context);

// Set orientation of layout to vertical

setOrientation(LinearLayout.VERTICAL);

 

// Create the child controls.

editText = new EditText(getContext());

clearButton = new Button(getContext());

clearButton.setText(“Clear”);

 

// Lay them out in the compound control.

int lHeight = LayoutParams.WRAP_CONTENT;

int lWidth = LayoutParams.FILL_PARENT;

addView(editText, new LinearLayout.LayoutParams(lWidth, lHeight));

addView(clearButton, new LinearLayout.LayoutParams(lWidth, lHeight));

 

// Hook up the functionality

hookupButton();

}

 

一旦屏幕已经构建完成,你可以为每个子控件连接事件管理器来提供你需要的功能。在接下来的片段,hookupButton方法填充了当Button按下时清除文本的代码:

 

private void hookupButton() {

clearButton.setOnClickListener(new Button.OnClickListener()

{

public void onClick(View v)

{

editText.setText(“”);

}

});

}

 

Sample code

http://files.cnblogs.com/xirihanlin/DL090722@cc-CompoundControl.zip

转载于:https://www.cnblogs.com/xirihanlin/archive/2009/07/22/1528725.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值