package com.linquan.test.radiobutton;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
public class test_RadioButton extends Activity {
TextView m_TextView;
RadioGroup rg;
RadioButton rb1,rb2,rb3,rb4;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
m_TextView = (TextView)this.findViewById(R.id.tv1);
rg = (RadioGroup)this.findViewById(R.id.rg1);
rb1 = (RadioButton)this.findViewById(R.id.rb1);
rb2 = (RadioButton)this.findViewById(R.id.rb2);
rb3 = (RadioButton)this.findViewById(R.id.rb3);
rb4 = (RadioButton)this.findViewById(R.id.rb4);
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
if(checkedId == rb2.getId()){
DisplayToast("正确答案:"+ rb2.getText()+",恭喜你回答正确!");
}
else{
DisplayToast("请注意,回答错误!");
}
}
});
}
public void DisplayToast(String msg){
Toast toast=Toast.makeText(this, msg, Toast.LENGTH_LONG);
//设置toast显示位置
toast.setGravity(Gravity.TOP, 0, 220);
toast.show();
}
}