组合view之接口传递

来源:互联网 发布:hp1022n网络打印驱动 编辑:程序博客网 时间:2024/06/08 00:03
//主页面public class MainActivity extends AppCompatActivity {    private AddDeleteView adv;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        adv = (AddDeleteView) findViewById(R.id.add_delete_view);        adv.setOnAddDelClickListener(new AddDeleteView.OnAddDelClickListener() {            @Override            public void OnAddClick(View v) {                int origin = adv.getNumber();                origin++;                adv.setNumber(origin);            }            @Override            public void OnDelClick(View v) {                int origin = adv.getNumber();                origin--;                adv.setNumber(origin);            }        });    }}// 继承页面
public class AddDeleteView extends LinearLayout{    private EditText et_num;    private OnAddDelClickListener listener;    //定义接口    interface OnAddDelClickListener{        void OnAddClick(View v);        void OnDelClick(View v);    }    public void setOnAddDelClickListener(OnAddDelClickListener listener){          if(listener!=null){               this.listener=listener;          }    }    public AddDeleteView(Context context) {        this(context,null);    }    public AddDeleteView(Context context, @Nullable AttributeSet attrs) {        this(context, attrs,0);    }    public AddDeleteView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);        initView(context, attrs, defStyleAttr);    }    private void initView(Context context, AttributeSet attrs, int defStyleAttr) {        View.inflate(context,R.layout.layout_adddel,this);        TextView tv_del=(TextView)findViewById(R.id.tv_del);        TextView tv_add=(TextView)findViewById(R.id.tv_add);        et_num = (EditText) findViewById(R.id.et_num);        tv_del.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View view) {                listener.OnDelClick(view);            }        });        tv_add.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View view) {                listener.OnAddClick(view);            }        });    }    public void setNumber(int number) {        if (number > 0) {            et_num.setText(number + "");        }    }    public int getNumber() {        int number = 0;        try {            String numberStr = et_num.getText().toString().trim();            number = Integer.valueOf(numberStr);        } catch (Exception e) {            number = 0;        }        return number;    }}
//主xml页面
<LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">    <com.day01.AddDeleteView        android:id="@+id/add_delete_view"        android:layout_width="wrap_content"        android:layout_height="wrap_content">    </com.day01.AddDeleteView></LinearLayout>
//自定义xml页面
<?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">    <TextView        android:id="@+id/tv_del"        android:textSize="50dp"        android:background="#ccc"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="-" />    <EditText        android:id="@+id/et_num"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_weight="1"        android:gravity="center"        android:text="0"/>    <TextView        android:id="@+id/tv_add"        android:textSize="50dp"        android:background="#ccc"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="+" /></LinearLayout>