关于自定义组合组件应用的问题总结
来源:互联网 发布:上海网络推广招聘 编辑:程序博客网 时间: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());
}
- 关于自定义组合组件应用的问题总结
- Android 自定义组合组件的总结
- 自定义组件的应用实践
- 自定义组件(二)------使用系统控件组合的自定义控件
- 关于angularjs中写组件报错的问题总结
- 自定义组件注册的问题
- 自定义组合控件关于LayoutInflater.from(context).inflate(R.layout.view_title, this,true)的问题
- 32、自定义组件RelativeLayout、设置组合控件的状态
- 关于android自定义标题栏出现的问题总结
- 关于使用TabLayout自定义样式的问题总结
- 关于组件jClientUpload的问题
- 关于flash组件的问题~~~~~~
- 关于求组合数的一些总结
- 关于自定义spark组件的学习体会
- 【总结】关于fileoutputstream应用中的FileNotFoundException问题的解决办法
- 组合计数问题中容斥原理的应用
- 关于JSF2中自定义组件处理表单值的一些问题
- 关于任一数组的组合问题
- 安装双系统:XP + Ubuntu 11.04
- javascript_core阅读笔记(四)
- WPF 向gird添加子控件
- javascript_core阅读笔记(五)
- django第三方类库 - django-south 项目开始中就使用south方法
- 关于自定义组合组件应用的问题总结
- PostgreSQL使用方法
- javascript原型链体会
- 动态创建多个连接池
- Logback在调用打印方法后所做的事
- Logback笔记
- 在windows XP下搭建android开发环境
- android一周小结
- android第二周小结