package com.example.lenovo.moniyuekao.ui;
import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.example.lenovo.moniyuekao.R;
/**
* date:2018/6/29 16:16
* function:
*/
public class AddSubView extends LinearLayout {
private ImageView mImageSub;
private TextView mTextView;
private ImageView mImageAdd;
private int textnum;
public int getTextnum() {
String s = mTextView.getText().toString();
Integer num = Integer.valueOf(s);
return num;
}
public void setTextnum(int textnum) {
mTextView.setText(textnum+"");
}
public AddSubView(Context context) {
this(context, null);
}
public AddSubView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public AddSubView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
View view = LayoutInflater.from(context).inflate(R.layout.add, this, true);
initView(view);
mImageAdd.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String s = mTextView.getText().toString();
Integer num = Integer.valueOf(s);
num++;
mTextView.setText(num+"");
if (mOnAddItemClick!=null){
mOnAddItemClick.OnAddClick();
}
}
});
mImageSub.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String s = mTextView.getText().toString();
Integer num = Integer.valueOf(s);
if (num>1){
num--;
}
mTextView.setText(num+"");
if (mOnAddItemClick!=null){
mOnAddItemClick.OnAddClick();
}
}
});
}
private void initView(View view) {
mImageSub = view.findViewById(R.id.imageSub);
mTextView = view.findViewById(R.id.textView);
mImageAdd = view.findViewById(R.id.imageAdd);
}
//j点击商品事件商品
public interface OnAddItemClick{
void OnAddClick();
}
private OnAddItemClick mOnAddItemClick;
public void setmOnAddItemClick(OnAddItemClick onAddItemClick) {
this.mOnAddItemClick = onAddItemClick;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/imageSub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
app:srcCompat="@drawable/goods_sub_btn" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="57dp"
android:layout_weight="1"
android:gravity="center"
android:text="1"
android:textSize="18dp" />
<ImageView
android:id="@+id/imageAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
app:srcCompat="@drawable/goods_add_btn" />
</LinearLayout>