android初步學習--Radiogroup使用

不廢話,先來佈局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imageview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
        >

    <RadioGroup
        android:id="@+id/radiogroup"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:checkedButton="@+id/rb1" >
        
        <RadioButton
            android:id="@+id/rb1"
            android:text="1\n號\n佳\n麗" />

        <RadioButton
            android:id="@+id/rb2"
            android:text="2\n號\n佳\n麗" />

        <RadioButton
            android:id="@+id/rb3"
            android:text="3\n號\n佳\n麗" />

        <RadioButton
            android:id="@+id/rb4"
            android:text="4\n號\n佳\n麗" />
    </RadioGroup>

</LinearLayout>
關於佈局文件我也不多說了,一看就能明白,下面我貼Activity代碼

public class ImgCheckActivity extends ActionBarActivity {
    private ImageView imageview;
    private RadioGroup radiogroup;
    private RadioButton rb1,rb2,rb3,rb4;
    
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.checkimg);
        imageview=(ImageView)super.findViewById(R.id.imageview);
        
        rb1=(RadioButton)super.findViewById(R.id.rb1);
        rb2=(RadioButton)super.findViewById(R.id.rb2);
        rb3=(RadioButton)super.findViewById(R.id.rb3);
        rb4=(RadioButton)super.findViewById(R.id.rb4);
        
       /*
        * 以匿名內部類的方法添加監聽器
        * setImageResource()爲添加圖片資源
        * checkedId爲匹配單選按鈕做選擇
        *
       */

        radiogroup=(RadioGroup)super.findViewById(R.id.radiogroup);
	radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
			
	    public void onCheckedChanged(RadioGroup group, int checkedId) {
	        if(checkedId==rb1.getId()){
	            imageview.setImageResource(R.drawable.ww);
	        }
	        else if(checkedId==rb2.getId()){
	            imageview.setImageResource(R.drawable.wa);
	        }
	        else if(checkedId==rb3.getId()){
	            imageview.setImageResource(R.drawable.ws);
	        }
	        else if(checkedId==rb4.getId()){
	            imageview.setImageResource(R.drawable.wd);
	        }
	    }
	});
}
	
}
代碼很簡單,大家勿噴。

發佈了39 篇原創文章 · 獲贊 5 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章