The use of a simple small control, the code is simple and easy to use

layout/number_adder.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
<ImageView
android:id="@+id/btn_reduce"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@mipmap/reduce"
android:textSize="20sp" />
<TextView
android:id="@+id/tv_count"
android:layout_width="50dp"
android:layout_height="40dp"
android:gravity="center"
android:text="1"
android:textColor="#000"
android:textSize="20sp" />
<ImageView
android:id="@+id/btn_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@mipmap/add"
android:textSize="20sp" />
</LinearLayout>
Custom view code
package adpickerview.zbxxjl.com.addersubtractor;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import butterknife.OnClick;
/**
* Created by Administrator on 2017/11/20.
*/
public class adderView extends LinearLayout implements View.OnClickListener {
private int value = 1;
private int minValue = 1;
private int maxValue = 10;
private final TextView tvCount;
public adderView(Context context, AttributeSet attrs) {
super(context, attrs);
View view = View.inflate(context, R.layout.number_adder, this);
ImageView btn_reduce = (ImageView) view.findViewById(R.id.btn_reduce);
tvCount = (TextView) view.findViewById(R.id.tv_count);
ImageView btn_add = (ImageView) view.findViewById(R.id.btn_add);
btn_reduce.setOnClickListener(this);
btn_add.setOnClickListener(this);
//Set the default value
int value = getValue();
setValue(value);
}
@OnClick({R.id.btn_reduce, R.id.btn_add})
public void onViewClicked(View view) {
switch (view.getId()) {
case R.id.btn_reduce://Less
reduce();
break;
case R.id.btn_add://plus
add();
break;
}
}
/**
* If the current value is greater than the minimum value minus
*/
private void reduce() {
if (value > minValue) {
value--;
}
setValue(value);
if (onValueChangeListene != null) {
onValueChangeListene.onValueChange(value);
}
}
/**
* If the current value is less than the minimum value, add
*/
private void add() {
if (value < maxValue) {
value++;
}
setValue(value);
if (onValueChangeListene != null) {
onValueChangeListene.onValueChange(value);
}
}
//Get specific value
public int getValue() {
String countStr = tvCount.getText().toString().trim();
if (countStr != null) {
value = Integer.valueOf(countStr);
}
return value;
}
public void setValue(int value) {
this.value = value;
tvCount.setText(value + "");
}
public int getMinValue() {
return minValue;
}
public void setMinValue(int minValue) {
this.minValue = minValue;
}
public int getMaxValue() {
return maxValue;
}
public void setMaxValue(int maxValue) {
this.maxValue = maxValue;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_reduce://Less
reduce();
break;
case R.id.btn_add://plus
add();
break;
}
}
//Monitor callback
public interface OnValueChangeListener {
public void onValueChange(int value);
}
private OnValueChangeListener onValueChangeListene;
public void setOnValueChangeListene(OnValueChangeListener onValueChangeListene) {
this.onValueChangeListene = onValueChangeListene;
}
}
Activity code
package adpickerview.zbxxjl.com.addersubtractor;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import butterknife.Bind;
import butterknife.ButterKnife;
public class MainActivity extends AppCompatActivity {
@Bind(R.id.adderview)
adderView adderview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
//monitor
adderview.setOnValueChangeListene(new adderView.OnValueChangeListener() {
@Override
public void onValueChange(int value) {
ToastUtils.show(MainActivity.this, value + "");
}
});
}
}
Finally github address:https://github.com/787060894/adderSubtractor
Before writing the function, you need to check whether the bean package is checked. If you don't add it yourself, add a few layers of data. Let's write a custom View first control Then we need to impr...
Attributes editable boolean Can you manually enter location string The position of the input box (on the left or right), the default middle ImageWidth dimension The width of the left and right sides +...
10 Lesson skill practice...
Simply wrap a shopping cart counter with a button...
This article button see my blogbutton Use button to simply encapsulate a shopping cart counter...
little practice exam No. Numbering name unit price Quantity Subtotal Place of origin operating {{$index}} {{x.id}} {{x.name}} {{x.price}} {{x.price*x.num}}...
Old routine, first look at the renderings Code directly: Main activity.java Source code required:Direct source code...
Calculate the price ***// First step *** Adapter write //Write a quantity method //achieve public void jianshuanPrice() { //Define a price double type is 0 //Click the box to associate with the price ...