Android RadioButton与TextView浪漫约会?

情景一
今天主要实现一个国家与地区切换,就是当我们选中RadioButton时然后将值设置到TextView中,听着这需求应该不难对吧?那么我们就开始约会吧?

看下原型图

Android RadioButton与TextView浪漫约会?_第1张图片

准备条件:

  1. 首先需要一个radiobutton然后一个textview作为子item
  2. 需要一个适配器这里不再多赘述
  3. 需要写回调方法 void onItemClickListener(int position);
  4. 在主Activity 需要将适配器与数据源绑定然后进行回调将当前选中的标题设置到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>

效果

Android RadioButton与TextView浪漫约会?_第2张图片

  1. 适配器RadioAdapter实现
package com.visoport.medicine.ui.activity.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.visoport.medicine.R;


/**
 * 适配器
 */

public class RadioAdapter extends BaseAdapter {

    private LayoutInflater inflater;
    private String[] areas;
    private viewHolder holder;
    // 标记用户当前选择那个地区
    private int index = -1;
    private Context c;
    private   OnItemSelectedListener listener;

    public interface OnItemSelectedListener{
        void onItemClickListener(int position);
    }

    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() {
        return areas.length;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        holder = new viewHolder();
        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(new CompoundButton.OnCheckedChangeListener() {


                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        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);
        }
        return convertView;
    }

    public class viewHolder {
        public TextView nameTxt;
        public RadioButton selectBtn;
    }
}

  1. 需要写回调方法 void onItemClickListener(int position);
 private   OnItemSelectedListener listener;

    public interface OnItemSelectedListener{
        void onItemClickListener(int position);
    }

    public void setOnItemSelectedLstenerner(OnItemSelectedListener listener) {
        this.listener = listener;
    }
      holder.selectBtn
                .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {


                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        if (isChecked) {
                            Toast.makeText(c, "您:" + areas[position], Toast.LENGTH_LONG).show();
                            if (listener != null) {
                                listener.onItemClickListener(position);
                            }
                            index = position;
                            notifyDataSetChanged();
                        }
                    }
                });

4、主Activity调用

package com.visoport.medicine;

import android.os.Bundle;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.visoport.medicine.ui.activity.activity.base.BaseActivity;
import com.visoport.medicine.ui.activity.adapter.RadioAdapter;

/**
 * 国家与地区
 */
public class CounrtyAreaActivity extends BaseActivity {
    private ListView radioButtonList;

    private RadioAdapter adapter;

    private String[] areas = {"香港", "澳门", "台湾", "中国大陆", "新加坡",
            "马来西亚", "美国", "日本"};
    private TextView tv_country_area_name;
    private RadioGroup 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 = new RadioAdapter(this, areas);
        radioButtonList.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        //回调
        adapter.setOnItemSelectedLstenerner(new RadioAdapter.OnItemSelectedListener() {
            @Override
            public void onItemClickListener(int position) {
                tv_country_area_name.setText(areas[position]);
            }
        });
    }

    @Override
    protected void handler(Message msg) {

    }

    @Override
    protected void initContentView(Bundle savedInstanceState) {

    }
}

我这里用的是listview,大家也可以用RecycleView去尝试都不难!
效果图如下

Android RadioButton与TextView浪漫约会?_第3张图片

补充说明:
点击RadioButton背景切换


<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>

主布局


<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这里那个recycleview需要源码的可以直接在博客左边加群或者加我QQ1040271995欢迎大家批评指导!

你可能感兴趣的:(Android基础篇)