您的位置:首页 > 其它

RadioGroup动态添加RadioButton,并且获得事件

2016-01-11 10:58 579 查看
原先的xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:background="#ffffff"
android:orientation="vertical" >

<com.lvche.origin.view.HomeActivityTitleView
android:id="@+id/homeTitle"
android:layout_width="match_parent"
android:layout_height="45dp"
android:background="#fff888" />

<ScrollView
android:layout_width="match_parent"
android:scrollbars="none"
android:layout_height="wrap_content" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:ignore="ScrollViewSize" >

<!-- 滚动 轮播图片 -->

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:orientation="vertical" >

<com.lvche.origin.activity.slide_imgs.SlideViewPager
android:id="@+id/slide_viewpager"
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="#fff888" />

<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/slide_viewpager"
android:layout_centerHorizontal="true"
android:button="@drawable/btn_radio_holo_light1"
android:clickable="false"
android:orientation="horizontal" >

<RadioButton
android:id="@+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:button="@drawable/btn_radio_holo_light1"
android:checked="true"
android:clickable="false" />

<RadioButton
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:button="@drawable/btn_radio_holo_light1"
android:clickable="false" />

<RadioButton
android:id="@+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:button="@drawable/btn_radio_holo_light1"
android:clickable="false" />

<RadioButton
android:id="@+id/radio3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:button="@drawable/btn_radio_holo_light1"
android:clickable="false" />

<RadioButton
android:id="@+id/radio4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:button="@drawable/btn_radio_holo_light1"
android:clickable="false" />
</RadioGroup>
</RelativeLayout>
<!-- 滚动 轮播图片  end -->
<!-- 地图导航 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="58dp"
android:orientation="horizontal" >

<ImageView
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="25dp"
android:background="@drawable/logo_pgg" />

<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="25dp"
android:background="@drawable/logo_pgg" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
android:text="020国际皮革五金城导航"
tools:ignore="HardcodedText" />
</LinearLayout>
<ImageView
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#aaaaaa" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="58dp"
android:orientation="horizontal" >

<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.21"
android:src="@drawable/correcting_border"
android:gravity="center_vertical|center_horizontal"
android:text="-1F"
android:textColor="#ffffff"
android:textSize="25sp"
android:background="#555fff"
android:textStyle="bold" />

<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.68"
android:orientation="vertical"
android:paddingLeft="6dp" >

<TextView
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="0.59"
android:gravity="center_vertical"
android:text="富一层"
android:textColor="#555555"
android:textSize="18sp"
tools:ignore="HardcodedText" />

<TextView
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:textColor="#999999"
android:textSize="12sp"
android:text="羊皮、山东大皮、进口头层"
tools:ignore="HardcodedText" />
</LinearLayout>

<ImageView
android:layout_width="0dp"
android:layout_height="25dp"
android:layout_gravity="center_vertical"
android:layout_weight="0.1"
android:background="@drawable/logo_pgg" />
</LinearLayout>

<ImageView
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#aaaaaa" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="58dp"
android:orientation="horizontal" >

<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.21"
android:src="@drawable/correcting_border"
android:gravity="center_vertical|center_horizontal"
android:text="1F"
android:textColor="#ffffff"
android:background="#8ffeee"
android:textSize="25sp"
android:textStyle="bold" />

<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.68"
android:orientation="vertical"
android:paddingLeft="6dp" >

<TextView
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="0.59"
android:gravity="center_vertical"
android:text="一层"
android:textColor="#555555"
android:textSize="18sp"
tools:ignore="HardcodedText" />

<TextView
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:textColor="#999999"
android:textSize="12sp"
android:text="绵羊皮、山羊皮、超纤、PU革"
tools:ignore="HardcodedText" />
</LinearLayout>

<ImageView
android:layout_width="0dp"
android:layout_height="25dp"
android:layout_gravity="center_vertical"
android:layout_weight="0.1"
android:background="@drawable/logo_pgg" />
</LinearLayout>
<ImageView
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#aaaaaa" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="58dp"
android:orientation="horizontal" >

<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.21"
android:src="@drawable/correcting_border"
android:gravity="center_vertical|center_horizontal"
android:text="2F"
android:background="#00ffaa"
android:textColor="#ffffff"
android:textSize="25sp"
android:textStyle="bold" />

