//主页面 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>
组合view之接口传递
最新推荐文章于 2022-12-13 11:05:32 发布
本文介绍了一个自定义的Android视图组件AddDeleteView,该组件实现了数值的增加和减少功能,并通过接口回调的方式通知主页面进行更新。组件内部包含了一个EditText用于显示当前数值,两个TextView分别作为增加和减少按钮。
摘要由CSDN通过智能技术生成