您的位置:首页 > 移动开发 > Android开发

自定义android用户控件,使用回调函数实现自定义事件

2016-07-19 15:05 716 查看
直接上代码:

下面是源代码:代码中添加了一个接口,这个接口用于给自定义控件设置自定义的事件

mycontrol.java代码:

[java] view
plaincopy

package paj.control;  

  

import android.content.Context;  

import android.util.AttributeSet;  

import android.view.View;  

import android.widget.Button;  

import android.widget.EditText;  

import android.widget.LinearLayout;  

//继承LinearLayout  

public class mycontrol extends LinearLayout {  

  

    /** 

     * 一定一个接口 

     */  

    public interface ICoallBack{  

        public void onClickButton(String s);  

    }  

      

    /** 

     * 初始化接口变量 

     */  

    ICoallBack icallBack = null;  

      

    /** 

     * 自定义控件的自定义事件 

     * @param iBack 接口类型 

     */  

    public void setonClick(ICoallBack iBack)  

    {  

        icallBack = iBack;  

    }  

      

    //////////////////////////////////////////////////////////////////////////////  

    ////////////////////////////////////////////////////////////////////////////  

      

    private Context _Context;  

      

    /** 

     * 两个参数的构造函数(必须使用两个参数的构造函数,因为自定义的控件需要在XML布局中使用,里面含有属性) 

     * @param context 调用自定义控件的对象 

     * @param attrs 

     */  

    public mycontrol(Context context, AttributeSet attrs) {  

        super(context, attrs);  

          

        _Context = context;  

        //将自定义的控件添加到主布局  

        this.addView(CreateLayout());  

    }  

  

    private View CreateLayout(){  

        //创建一个LainearLayout布局  

        LinearLayout layout = new LinearLayout(_Context);  

        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT , LayoutParams.WRAP_CONTENT);  

        layout.setOrientation(LinearLayout.VERTICAL);  

        layout.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) {  

                // 返回这个自定义控件中计算出的值,使用回调实现  

                icallBack.onClickButton(edit.getText().toString());  

            }  

        });  

        //文本编辑框和按钮添加到layout布局  

        layout.addView(edit);  

        layout.addView(button);  

          

        return layout;  

    }  

  

}  

 

 

 

public class MainActivity extends Activity {  

  

    mycontrol _mMycontrol;  

    TextView textView;  

      

    @Override  

    protected void onCreate(Bundle savedInstanceState) {  

        super.onCreate(savedInstanceState);  

        setContentView(R.layout.activity_main);  

        textView = (TextView)findViewById(R.id.test);  

        //得到自定义控件  

        _mMycontrol = (mycontrol)findViewById(R.id.mycontrol);  

        //实现自定义控件中的setonClick自定义事件  

        _mMycontrol.setonClick(new ICoallBack() {  

              

            @Override  

            public void onClickButton(String s) {  

                textView.setText(s);//将自定义控件传递的值显示到文本框内  

            }  

        });  

    }  

}  
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: