用纯代码实现RadioGroup效果

 
一.代码如下:
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.ScrollView;
import android.widget.TextView;

/**
 * 用纯代码实现RadioGroup效果
 * */
public class QuestionActivity extends Activity {
    /**
     * 性别问题标题
     * */
    private TextView mSexQuestionTv;
    /**
     * 冠军问题标题
     * */
    private TextView mChampionQuestionTv;
    /**
     * 城市名称问题标题
     * */
    private TextView mCityQuestionTv;
    /**
     * 城市名称问题答案
     * */
    private TextView mCitySolutionTv;
    
    /**
     * 性别问题选项组
     * */
    private RadioGroup mSexQuestionRG;
    /**
     * 冠军问题选项组
     * */
    private RadioGroup mChampionQuestionRG;
    /**
     * 城市名称选项组
     * */
    private RadioGroup mCityQuestionRG;
    
    /**
     * 帅哥
     * */
    private RadioButton mHandsomeBoyRd;
    /**
     * 美女
     * */
    private RadioButton mBelleRd;
    /**
     * 西班牙
     * */
    private RadioButton mSpainRd;
    /**
     * 荷兰
     * */
    private RadioButton mHollandRd;
    /**
     * 德国
     * */
    private RadioButton mGermanyRd;
    /**
     * 北京
     * */
    private RadioButton mBeijingCityRd;
    /**
     * 上海
     * */
    private RadioButton    mShangHaiCityRd;
    /**
     * 广州
     * */
    private RadioButton mGuangZhouRd;
    /**
     * 深圳
     * */
    private RadioButton mShenZhenCityRd;
    private ScrollView mScrollView;
    
    /**
     * 问题根部局
     * */
    private LinearLayout mRootLl;
    /**
     * RadioGroup布局参数
     * */
    RadioGroup.LayoutParams mRadioParams;
    /**
     * 线性布局参数
     * */
    LinearLayout.LayoutParams mLinearParams;
    /**
     * 设置布局的宽高
     * */
    private final int mWidth = LinearLayout.LayoutParams.FILL_PARENT;
    /**
     * 设置布局的宽高
     * */
    private final int mHeight = LinearLayout.LayoutParams.FILL_PARENT;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        mScrollView = new ScrollView(this);
        /**
         * 设置滚屏布局的宽高布局
         * */
        mScrollView.setLayoutParams(new LayoutParams(mWidth, mHeight));

        mRootLl = new LinearLayout(this);
        // sLinerLayout.
        // sLinerLayout.setBackgroundResource(R.drawable.back);
        mRootLl.setBackgroundColor(Color.BLACK);
        /**
         * 设置Root线性布局的宽高
         * */
        mRootLl.setLayoutParams(new LayoutParams(mWidth, mHeight));
        mRootLl.setOrientation(LinearLayout.VERTICAL);
        mLinearParams = new LinearLayout.LayoutParams(mWidth, mHeight);
        mScrollView.addView(mRootLl);

        mSexQuestionTv = new TextView(this);
        mSexQuestionTv.setText("请问你是");
        mRootLl.addView(mSexQuestionTv, mLinearParams);
        mSexQuestionRG = new RadioGroup(this);
        mSexQuestionRG.setOrientation(RadioGroup.VERTICAL);
        mLinearParams = new LinearLayout.LayoutParams(mWidth, mHeight);
        mRootLl.addView(mSexQuestionRG, mLinearParams);
        
        mHandsomeBoyRd = new RadioButton(this);
        mHandsomeBoyRd.setText("帅哥");
        /**
         * 设置ID
         * */
        mHandsomeBoyRd.setId(1001);
        mRadioParams = new RadioGroup.LayoutParams(mWidth, mHeight);
        mSexQuestionRG.addView(mHandsomeBoyRd, mRadioParams);
        mBelleRd = new RadioButton(this);
        mBelleRd.setText("美女");
        /**
         * 设置ID
         * */
        mBelleRd.setId(1002);
        mRadioParams = new RadioGroup.LayoutParams(mWidth, mHeight);
        mSexQuestionRG.addView(mBelleRd, mRadioParams);

        mChampionQuestionTv = new TextView(this);
        mChampionQuestionTv.setText("2010南非世界杯季军是");
        mRootLl.addView(mChampionQuestionTv, mLinearParams);
        
        mChampionQuestionRG = new RadioGroup(this);
        mChampionQuestionRG.setOrientation(RadioGroup.VERTICAL);
    
        mLinearParams = new LinearLayout.LayoutParams(mWidth, mHeight);
        mRootLl.addView(mChampionQuestionRG, mLinearParams);
        
        mSpainRd = new RadioButton(this);
        mSpainRd.setText("西班牙");
        /**
         * 设置ID
         * */
        mSpainRd.setId(1003);
        mRadioParams = new RadioGroup.LayoutParams(mWidth, mHeight);
        mChampionQuestionRG.addView(mSpainRd, mRadioParams);
        mHollandRd = new RadioButton(this);
        mHollandRd.setText("荷兰");
        /**
         * 设置ID
         * */
        mHollandRd.setId(1004);
        mRadioParams = new RadioGroup.LayoutParams(mWidth, mHeight);
        mChampionQuestionRG.addView(mHollandRd, mRadioParams);
        mGermanyRd = new RadioButton(this);
        mGermanyRd.setText("德国");
        /**
         * 设置ID
         * */
        mGermanyRd.setId(1005);
        mRadioParams = new RadioGroup.LayoutParams(mWidth, mHeight);
        mChampionQuestionRG.addView(mGermanyRd, mRadioParams);

        mCityQuestionTv = new TextView(this);
        mCityQuestionTv.setText("2010亚运会在哪个城市举行: ");
        mRootLl.addView(mCityQuestionTv, mLinearParams);
        
        mCityQuestionRG = new RadioGroup(this);
        mCityQuestionRG.setOrientation(RadioGroup.VERTICAL);
      
        mLinearParams = new LinearLayout.LayoutParams(mWidth, mHeight);
        mRootLl.addView(mCityQuestionRG, mLinearParams);
        
        mBeijingCityRd = new RadioButton(this);
        mBeijingCityRd.setText("北京");
        /**
         * 设置ID
         * */
        mBeijingCityRd.setId(1006);
        mRadioParams = new RadioGroup.LayoutParams(mWidth, mHeight);
        mCityQuestionRG.addView(mBeijingCityRd, mRadioParams);
        mShangHaiCityRd = new RadioButton(this);
        mShangHaiCityRd.setText("上海");
        /**
         * 设置ID
         * */
        mShangHaiCityRd.setId(1007);
        mRadioParams = new RadioGroup.LayoutParams(mWidth, mHeight);
        mCityQuestionRG.addView(mShangHaiCityRd, mRadioParams);
        mGuangZhouRd = new RadioButton(this);
        mGuangZhouRd.setText("广州");
        /**
         * 设置ID
         * */
        mGuangZhouRd.setId(1008);
        mRadioParams = new RadioGroup.LayoutParams(mWidth, mHeight);
        mCityQuestionRG.addView(mGuangZhouRd, mRadioParams);
        mShenZhenCityRd = new RadioButton(this);
        mShenZhenCityRd.setText("深圳");
        /**
         * 设置ID
         * */
        mShenZhenCityRd.setId(1009);
        mRadioParams = new RadioGroup.LayoutParams(mWidth, mHeight);
        mCityQuestionRG.addView(mShenZhenCityRd, mRadioParams);
        
        mCitySolutionTv = new TextView(this);
        mCitySolutionTv.setText("答案: ");
        mRootLl.addView(mCitySolutionTv, mLinearParams);
        
        /**
         * 设置当前的布局
         * */
        setContentView(mScrollView);

        mSexQuestionRG.setOnCheckedChangeListener(mRadioChangeListener);
        mChampionQuestionRG.setOnCheckedChangeListener(mRadioChangeListener);
        mCityQuestionRG.setOnCheckedChangeListener(mRadioChangeListener);
    }

    /**
     * 选项选择改变的事件
     * */
    private RadioGroup.OnCheckedChangeListener mRadioChangeListener = new RadioGroup.OnCheckedChangeListener() {
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            if (checkedId == mHandsomeBoyRd.getId()) {
                mSexQuestionTv.setText(mBelleRd.getText());
            } else if (checkedId == mBelleRd.getId()) {
                mSexQuestionTv.setText(mBelleRd.getText());
            } else if (checkedId == mSpainRd.getId()) {
                mChampionQuestionTv.setText(mSpainRd.getText());
            } else if (checkedId == mHollandRd.getId()) {
                mChampionQuestionTv.setText(mHollandRd.getText());
            } else if (checkedId == mGermanyRd.getId()) {
                mChampionQuestionTv.setText(mGermanyRd.getText());
            } else if (checkedId == mBeijingCityRd.getId()) {
                mCitySolutionTv.setText("答案: " + mBeijingCityRd.getText());
            } else if (checkedId == mShangHaiCityRd.getId()) {
                mCitySolutionTv.setText("答案: " + mShangHaiCityRd.getText());
            } else if (checkedId == mGuangZhouRd.getId()) {
                mCitySolutionTv.setText("答案: " + mGuangZhouRd.getText());
            } else if (checkedId == mShenZhenCityRd.getId()) {
                mCitySolutionTv.setText("答案: " + mShenZhenCityRd.getText());
            }
        }
    };

}


二.需要在清单文件中添加

                   android:name=".QuestionActivity"
            android:label="@string/title_activity_question" 

            android:theme="@android:style/Theme.Translucent"/>


三.最后在MainActivity 中调用gotoQuestionActivity()函数就可以看到效果。

    /**
     * 跳转到自定义RadioGroup
     * */
    private void gotoQuestionActivity(Context content){
        Intent intent = new Intent(MainActivity.this, QuestionActivity.class);
        content.startActivity(intent);
    }


 

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