关于获取自定义控件(加减控件)及获取其中的子控件

来源:互联网 发布:氧气内衣 知乎 编辑:程序博客网 时间:2024/05/16 06:47

       目前写毕设的时候碰见自定义加减按钮效果图如下:


先说下自定义控件可以挺过自己写的一个累实现,只是在后来的布局XML文件中直接添加就好,例如,上图后面的点击按钮 数量增减,我在这自定义控件为add_sub控件在list.xml 就可以直接使用了,eg :index_detail_item.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"    android:orientation="horizontal" >    <ImageView        android:id="@+id/seller_image"        android:layout_width="0dp"        android:layout_height="fill_parent"        android:scaleType="fitStart"        android:layout_gravity="left"        android:layout_weight="1"         />    <TextView        android:id="@+id/seller_name"        android:layout_width="0dp"        android:layout_height="wrap_content"        android:layout_weight="1.5"                    />    <TextView        android:id="@+id/seller_money"        android:layout_width="0dp"        android:layout_height="wrap_content"        android:layout_weight="0.5"        android:text="TextView" />   <myself.add_sub         android:id="@+id/seller_number"        android:layout_width="0dp"        android:layout_gravity="start"        android:layout_height="wrap_content"        android:layout_weight="1"/> </LinearLayout>
其中add_sub 直接当控件使用。

要获取控件中的数量则需要使用如下代码:

   final View layout2=inflater.inflate(R.layout.index_detail_item)(父控件布局),
(ViewGroup) layout.findViewById(R.id.seller_number)---自定义控件);
    final EditText numEditText=(EditText)layout2.findViewById(R.id.num);


0 0
原创粉丝点击