关于自定义组合组件应用的问题总结
2013-09-24 10:38
302 查看
假设我们自定义了一下的布局:
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());
}
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());
}
相关文章推荐
- 关于JSF2中自定义组件处理表单值的一些问题
- 关于android自定义标题栏出现的问题总结
- 关于flex开发自定义组件需要注意的问题
- Android 自定义组合组件的总结
- 关于app集成支付宝应用内支付的问题总结
- 开启eslint,关于import自定义组件报错问题
- 最近在ArcGIS Engine开发中关于调用gp工具过程出现COM 组件的调用返回了错误 HRESULT E_FAIL 错误的解决方法 和 学习oracle中遇到的一些问题总结
- 关于使用TabLayout自定义样式的问题总结
- 关于app集成支付宝应用内支付的问题总结
- 关于app集成支付宝应用内支付的问题总结
- 关于app集成支付宝应用内支付的问题总结
- Java【多线程知识总结(7)】多线程同步问题-关于synchronized代码块和synchronized方法的应用
- 【总结】关于fileoutputstream应用中的FileNotFoundException问题的解决办法
- Java【多线程知识总结(7)】多线程同步问题-关于synchronized代码块和synchronized方法的应用
- 关于angularjs中写组件报错的问题总结
- 关于网友flex自定义组件问题的答复
- 自定义组合控件关于LayoutInflater.from(context).inflate(R.layout.view_title, this,true)的问题
- Android应用中使用Fragment组件的一些问题及解决方案总结
- 这是我们公司总结的一些关于中文乱码问题的一些解决方案和经验和大家分享! .
- 关于计算机中的原码、反码、补码问题总结