public void onCheckedChanged(RadioGroup group, int checkedId) { RadioButton radbtn = (RadioButton) findViewById(checkedId); Toast.makeText(getApplicationContext(), "按钮组值发生改变,你选了" + radbtn.getText(), Toast.LENGTH_LONG).show(); } });
获得RadioButton相关信息的方法:
-getClidCont() 获得按钮组中的单选按钮的数目 -getClindAt() 根据索引值获得单选按钮 (参数为索引值 0,1,2,3..) -isChecked() 判断按钮是否被选中(参数为true/false)
CheckBox复选框
1)代码:
<CheckBox android:id="@+id/checkbox" android:text="苹果" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <CheckBox android:id="@+id/checkbox1" android:text="香蕉" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <CheckBox android:id="@+id/checkbox2" android:text="大鸭梨" android:layout_width="wrap_content" android:layout_height="wrap_content" />
2)监听方法:
可以同时选中多个选项,至于获得选中的值,一般是为每个CheckBox添加事件:setOnCheckedChangeListener
public class MyActivity extends Activity implements View.OnClickListener,CompoundButton.OnCheckedChangeListener{ private CheckBox cb_one; private CheckBox cb_two; private CheckBox cb_three; priva
|