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的專欄

發佈留言