Android 单选框RadioGroup
16lz
2021-01-23
Android 单选框RadioGrop的使用。里面添加的是RadioButton
package com.radiodemo;import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.widget.RadioButton;import android.widget.RadioGroup;import android.widget.TextView;public class MainActivity extends Activity {/************************************* * 单选框RadioButton的使用 * * 1.定义见布局文件 2.事件的添加方法 * ***********************************/private TextView textView;private RadioGroup radioGroup;private RadioButton radioButton1, radioButton2;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);// 关联布局中的控件textView = (TextView) findViewById(R.id.textView);// 用来显示选择radioGroup = (RadioGroup) findViewById(R.id.radioGroup);radioButton1 = (RadioButton) findViewById(R.id.radioButton1);radioButton2 = (RadioButton) findViewById(R.id.radioButton2);radioGroup.setOnCheckedChangeListener(mChangeListener);}// 响应事件的函数。private RadioGroup.OnCheckedChangeListener mChangeListener = new RadioGroup.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) {// TODO Auto-generated method stubif (radioButton1.getId() == checkedId) {textView.setText(radioButton1.getText());} else {textView.setText(radioButton2.getText());}}};@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}
布局文件main.xml
更多相关文章
- Android触控事件
- [转]android layout布局属性
- Android Layout 布局属性全解
- AndroiManifest.xml文件中android属性
- 常见UI布局
- Android 将从网络获取的数据缓存到私有文件