MainActivity
package com.sub.subview;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private SubView sub_view;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
sub_view.setMaxValue(20);
sub_view.setOnNumChangeListener(new SubView.OnNumChangeListener() {
@Override
public void OnNumChangeListener(int value) {
Toast.makeText(MainActivity.this, value+"",Toast.LENGTH_SHORT).show();
}
});
}
private void initView() {
sub_view = (SubView) findViewById(R.id.sub_view);
}
}
自定义控件 内部数据的增减 使用接口回调
package com.sub.subview;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;
public class SubView extends FrameLayout implements View.OnClickListener {
private Button sub;
private Button add;
private TextView text;
public int max = 20;
public SubView(@NonNull Context context) {
this(context,null);
}
public SubView(@NonNull Context context,@Nullable AttributeSet attrs) {
this(context, attrs,0);
}
public SubView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView(context);
}
//初始化资源控件
private void initView(Context context) {
View view = View.inflate(context, R.layout.activity_sub, this);
sub = view.findViewById(R.id.sub);
add = view.findViewById(R.id.add);
text = view.findViewById(R.id.text);
sub.setOnClickListener(this);
add.setOnClickListener(this);
int textValue = getValue();
setValue(textValue);
}
private void setValue(int textValue) {
text.setText(textValue+"");
}
private int value = 1 ;
private int getValue() {
String trim = text.getText().toString().trim();
if (!TextUtils.isEmpty(trim)){
value = Integer.valueOf(trim);
}
return value;
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.add:
addNumValue();
break;
case R.id.sub:
subNumValue();
break;
}
}
private void subNumValue() {
if (value > 1){
value--;
}
setValue(value);
mOnNumChangeListener.OnNumChangeListener(value);
}
public void setMaxValue(int maxx){
max= maxx;
}
private void addNumValue() {
if (value < max){
value++;
}
setValue(value);
mOnNumChangeListener.OnNumChangeListener(value);
}
//接口回调
public interface OnNumChangeListener{
void OnNumChangeListener(int value);
}
private OnNumChangeListener mOnNumChangeListener;
public void setOnNumChangeListener(OnNumChangeListener onNumChangeListener){
mOnNumChangeListener = onNumChangeListener;
// onNumChangeListener = mOnNumChangeListener;
}
}
接下来 看一下布局文件
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.sub.subview.SubView
android:id="@+id/sub_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></com.sub.subview.SubView>
</LinearLayout>
组合式控件的布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="match_parent">
<Button
android:id="@+id/sub"
android:text="-"
android:layout_width="100dp"
android:background="@drawable/shape"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/text"
android:text="1"
android:textSize="25sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/add"
android:text="+"
android:background="@drawable/shape"
android:layout_width="100dp"
android:layout_height="wrap_content" />
</LinearLayout>
稍微加一个shape 美化一下按钮
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/colorAccent"/>
<stroke android:width="1dp"
android:color="@color/colorAccent"/>
<corners android:radius="30dp"/>
</shape>