RadioGroup基本使用方法

一.布局文件

"http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    id="@+id/tv_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="listview示例" />

    id="@+id/rg_radiogroup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:contentDescription="适配器模式"
        >
        id="@+id/rb_arrayadapter"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ArrayAdapter"
            />
        id="@+id/rb_simpleadapter"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="SimpleAdapter"
            />
        id="@+id/rb_baseadapter"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="BaseAdapter"
            />    
    
    id="@+id/lv_listviewdemo"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"/>

二.java文件

package com.m520it.listviewreview;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

public class MainActivity extends Activity {
    private RadioButton arrRadio;
    private RadioButton baseRadio;
    private RadioButton simpleRadio;
    private RadioGroup meduleradiogroup;
    private ListView lvdemo;
    private TextView titletv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
    }

    public void initView(){
        setContentView(R.layout.activity_main);
        arrRadio = (RadioButton) findViewById(R.id.rb_arrayadapter);
        baseRadio = (RadioButton) findViewById(R.id.rb_baseadapter);
        simpleRadio = (RadioButton) findViewById(R.id.rb_simpleadapter);
        meduleradiogroup = (RadioGroup) findViewById(R.id.rg_radiogroup);
        lvdemo = (ListView)findViewById(R.id.lv_listviewdemo);
        titletv = (TextView)findViewById(R.id.tv_title);
        meduleradiogroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                case R.id.rb_arrayadapter:
                    titletv.setText("array");
                    break;
                case R.id.rb_baseadapter:
                    titletv.setText("base");
                    break;
                case R.id.rb_simpleadapter:
                    titletv.setText("simple");
                    break;
                }
            }
        });
    }
}

你可能感兴趣的:(RadioGroup,android)