android構建複合組件

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.