关于自定义组合组件应用的问题总结

来源:互联网 发布:上海网络推广招聘 编辑:程序博客网 时间:2024/04/30 12:50

假设我们自定义了一下的布局:

 package com.InterfaceDemo.interfacedemo;


import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;


public class MyControl extends LinearLayout
{


public MyControl(Context context, AttributeSet attrs, int defStyle)
{
super( context, attrs, defStyle );
}

public MyControl(Context context, AttributeSet attrs)
{
          this( context, null, 0 );
          
}

public MyControl(Context context)
{
this( context, null );

}

public  MyControl(Activity activity,int type,String ty){
this( activity.getApplicationContext() );
initView(activity.getApplicationContext());
}



private void initView(Context context){
LinearLayout linearLayout = new LinearLayout( context );
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT );
linearLayout.setOrientation( LinearLayout.VERTICAL );
linearLayout.setLayoutParams( params );
//创建一个文本编辑框  
        final EditText edit = new EditText(context);  
        LayoutParams editParams = new LayoutParams(LayoutParams.FILL_PARENT , LayoutParams.WRAP_CONTENT);  
        edit.setLayoutParams(editParams);  
        //创建一个按钮  
        Button button = new Button(context);  
        LayoutParams btpParams = new LayoutParams(LayoutParams.WRAP_CONTENT , LayoutParams.WRAP_CONTENT);  
        button.setLayoutParams(btpParams);  
        button.setText("点击获取");  
        
        button.setOnClickListener( new OnClickListener()
{

@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
callBack.onButtonClick( edit.getText().toString() );
}
} );
        linearLayout.addView( button );
        linearLayout.addView( edit );        
   this.addView( linearLayout );
}

//自定义一个接口
public interface MycallBack{
public void onButtonClick(String s);
};

MycallBack callBack;

public void setInterface(MycallBack callBack){
this.callBack = callBack;
}
}


我们在项目中要使用的时候有两种方式:

A:在对应的xml中声明

B:在代码中生成对象

下面来看看两者在使用的时候的区别:

A: 直接在xml中

<!-- 自定义控件 -->
<!-- <com.InterfaceDemo.interfacedemo.MyControl  -->
<!--    android:id="@+id/mycontrol"  -->
<!--    android:layout_width="fill_parent"  -->
<!--    android:layout_height="wrap_content"  -->
<!--    /> -->

此时需要注意的是在我们的自定义代码中需要同时实现三个构造函数:

public MyControl(Context context, AttributeSet attrs, int defStyle)
{
super( context, attrs, defStyle );
}

public MyControl(Context context, AttributeSet attrs)
{
          this( context, null, 0 );
          
}

public MyControl(Context context)
{
this( context, null );

}

同时initView(context);的位置大家可以试着放到不同构造器中,我测试的结果是最好放在有三个参数的构造器中比较保险

B:在代码中生成对象。这个方法一般用的会比较多,此刻我们如果需要在构造对象的时候传递一些参数过来的话可以这么处理:

public MyControl(Context context, AttributeSet attrs, int defStyle)
{
super( context, attrs, defStyle );
}

public MyControl(Context context, AttributeSet attrs)
{
          this( context, null, 0 );
          
}

public MyControl(Context context)
{
this( context, null );

}

//自定义构造器,传递相应的参数,最关键的是调用系统的构造器
public  MyControl(Activity activity,int type,String ty){
this( activity.getApplicationContext() );
initView(activity.getApplicationContext());
}






原创粉丝点击