AddSubView写法

本文介绍了一个自定义的Android视图组件AddSubView,该组件实现了基本的数量增减功能,并提供了设置数量、获取数量及为加减按钮设置点击事件的方法。布局包括减号、数量显示和加号三个TextView元素。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public class AddSubView extends LinearLayout {

    private TextView sub;
    private TextView num;
    private TextView add;

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

    public AddSubView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        View view = LayoutInflater.from(context).inflate(R.layout.add_sub_view, this);
        sub = findViewById(R.id.child_text_jian);
        num = findViewById(R.id.child_text_num);
        add = findViewById(R.id.child_text_add);
    }

    /**
     * 设置数量
     * @param str
     */
    public void setNum(String str) {
        num.setText(str);
    }

    /**
     * 获取数量
     *
     * @return
     */
    public String getNum() {
        return num.getText().toString();
    }

    /**
     * 给加号设置点击事件
     *
     * @param onclickListener
     */
    public void setAddOnclickListener(OnClickListener onclickListener) {
        add.setOnClickListener(onclickListener);
    }

    /**
     * 给减号设置点击事件
     *
     * @param onclickListener
     */
    public void setSubOnclickListener(OnClickListener onclickListener) {
        sub.setOnClickListener(onclickListener);
    }

}

布局文件

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/child_image"
    android:layout_marginRight="5dp"
    android:layout_toLeftOf="@+id/child_text_delete"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/child_text_jian"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/cart_biankuang"
        android:padding="5dp"
        android:text=""/>

    <TextView
        android:id="@+id/child_text_num"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/cart_biankuang"
        android:paddingBottom="5dp"
        android:paddingLeft="8dp"
        android:paddingRight="8dp"
        android:paddingTop="5dp"/>

    <TextView
        android:id="@+id/child_text_add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/cart_biankuang"
        android:padding="5dp"
        android:text=""/>

</LinearLayout>
在drawable里面写一个cart_biankuang

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#ffffff" />

    <stroke
        android:width="0.1dp"
        android:color="#000000" />

</shape>


运行为14:42: error: use of undeclared type 'SystemImage' static func loadImage(named: String) -> SystemImage? { ^~~~~~~~~~~ 28:24: error: 'URLSession' is unavailable: This type has moved to the FoundationNetworking module. Import that module to use it. private let session = URLSession.shared ^~~~~~~~~~ Foundation.URLSession:2:18: note: 'URLSession' has been explicitly marked unavailable here public typealias URLSession = AnyObject ^ 50:22: error: use of unresolved identifier 'SystemLabel' private let label = SystemLabel() ^~~~~~~~~~~ 51:26: error: use of unresolved identifier 'SystemImageView' private let imageView = SystemImageView() ^~~~~~~~~~~~~~~ 52:23: error: use of unresolved identifier 'SystemButton' private let button = SystemButton() ^~~~~~~~~~~~ 54:11: error: initializer does not override a designated initializer from its superclass override init(frame: CGRect) { ~~~~~~~~ ^ 49:21: error: use of undeclared type 'SystemView' class PlatformView: SystemView { ^~~~~~~~~~ 55:3: error: 'super' members cannot be referenced in a root class super.init(frame: frame) ^ 60:3: error: 'super' members cannot be referenced in a root class super.init(coder: coder) ^ 67:21: error: use of unresolved identifier 'SystemColor' label.textColor = SystemColor.label ^~~~~~~~~~~ 68:3: error: use of unresolved identifier 'addSubview' addSubview(label) ^~~~~~~~~~ 73:3: error: use of unresolved identifier 'addSubview' addSubview(imageView) ^~~~~~~~~~ 83:3: error: use of unresolved identifier 'addSubview' addSubview(button) ^~~~~~~~~~ 90:3: error: use of unresolved identifier 'NSLayoutConstraint' NSLayoutConstraint.activate([ ^~~~~~~~~~~~~~~~~~ 91:48: error: use of unresolved identifier 'centerXAnchor' imageView.centerXAnchor.constraint(equalTo: centerXAnchor), ^~~~~~~~~~~~~ 92:44: error: use of unresolved identifier 'topAnchor' imageView.topAnchor.constraint(equalTo: topAnchor, constant: 20), ^~~~~~~~~ 97:44: error: use of unresolved identifier 'centerXAnchor' label.centerXAnchor.constraint(equalTo: centerXAnchor), ^~~~~~~~~~~~~ 100:45: error: use of unresolved identifier 'centerXAnchor' button.centerXAnchor.constraint(equalTo: centerXAnchor), ^~~~~~~~~~~~~
最新发布
07-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值