自定义控件

好处:

  • 提高布局文件开发效率
  • 降低布局文件维护成本
  • 降低布局文件和Activity代码耦合性
  • 容易扩展
  • 简单易用


分类:

1.自绘控件(完全自定义控件):继承的是View
2.组合式自定义控件:继承的是viewGroup
3.继承式自定义控件

一:首先说一下自绘式自定义控件,实现它需要牢记三个方法:

1. onMeasure(int,int)测量:该方法来检查View组件以及它所包含的所有子组件i 的大小
2. onLayout(boolean,int,int,int,int)位置:当该组件要分配子组件的位置,大小时,调用(使用频率较少)
3. onDraw(canves) 绘制:当该组件将要绘制它的内容是,回调该方法(使用频率最高,不要在此方法中做耗时操作和对象的建立)

自绘式自定义控件的实现步骤:

1. 类继承View(从一个普通的类,变成一个控件)
2. 覆写它必须要覆写三个方法
3. 在OnMeasure和OnDrawer方法中,写 对应的业务逻辑


二:组合式自定义

实现的效果
自定义控件——加减器


实现的步骤:

1.首先定义一个类,在ViewGroup下继承任意一个子控件

public class AddsubView extends Linearlayout


2.覆写它的三个构造方法

自定义控件——加减器


3. xml文件中布局组合自定义控件

自定义控件——加减器


4.初始化这个布局,并将这个布局加载到自定义类AddSubView中
a.初始化布局中的控件
自定义控件——加减器

b. 添加两个Imageview的点击事件,以及业务逻辑代码

自定义控件——加减器


private void SubNumber() {
    if (value<maxvalue){
        value++;
    }
    setvalue(value);

}
private void AddNumber() {
    if (value>minvalue){
        value--;
    }
    setvalue(value);
}


c。获取value值和设置value值进行显示。实现加减的效果

自定义控件——加减器

5.在activity_main.xml文件中定义自定义控件,实现展示在Activity上自定义控件——加减器



===================================================================================================================================


整体代码:

布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- 整体布局,包括增加和减少商品数量的符号以及中间的商品数量 -->
    <LinearLayout
        android:gravity="center"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <!-- 减少商品数量的布局 -->
        <Button
            android:id="@+id/addbt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#0157D3"
            android:text="-">
        </Button>
        <!-- 商品数量的布局 -->
        <TextView
            android:id="@+id/tv"
            android:text="0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
        </TextView>
        <!-- 增加商品数量的布局 -->
        <Button
            android:id="@+id/subbt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#0157D3"
            android:text="+">
        </Button>
        <!-- 显示商品数量的布局 -->

    </LinearLayout>
</LinearLayout>

main方法:

public class View1 extends LinearLayout implements View.OnClickListener{
    private Button addbt;
    private Button subbt;
    private TextView tv;
    private int value = 1;
    private int maxvalue = 5;
    private int minvalue = 1;



    public View1(Context context) {
        this(context, null);
    }

    public View1(Context context, AttributeSet attrs) {
        this(context, null, 0);

    }

    public View1(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initview(context);
    }

    private void initview(Context context) {
        //把一个布局文件实例化,并且加载到本类中
        View view = (View) View.inflate(context, R.layout.add_view, this);
        //初始化控件
        addbt = view.findViewById(R.id.addbt);
        subbt = view.findViewById(R.id.subbt);
        tv  = view.findViewById(R.id.tv);
        //设置点击事件
        addbt.setOnClickListener(this);
        subbt.setOnClickListener(this);

        //获取value值
        int value = getvalue();

        //设置value值
        setvalue(value);


    }

    private int getvalue() {
        String trim = tv.getText().toString().trim();
        if (!TextUtils.isEmpty(trim)){
            value = Integer.valueOf(trim);
        }
        return value;
    }

    private void setvalue(int value) {
        this.value = value;
        tv.setText(value+"") ;
    }


    @Override
    public void onClick(android.view.View view) {
        switch (view.getId()){
            //添加
            case R.id.addbt:
                AddNumber();
                break;
            case R.id.subbt:
                SubNumber();
                break;
        }
    }

    private void SubNumber() {
        if (value<maxvalue){
            value++;
        }
        setvalue(value);

    }
    private void AddNumber() {
        if (value>minvalue){
            value--;
        }
        setvalue(value);
    }
}






















相关文章:

  • 2021-06-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-08
  • 2021-12-18
猜你喜欢
  • 2021-11-04
  • 2021-05-24
  • 2022-12-23
  • 2022-12-23
  • 2021-10-02
相关资源
相似解决方案