Android按钮控制加减,,(购物车中的加减器)
16lz
2021-01-26
<? xml version= "1.0" encoding= "utf-8" ?>< RelativeLayout 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= "com.example.com.zhangjinlong0514.MainActivity"> < LinearLayout android :layout_width= "match_parent" android :layout_height= "match_parent" android :orientation= "horizontal"> < Button android :layout_width= "wrap_content" android :layout_height= "wrap_content" android :layout_weight= "1" android :id= "@+id/but1" android :text= "-"/> < TextView android :layout_width= "wrap_content" android :layout_height= "wrap_content" android :layout_weight= "1" android :id= "@+id/tv" android :text= "12条数据"/> < Button android :layout_width= "wrap_content" android :layout_height= "wrap_content" android :layout_weight= "1" android :id= "@+id/but2" android :text= "+"/> LinearLayout> < com.example.com.zhangjinlong0514.MyViewGroup android :layout_width= "match_parent" android :layout_height= "wrap_content" android :layout_marginTop= "200dp" > < TextView android :layout_width= "150dp" android :layout_height= "40dp" android :background= "#f00" /> < TextView android :layout_width= "150dp" android :layout_height= "40dp" android :background= "#02fac0" /> < TextView android :layout_width= "150dp" android :layout_height= "40dp" android :background= "#0033ff" /> com.example.com.zhangjinlong0514.MyViewGroup> RelativeLayout> package com.example.com.zhangjinlong0514; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends AppCompatActivity { private Button but1; private Button but2; private int a= 0; private int b; private TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate( savedInstanceState); setContentView(R.layout. activity_main); but1 = findViewById(R.id. but1); but2 = findViewById(R.id. but2); tv = findViewById(R.id. tv); but2.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View view) { b= a+ 1; a= b; tv.setText( ""+ b); } }); but1.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View view) { if( b> 0){ b--; tv.setText( ""+ b); } } }); } } 购物车中的加减器:
运用组合式控件编写一个类似于购物车的增加删除数量的效果,如图所示:
MainActivity中
public class MainActivity extends AppCompatActivity { private viewjia av; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); av = (viewjia) findViewById(R.id.jia); av.setMaxValue(10); av.setOnNumberChangerListener(new viewjia.OnNumberChangerListener() { @Override public void OnNumberChanger(int value) { //属于我自己的业务逻辑 Toast.makeText(MainActivity.this, "变化的数量值"+value, Toast.LENGTH_SHORT).show(); } }); }}
类继承一个属于ViewGroup下的任意自定义控件
public class viewjia extends LinearLayout implements View.OnClickListener { private ImageButton mIv_sub; private ImageButton mIv_add; private TextView mTv_value; private int mValue; //1.创建对象的时候 public viewjia(Context context) { this(context, null); } //2.XML中使用的时候回调 public viewjia(Context context, AttributeSet attrs) { this(context, attrs , 0); } //3.在XML中使用,且使用Style风格中. public viewjia(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initView(context); } private void initView(Context context) { //把一个布局文件实例化,并且加载到AddSuVIew类中 View inflate = View.inflate(context, R.layout.item, this); //初始化控件 mIv_sub = (ImageButton) inflate.findViewById(iv_sub); mIv_add = (ImageButton) inflate.findViewById(iv_add); mTv_value = (TextView) inflate.findViewById(tv_value); mIv_add.setOnClickListener(this); mIv_sub.setOnClickListener(this); //获取Value值 int value = getValue(); //设置valus的值 setValue(value); } //当前数量值,默认为1,设置对此值获取. private int value =1; private int mMaxValue =5; private int mMinValue =1; public void setMaxValue(int maxValue) { mMaxValue = maxValue; } /** * 这里获取Value是从UI那里拿到值 * @return */ public int getValue() { String trim = mTv_value.getText().toString().trim(); if (!TextUtils.isEmpty(trim)){ //获取出来,因为其值是字符串,所以要进行Int型转换 value =Integer.valueOf(trim); } return value; } public void setValue(int value) { mValue = value; mTv_value.setText(value+""); } //ImageView按钮的点击事件 @Override public void onClick(View view) { switch (view.getId()) { //添加 case iv_add: addNumber(); break; //减少 case iv_sub: subNumber(); break; default: break; } } private void addNumber() { if (value < mMaxValue) { value++; } setValue(value); if (mOnNumberChangerListener !=null){ mOnNumberChangerListener.OnNumberChanger(value); } } private void subNumber() { if (value > mMinValue){ value--; } setValue(value); //当按钮是Value值发生变化时,回调该接口方法 if (mOnNumberChangerListener !=null){ mOnNumberChangerListener.OnNumberChanger(value); } } /** * B.定义接口,及所要调用的接口方法,当商品数量发生变化时,回调给接口 */ public interface OnNumberChangerListener{ void OnNumberChanger(int value); } //定义接口对象 private OnNumberChangerListener mOnNumberChangerListener; //设置方法接收外界传来的接口对象方法 public void setOnNumberChangerListener(OnNumberChangerListener onNumberChangerListener){ mOnNumberChangerListener =onNumberChangerListener; }}
MainXML
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.view_.MainActivity" android:orientation="horizontal"><com.example.view_.viewjia android:id="@+id/jia" android:layout_width="wrap_content" android:layout_height="wrap_content">com.example.view_.viewjia>LinearLayout>
自定义控件的布局
<?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"> <ImageButton android:id="@+id/iv_add" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/b"/> <TextView android:id="@+id/tv_value" android:layout_width="50dp" android:layout_height="50dp" android:gravity="center" android:textSize="20sp" android:text="1"/> <ImageButton android:id="@+id/iv_sub" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/a"/>LinearLayout>
更多相关文章
- 【Android】图片操作工具类(ImageUtil.java)
- Android(安卓)日期选择控件
- Android(安卓)Calendar的学习与运用【转】
- Android/读取指定类型的文件
- Android获取USB权限
- android双待手机获取每一张SIM卡的imei
- 获取mic音量大小
- 安卓布局知识点
- Navigation(2)