單選題里會用到radiobutton,如果不想使用系統提供的圓圈樣式,可以自定義樣式,想要做成的效果就是,
使用自定義的圖片替換圓圈,然后選擇有4個選項的其中一個,圖片上有個對勾標記,
然后如果正確選項是帥哥,則選擇A后,圖片替換成
所以首先在xml屬性里,將自定義的圖片替換圓圈:關鍵就是
android:button="@drawable/c" 這一句:
<RadioButton android:id="@+id/radio2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="丑男" android:button="@drawable/c" />
然后在代碼中設置監聽事件,當選中了正確答案,
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { // TODO Auto-generated method stub result = (RadioButton)findViewById(checkedId); if (result.getText().toString().equals("帥哥")) { result.setButtonDrawable(R.drawable.b1);//選對后替換打鈎的圖片 resultDetail.setVisibility(View.VISIBLE); resultDetail.setText("恭喜你,答對了!"); }else { resultDetail.setVisibility(View.VISIBLE); resultDetail.setText("很遺憾,答錯了!"); } } });