自定义控件(17)---布局方式组合系统控件,并封装自定义控件思想(自定义CheckBox)

来源:互联网 发布:东风21d知乎 编辑:程序博客网 时间:2024/06/15 18:47

先看主布局

activity_main.xml(就一个自定义控件)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"      xmlns:tools="http://schemas.android.com/tools"      android:layout_width="match_parent"      android:layout_height="match_parent"      tools:context="com.example.customerview.MainActivity" >        <com.example.customerview.CustomerView          android:layout_width="fill_parent"          android:layout_height="wrap_content" />    </RelativeLayout>  

MainActivity(简单吧,就加载一个主布局文件)

package com.example.customerview;    import android.app.Activity;  import android.os.Bundle;  import android.view.Menu;  import android.view.MenuItem;    public class MainActivity extends Activity {        @Override      protected void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.activity_main);      }    } 

来看自定义控件

CustomerView

package com.example.customerview;    import android.content.Context;  import android.util.AttributeSet;  import android.view.View;  import android.widget.LinearLayout;  import android.widget.Toast;    public class CustomerView extends LinearLayout implements View.OnClickListener {      private Context context;        public CustomerView(Context context, AttributeSet attrs, int defStyleAttr) {          super(context, attrs, defStyleAttr);          init(context);      }        public CustomerView(Context context, AttributeSet attrs) {          super(context, attrs);          init(context);      }        public CustomerView(Context context) {          super(context);          init(context);      }        /**      * 初始化      */      private void init(Context context) {          this.context = context;          inflate(context, R.layout.childview, this);          View button1 = findViewById(R.id.button1);          View button2 = findViewById(R.id.button2);          button1.setOnClickListener(this);          button2.setOnClickListener(this);      }        @Override      public void onClick(View v) {          switch (v.getId()) {          case R.id.button1:              Toast.makeText(context, "button1", 0).show();              break;            case R.id.button2:              Toast.makeText(context, "button2", 0).show();              break;          default:              break;          }      }    }  

inflate(context, R.layout.childview, this);这行代码加载的布局
childview.xml(简单把)

<?xml version="1.0" encoding="utf-8"?>  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"      android:layout_width="match_parent"      android:layout_height="match_parent"      android:orientation="horizontal" >        <Button          android:id="@+id/button2"          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:text="Button" />        <Button          android:id="@+id/button1"          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:text="Button" />    </LinearLayout>  

接下来看一个自定义的CheckBox系统控件


+++++++++++++++++++++++++++++++++++++++++++开始+++++++++++++++++++++++++++++++++++++++++++

activity_main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <com.aigestudio.customviewdemo.activities.CustomCheckBox        android:id="@+id/main_ccb"        android:layout_width="fill_parent"        android:layout_height="fill_parent" >    </com.aigestudio.customviewdemo.activities.CustomCheckBox></LinearLayout>

view_custom_check_box.xml

<?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <ImageView        android:id="@+id/view_custom_check_box_on"        android:layout_width="60dp"        android:layout_gravity="center"        android:scaleType="fitCenter"        android:layout_height="60dp" />    <ImageView        android:id="@+id/view_custom_check_box_off"        android:layout_gravity="center"        android:layout_width="60dp"        android:layout_height="60dp"        android:scaleType="fitCenter" /></FrameLayout>

MainActivity

package com.aigestudio.customviewdemo.activities;import android.app.Activity;import android.os.Bundle;import android.widget.Toast;import com.aigestudio.customviewdemo.R;import com.aigestudio.customviewdemo.activities.CustomCheckBox.CustomCheckBoxChangeListener;/** * 主界面 */public class MainActivity extends Activity {private CustomCheckBox ccbTest;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);ccbTest = (CustomCheckBox) findViewById(R.id.main_ccb);ccbTest.setCustomCheckBoxChangeListener(new CustomCheckBoxChangeListener() {@Overridepublic void customCheckBoxOn() {Toast.makeText(MainActivity.this, "Check on", Toast.LENGTH_SHORT).show();}@Overridepublic void customCheckBoxOff() {Toast.makeText(MainActivity.this, "Check off", Toast.LENGTH_SHORT).show();}});}}
MeasureUtil

package com.aigestudio.customviewdemo.activities;import android.app.Activity;import android.graphics.Bitmap;import android.graphics.Color;import android.util.DisplayMetrics;/** * 测绘工具类 */public final class MeasureUtil {/** * 获取屏幕尺寸 *  * @param activity *            Activity * @return 屏幕尺寸像素值,下标为0的值为宽,下标为1的值为高 */public static int[] getScreenSize(Activity activity) {DisplayMetrics metrics = new DisplayMetrics();activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);return new int[] { metrics.widthPixels, metrics.heightPixels };}}

CustomCheckBox

package com.aigestudio.customviewdemo.activities;import android.content.Context;import android.content.res.TypedArray;import android.graphics.drawable.Drawable;import android.util.AttributeSet;import android.view.LayoutInflater;import android.view.View;import android.widget.FrameLayout;import android.widget.ImageView;import com.aigestudio.customviewdemo.R;/** * 自定义CheckBox *  */public class CustomCheckBox extends FrameLayout {private ImageView ivCheckOn, ivCheckOff;// 两种状态的ImageViewprivate boolean isCheck;// 是否被选中的标志值public CustomCheckBox(Context context) {this(context, null);}public CustomCheckBox(Context context, AttributeSet attrs) {this(context, attrs, 0);}public CustomCheckBox(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);// 设置布局文件LayoutInflater.from(context).inflate(R.layout.view_custom_check_box, this);// 获取控件元素ivCheckOn = (ImageView) findViewById(R.id.view_custom_check_box_on);ivCheckOff = (ImageView) findViewById(R.id.view_custom_check_box_off);// 设置两个ImageView的点击事件ivCheckOn.setOnClickListener(new ClickListener());ivCheckOff.setOnClickListener(new ClickListener());int imageOnResId = getResources().getIdentifier("bluedot", "drawable" , context.getPackageName());  int imageOffResId = getResources().getIdentifier("reddot", "drawable" , context.getPackageName());  // 设置显示资源setOnImage(imageOnResId);setOffImage(imageOffResId);// 默认显示的是没被选中的状态setCheckOff();}/** * 设置开启状态时CustomCheckBox的图片 */public void setOnImage(int resId) {ivCheckOn.setImageResource(resId);}/** * 设置关闭状态时CustomCheckBox的图片 */public void setOffImage(int resId) {ivCheckOff.setImageResource(resId);}/** * 设置CustomCheckBox为关闭状态 */public void setCheckOff() {isCheck = false;ivCheckOn.setVisibility(GONE);ivCheckOff.setVisibility(VISIBLE);}/** * 设置CustomCheckBox为开启状态 */public void setCheckOn() {isCheck = true;ivCheckOn.setVisibility(VISIBLE);ivCheckOff.setVisibility(GONE);}/** * 状态改变监听接口 */private CustomCheckBoxChangeListener customCheckBoxChangeListener;// 切换的监听器public interface CustomCheckBoxChangeListener {void customCheckBoxOn();void customCheckBoxOff();}public void setCustomCheckBoxChangeListener(CustomCheckBoxChangeListener customCheckBoxChangeListener) {this.customCheckBoxChangeListener = customCheckBoxChangeListener;}/** * 自定义CustomCheckBox中控件的事件监听器 */private class ClickListener implements OnClickListener {@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.view_custom_check_box_on:setCheckOff();customCheckBoxChangeListener.customCheckBoxOff();break;case R.id.view_custom_check_box_off:setCheckOn();customCheckBoxChangeListener.customCheckBoxOn();break;}}}}




0 0