情景一 今天主要实现一个国家与地区切换,就是当我们选中 RadioButton 时然后将值设置到 TextView 中,听着这需求应该不难对吧?那么我们就开始约会吧?
看下原型图
准备条件:
下面看下具体的实现流程
1、看下布局文件子 item radiobutton.xml
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <RadioGroup
- android:id="@+id/rg_area"
- android:layout_alignParentTop="true"
- android:layout_marginBottom="20dp"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content">/></RadioGroup>
- <RadioButton
- android:id="@+id/radio"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentRight="true"
- android:layout_marginTop="10dp"
- android:button="@drawable/check_box_selected"
- android:background="@null"
- android:focusable="true"
- android:checked="false"
- android:clickable="true"
- android:layout_marginRight="10dp"/>
- <TextView
- android:id="@+id/tv_country_and_area"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"
- android:layout_marginTop="10dp"
- android:text="香港"
- android:layout_marginLeft="10dp"
- android:textSize="22sp"/>
- </RelativeLayout>
效果
- packagecom.visoport.medicine.ui.activity.adapter;importandroid.content.Context;importandroid.view.LayoutInflater;importandroid.view.View;importandroid.view.ViewGroup;importandroid.widget.BaseAdapter;importandroid.widget.CompoundButton;importandroid.widget.RadioButton;importandroid.widget.TextView;importandroid.widget.Toast;importcom.visoport.medicine.R;/**
- * 适配器
- */
- public class RadioAdapter extends BaseAdapter{
- privateLayoutInflater inflater;privateString[] areas;privateviewHolder holder;// 标记用户当前选择那个地区
- private intindex = -1;privateContext c;privateOnItemSelectedListener listener;public interface OnItemSelectedListener{
- voidonItemClickListener(intposition);
- }public void setOnItemSelectedLstenerner(OnItemSelectedListener listener) {this.listener = listener;
- }public RadioAdapter(Context c, String[] areas) {super();this.c = c;this.areas = areas;
- inflater = LayoutInflater.from(c);
- }@Override
- public int getCount() {returnareas.length;
- }@Override
- publicObjectgetItem(intposition) {return null;
- }@Override
- public long getItemId(intposition) {return 0;
- }@Override
- publicViewgetView(final intposition, View convertView, ViewGroup parent) {
- holder =newviewHolder();if(convertView ==null) {
- convertView = inflater.inflate(R.layout.item_radiobutton,null);
- holder.nameTxt = (TextView) convertView.findViewById(R.id.tv_country_and_area);
- holder.selectBtn = (RadioButton) convertView.findViewById(R.id.radio);
- convertView.setTag(holder);
- }else{
- holder = (viewHolder) convertView.getTag();
- }
- holder.nameTxt.setText(areas[position]);
- holder.selectBtn
- .setOnCheckedChangeListener(newCompoundButton.OnCheckedChangeListener() {@Override
- public void onCheckedChanged(CompoundButton buttonView,booleanisChecked) {if(isChecked) {
- Toast.makeText(c,"您选择的地区是:"+ areas[position], Toast.LENGTH_LONG).show();if(listener !=null) {
- listener.onItemClickListener(position);
- }
- index = position;
- notifyDataSetChanged();
- }
- }
- });if(index == position) {// 选中的条目和当前的条目是否相等holder.selectBtn.setChecked(true);
- }else{
- holder.selectBtn.setChecked(false);
- }returnconvertView;
- }public class viewHolder{
- publicTextView nameTxt;publicRadioButton selectBtn;
- }
- }
- privateOnItemSelectedListener listener;public interface OnItemSelectedListener{
- voidonItemClickListener(intposition);
- }public void setOnItemSelectedLstenerner(OnItemSelectedListener listener) {this.listener = listener;
- }
- holder.selectBtn
- .setOnCheckedChangeListener(newCompoundButton.OnCheckedChangeListener() {@Override
- public void onCheckedChanged(CompoundButton buttonView,booleanisChecked) {if(isChecked) {
- Toast.makeText(c,"您:"+ areas[position], Toast.LENGTH_LONG).show();if(listener !=null) {
- listener.onItemClickListener(position);
- }
- index = position;
- notifyDataSetChanged();
- }
- }
- });
4、主 Activity 调用
- packagecom.visoport.medicine;importandroid.os.Bundle;importandroid.os.Message;importandroid.support.annotation.Nullable;importandroid.support.v7.widget.Toolbar;importandroid.widget.ListView;importandroid.widget.RadioGroup;importandroid.widget.TextView;importcom.visoport.medicine.ui.activity.activity.base.BaseActivity;importcom.visoport.medicine.ui.activity.adapter.RadioAdapter;/**
- * 国家与地区
- */
- public class CounrtyAreaActivity extends BaseActivity{
- privateListView radioButtonList;privateRadioAdapter adapter;privateString[] areas = {"香港","澳门","台湾","中国大陆","新加坡","马来西亚","美国","日本"};privateTextView tv_country_area_name;privateRadioGroup rg_area;// private Button btn_ok;
- @Override
- protected void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);
- setContentView(R.layout.item_courty_area);
- Toolbar toolbar=initToolBar(true,"国家与地区");
- toolbar.setNavigationIcon(R.drawable.ic_back);
- radioButtonList = (ListView) findViewById(R.id.list);
- tv_country_area_name = (TextView) findViewById(R.id.tv_country_area_name);
- rg_area= (RadioGroup) findViewById(R.id.rg_area);
- adapter =newRadioAdapter(this, areas);
- radioButtonList.setAdapter(adapter);
- adapter.notifyDataSetChanged();//回调adapter.setOnItemSelectedLstenerner(newRadioAdapter.OnItemSelectedListener() {@Override
- public void onItemClickListener(intposition) {
- tv_country_area_name.setText(areas[position]);
- }
- });
- }@Override
- protected void handler(Message msg) {
- }@Override
- protected void initContentView(Bundle savedInstanceState) {
- }
- }
我这里用的是 listview,大家也可以用 RecycleView 去尝试都不难! 效果图如下
补充说明: 点击 RadioButton 背景切换
- <?xml version="1.0" encoding="utf-8" ?>
- <selector xmlns:android="http://schemas.android.com/apk/res/android">
- <item android:state_checked="true" android:drawable="@drawable/ic_checked"
- />
- <item android:state_checked="false" android:drawable="@drawable/ic_uncheck"
- />
- <item android:drawable="@drawable/ic_uncheck" />
- </selector>
主布局
- <?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="vertical">
- <include
- android:id="@+id/head_title_menu"
- layout="@layout/custome_toolbar"/>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginTop="10dp"
- android:orientation="horizontal">
- <TextView
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:gravity="center"
- android:layout_weight="1"
- android:text="当前选择国家:"
- android:textSize="16sp"/>
- <TextView
- android:id="@+id/tv_country_area_name"
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:text="香港"
- android:layout_weight="1"
- android:textSize="16sp"/>
- </LinearLayout>
- <ListView
- android:id="@+id/list"
- android:layout_width="match_parent"
- android:layout_height="480dp"
- android:layout_marginTop="10dp"
- android:scrollbars="none"/>
- </LinearLayout>
来源: http://blog.csdn.net/qq_15950325/article/details/70260652