RadioGroup動態添加RadioButton,並且獲得事件


由於有許多的RadioButton是動態的,不是固定的一些,所以需要在代碼中,動態的添加到RadioGroup中,下面是我的實現方法。

1、添加RadioButton到RadioGroup中

RadioGroup group;
for(int i=0; i<10; i++)
{
    RadioButton tempButton = new RadioButton(this);
    tempButton.setBackgroundResource(R.drawable.xxx);	// 設置RadioButton的背景圖片
    tempButton.setButtonDrawable(R.drawable.xxx);			// 設置按鈕的樣式
    tempButton.setPadding(80, 0, 0, 0);           		// 設置文字距離按鈕四周的距離 
    tempButton.setText("按鈕 " + i);
    group.addView(tempButton, LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
}

2、為RadioGroup添加事件處理,可以得到當前選擇的RadioButton

        group.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                // TODO Auto-generated method stub
                RadioButton tempButton = (RadioButton)findViewById(checkedId); // 通過RadioGroup的findViewById方法,找到ID為checkedID的RadioButton
                // 以下就可以對這個RadioButton進行處理了
            }
        });


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM