在android软件设计中会用到好多的控件,但系统自带的控件有好多不能够达到需要实现的功能或是控件不够美观。那怎么办呢?
android应为我们提供了好多的控件,我们可以继承某一控件,然后对它进行重写来实现自己的一些功能。或是直接继承View自己定义自己的控件。
下面我来讲一下如何写最简单的自定义控件。
1.创建android工程,取名为ControlsTest,直接下一步下一步一直到完成。
2.在工程中新建一个包,命名为paj.control,然后完成
3.在paj.control包中新建一个类,起名为mycontrol并继承View
并添加,两个参数的构造函数(必须使用两个参数的构造函数,因为自定义的控件需要在XML布局中使用,里面含有属性)
下面是源代码:代码中添加了一个接口,这个接口用于给自定义控件设置自定义的事件
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; //继承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; } }
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); //实现自定义控件中的setonClick自定义事件 _mMycontrol.setonClick(new ICoallBack() { @Override public void onClickButton(String s) { textView.setText(s);//将自定义控件传递的值显示到文本框内 } }); } }