android构建复合组件
package andorid.j.awin;import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
/*
* create by danielinbiti
*/
public class EditTextBtn extends LinearLayout {
Button button = null;
EditText editText = null;
public EditTextBtn(Context context) {
super(context);
this.init(context);
}
public EditTextBtn(Context context,AttributeSet attrs){
super(context,attrs);
this.init(context);
}
private void init(Context context){
button = new Button(context);
editText = new EditText(context);
this.setOrientation(LinearLayout.HORIZONTAL);
this.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,50));
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.FILL_PARENT);
layoutParams.weight = 1;
this.addView(editText,layoutParams);
LinearLayout.LayoutParams layoutParamsBtn = new LinearLayout.LayoutParams(50,LayoutParams.FILL_PARENT);
button.setHeight(editText.getHeight());
this.addView(button,layoutParamsBtn);
}
public void onButtonClick(){
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editText.setText("测试danielinbiti");
}
});
}
}
//代码中使用时
editTextBtn = new EditTextBtn(this);
加入到布局里面,如果定义到xml里面也行
以上代码主要是一个简单的示例,组件的组合
摘自 danielinbiti的专栏
补充:移动开发 , Android ,