package com.example.android_day03_radiobutton_01;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.widget.RadioGroup.OnCheckedChangeListener;
/**
* TODO 选中哪个RadioButton 弹出对应的Toast
*
* 1.初始化控件
* -声明控件
* -控件赋值
* 2.设置监听事件
* 3.弹Toast
*
*/
public class MainActivity extends Activity {
private RadioGroup rGroup;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rGroup = (RadioGroup) findViewById(R.id.radio_gp);
//设置选中监听事件
/**
* 1.点击事件
* 2.触摸事件
* 3.选中事件
* ........
*/
//选中监听事件
rGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
/**
* 参数1: RadioGroup 你操作的RadioGroup
* 参数2: checkedId 选中RadioButton的id
*/
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
switch (checkedId) {
case R.id.chat:
Toast.makeText(MainActivity.this, "你选中了聊天", Toast.LENGTH_SHORT).show();
break;
case R.id.contacts:
Toast.makeText(MainActivity.this, "你选中了好友", Toast.LENGTH_SHORT).show();
break;
case R.id.discover:
Toast.makeText(MainActivity.this, "你选中了发现", Toast.LENGTH_SHORT).show();
break;
case R.id.me:
Toast.makeText(MainActivity.this, "你选中了我", Toast.LENGTH_SHORT).show();
break;
}
}
});
}
}
Android-day03-RadioButton
最新推荐文章于 2022-07-05 20:44:06 发布