自定义加减器

第一种方法:
attrs:
<resources>
    <declare-styleable name="MyAddAndRemoveView">
        <attr name="textColor" format="string"></attr>
        <attr name="textSize" format="integer"></attr>
    </declare-styleable>
</resources>
布局:
<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/Add_Btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="add" />

    <TextView
        android:id="@+id/Num_Text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="memeda" />

    <Button
        android:id="@+id/Remove_Btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="remove" />
</LinearLayout>

代码:
public class MyAddAndRemoveView extends LinearLayout implements View.OnClickListener {
    private Button mAdd, mRemove;
    private TextView mNumText;

    public MyAddAndRemoveView(Context context, AttributeSet attrs) {
        super(context, attrs);
        View.inflate(context, R.layout.add_remove_layout, this);
        initViews();
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.MyAddAndRemoveView);
        String color = array.getString(R.styleable.MyAddAndRemoveView_textColor);
        int textSize = array.getInteger(R.styleable.MyAddAndRemoveView_textSize, 0);
        array.recycle();
        mNumText.setTextSize(textSize);
        mNumText.setTextColor(Color.parseColor(color));
    }

    private void initViews() {
        mAdd = findViewById(R.id.Add_Btn);
        mAdd.setOnClickListener(this);
        mRemove = findViewById(R.id.Remove_Btn);
        mRemove.setOnClickListener(this);
        mNumText = findViewById(R.id.Num_Text);
    }

    public void setNum(int number) {
        mNumText.setText(number + "");
    }

    @Override
    public void onClick(View v) {
        String trim = mNumText.getText().toString().trim();
        int number = Integer.parseInt(trim);
        switch (v.getId()) {
            case R.id.Add_Btn:
                number++;
                mNumText.setText(number + "");
                break;
            case R.id.Remove_Btn:
                if (number > 0) {
                    number--;
                    mNumText.setText(number + "");
                }
                break;
        }
    }

}
MainActivity.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <com.example.fu_demo.weight.MyAddAndRemoveView
        android:id="@+id/MyAdd_View"
        android:layout_width="wrap_content"
        android:layout_height="80dp"
        app:textColor="#d13b3b"
        app:textSize="18" />
    <TextView
        android:id="@+id/Get_Number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="memeda"/>
</LinearLayout>
MainActivity.java:
public class MainActivity extends AppCompatActivity {

    private MyAddAndRemoveView MyAdd_View;
    private TextView Get_Number;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        MyAdd_View.setNum(10);
    }

    private void initView() {
        MyAdd_View = (MyAddAndRemoveView) findViewById(R.id.MyAdd_View);
        Get_Number = (TextView) findViewById(R.id.Get_Number);

    }
}
第二种方法:
布局:
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <Button
        android:id="@+id/jian"
        android:layout_width="30dp"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:textSize="20sp"
        android:gravity="center"
        android:text="-" />

    <TextView
        android:id="@+id/count"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:textSize="20sp"
        android:gravity="center"
        android:text="1" />

    <Button
        android:id="@+id/jia"
        android:layout_width="30dp"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:textSize="20sp"
        android:gravity="center"
        android:text="+" />

</LinearLayout>
主要代码:
public class CustomView extends LinearLayout implements View.OnClickListener {

    public Button jian;
    public TextView tvCount;
    public Button jia;
    public int count;


    public CustomView(Context context) {
        super(context);
    }

    public CustomView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

        LayoutInflater.from(context).inflate(R.layout.custom, this, true);
        jian = findViewById(R.id.jian);
        tvCount = findViewById(R.id.count);
        jia = findViewById(R.id.jia);
        jian.setOnClickListener(this);
        jia.setOnClickListener(this);
    }

    public CustomView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @Override
    public void onClick(View view) {
        jia.setOnClickListener(this);
        jian.setOnClickListener(this);

        switch (view.getId()) {
            case R.id.jia:
                count++;
                tvCount.setText(count + "");
                if (inter != null) {
                    inter.setnumber(count);
                }
                break;
            case R.id.jian:
                if (count > 1) {

                    count--;
                    tvCount.setText(count + "");
                    if (inter != null) {
                        inter.setnumber(count);
                    }
                } else {
                    Toast.makeText(getContext(), "参数不能小于1", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

    public MyInter inter;

    public void setInter(MyInter inter) {
        this.inter = inter;
    }

    public interface MyInter {
        public void setnumber(int index);
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值