学习参考:http://blog.csdn.net/hudashi/article/details/50913257
http://blog.csdn.net/gebitan505/article/details/18264091
http://blog.csdn.net/psuaije/article/details/8662266
http://www.cnblogs.com/yc-755909659/archive/2015/02/10/4283294.html
下面是源代码:代码中添加了一个接口,这个接口用于给自定义控件设置自定义的事件
mycontrol.Java代码:
- 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;
- public class mycontrol extends LinearLayout {
-
-
- public interface ICoallBack{
- public void onClickButton(String s);
- }
-
-
- ICoallBack icallBack = null;
-
-
- public void setonClick(ICoallBack iBack)
- {
- icallBack = iBack;
- }
-
-
-
-
- private Context _Context;
-
-
- public mycontrol(Context context, AttributeSet attrs) {
- super(context, attrs);
-
- _Context = context;
-
- this.addView(CreateLayout());
- }
-
- private View CreateLayout(){
-
- 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.addView(edit);
- layout.addView(button);
-
- return layout;
- }
-
- }
activity_main.xml代码
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-
- xmlns:paj_control="http://schemas.android.com/apk/res/paj.control.mycontrol"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical"
- >
-
- <paj.control.mycontrol android:id="@+id/mycontrol"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
-
-
- <TextView android:id="@+id/test"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- />
-
- </LinearLayout>
MainActivity.java 代码:
- package com.example.controlstest;
-
- import paj.control.mycontrol;
- import paj.control.mycontrol.ICoallBack;
- import android.os.Bundle;
- import android.app.Activity;
- import android.view.Menu;
- import android.widget.TextView;
-
- 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);
-
- _mMycontrol.setonClick(new ICoallBack() {
-
- @Override
- public void onClickButton(String s) {
- textView.setText(s);
- }
- });
- }
- }
android 自定义控件
原文:http://www.cnblogs.com/manmanlu/p/5842994.html