Android 自定义复合组件Demo
在新的混合组件的构造函数中,首先,调用所有的父类的构造函数,传入对应的参数。然后可以设置你的混合组件的其他的一些方面,在哪创建EditText组件,又在哪创建PopupList组件。注意:你同时也可以在XML文件中引入一些自己的属性和参数,这些属性和参数也可以被你的混合组件所使用。
你也可以创建时间监听器去监听新组件中View类触发的事件,例如,对List选项单击事件的监听,你必须在此时间发生后更新你EditText的值。
你可能创建自己的一些属性,带有访问和修改方法。例如,允许设置EditText初始值并且提供访问它的方法。
在Layout的派生类中,你没有必要去重载onDraw()和onMeasure()方法,因为Layout会有比较好的默认处理。但是,如果你觉得有必要你也可以重载它。
你也可能重载一些on系列函数,例如通过onKeyDown()的重载,你可以通过按某个键去选择列表中的对应的值。
总之,把Layout类作为基类有下面几个优点:
onDraw()函数和onMeasure()函数是没有必要重载的,两个函数已经做得很好了。
你可以很快的创建你的混合组件,并且可以像单一组件那样使用。
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
public class TextExt extends LinearLayout {
private TextView tv;
private ImageButton ib;
private onExtClickListener clickListener;
public TextExt(Context context, String text, int imgres) {
super (context);
// TODO Auto-generated constructor stub
this .setOrientation(VERTICAL);
tv = new TextView(context);
tv.setText(text);
addView(tv, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
ib = new ImageButton(context);
ib.setImageResource(imgres);
ib.setOnClickListener( new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (clickListener != null ) {
clickListener.onclickListenr(getText());
}
}
});
addView(ib, new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
}
public void setText(String text) {
tv.setText(text);
}
public String getText() {
return tv.getText().toString();
}
public void setImage( int res) {
ib.setImageResource(res);
}
public Drawable getDrawable() {
return ib.getDrawable();
}
public void setDrawable(Drawable dd) {
ib.setImageDrawable(dd);
}
public void setOnExtClickListener(onExtClickListener click) {
this .clickListener = click;
}
}
public void onclickListenr(String text);
}
import com.terry.custom.widget.TextExt;
import com.terry.custom.widget.onExtClickListener;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
public class CustomActivity extends Activity {
boolean isChange = true ;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextExt te = new TextExt( this , " 自定义组件 " , R.drawable.icon);
te.setOnExtClickListener( new onExtClickListener() {
@Override
public void onclickListenr(String text) {
// TODO Auto-generated method stub
Toast.makeText(CustomActivity. this , text, 1000 ).show();
}
});
LinearLayout ly = (LinearLayout) findViewById(R.id.testWidget);
ly.addView(te);
Button btn = (Button) findViewById(R.id.Button01);
Button btn2 = (Button) findViewById(R.id.Button02);
btn.setOnClickListener( new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (isChange) {
te.setImage(android.R.drawable.btn_dialog);
} else {
te.setDrawable(getResources().getDrawable(R.drawable.icon));
}
isChange = ! isChange;
}
});
btn2.setOnClickListener( new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
te.setText( " 只可以改变一次喔 " );
}
});
}
}