<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.68"
android:orientation="vertical"
android:paddingLeft="6dp" >

<TextView
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="0.59"
android:gravity="center_vertical"
android:text="二层"
android:textColor="#555555"
android:textSize="18sp"
tools:ignore="HardcodedText" />

<TextView
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:textColor="#999999"
android:textSize="12sp"
android:text="插口、铁线扣、压铸件、其他小配件"
tools:ignore="HardcodedText" />
</LinearLayout>

<ImageView
android:layout_width="0dp"
android:layout_height="25dp"
android:layout_gravity="center_vertical"
android:layout_weight="0.1"
android:background="@drawable/logo_pgg" />
</LinearLayout>
<ImageView
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#aaaaaa" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="58dp"
android:orientation="horizontal" >

<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.21"
android:src="@drawable/correcting_border"
android:gravity="center_vertical|center_horizontal"
android:text="3F"
android:textColor="#ffffff"
android:background="#ff00ff"
android:textSize="25sp"
android:textStyle="bold" />

<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.68"
android:orientation="vertical"
android:paddingLeft="6dp" >

<TextView
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="0.59"
android:gravity="center_vertical"
android:text="三层"
android:textColor="#555555"
android:textSize="18sp"
tools:ignore="HardcodedText" />

<TextView
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:textColor="#999999"
android:textSize="12sp"
android:text="中空钉、9字钉、其他小配件"
tools:ignore="HardcodedText" />
</LinearLayout>

<ImageView
android:layout_width="0dp"
android:layout_height="25dp"
android:layout_gravity="center_vertical"
android:layout_weight="0.1"
android:background="@drawable/logo_pgg" />
</LinearLayout>
<ImageView
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#aaaaaa" />
<!-- 地图导航  end -->

<!-- 广告 -->

<LinearLayout
android:layout_width="match_parent"
android:layout_height="58dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
android:textColor="#888888"
android:text="新品发布"
android:textSize="20sp"
tools:ignore="HardcodedText" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="150dp"
android:orientation="horizontal"
android:layout_marginBottom="15dp"
>
<ImageView
android:layout_height="fill_parent"
android:layout_width="0dp"
android:background="@drawable/logo_pgg"
android:layout_weight="0.48"
android:layout_marginRight="5dp"
/>
<LinearLayout
android:layout_height="fill_parent"
android:layout_width="0dp"
android:orientation="vertical"
android:layout_weight="0.5">
<ImageView
android:layout_height="0dp"
android:background="@drawable/e"
android:layout_width="fill_parent"
android:layout_weight="0.49"
android:layout_marginBottom="5dp"
/>
<ImageView
android:layout_height="0dp"
android:background="@drawable/a"
android:layout_width="fill_parent"
android:layout_weight="0.49"
/>
</LinearLayout>
</LinearLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="@drawable/c"
android:layout_marginBottom="15dp"
/>
<ImageView
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="@drawable/c"
android:layout_marginBottom="15dp"
/>

<ImageView
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="@drawable/c"
android:layout_marginBottom="15dp"
/>
<!-- 广告  end -->
</LinearLayout>
</ScrollView>

</LinearLayout>


由于有许多的RadioButton是动态的,不是固定的一些,所以需要在代码中,动态的添加到RadioGroup中,下面是我的实现方法。

1、添加RadioButton到RadioGroup中

[java] view
plain copy

RadioGroup group;
for(int i=0; i<10; i++)
{
RadioButton tempButton = new RadioButton(this);
tempButton.setBackgroundResource(R.drawable.xxx); // 设置RadioButton的背景图片
tempButton.setButtonDrawable(R.drawable.xxx); // 设置按钮的样式
tempButton.setPadding(80, 0, 0, 0); // 设置文字距离按钮四周的距离
tempButton.setText("按钮 " + i);
group.addView(tempButton, LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
}

2、为RadioGroup添加事件处理,可以得到当前选择的RadioButton

[java] view
plain copy

group.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
RadioButton tempButton = (RadioButton)findViewById(checkedId); // 通过RadioGroup的findViewById方法,找到ID为checkedID的RadioButton
// 以下就可以对这个RadioButton进行处理了
}
});
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: