android学习案例3——RadioGroup和RadioButton

android学习案例3——RadioGroup和RadioButton_第1张图片

                                                (图片来源慕课网)

Demo效果:

android学习案例3——RadioGroup和RadioButton_第2张图片

activity_main.xml代码:



        

        
    

    

        

        
    

Java代码:

public class MainActivity extends ActionBarActivity {
    private RadioGroup rg;
    private TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        rg = (RadioGroup) findViewById(R.id.rg);
        tv = (TextView) findViewById(R.id.tv);
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                String tip = checkedId == R.id.male ? "男性" : "女性";
                tv.setText(tip);
            }
        });
    }
}





你可能感兴趣的:(个人笔记,android,RadioGroup,RadioButton)