Android事件驅動機制--單選事件

單選事件

單選事件的監聽接口:RadioGroup.OnCheckedChangeListener
RadioButton與RadioGroup組合使用才能實現單選功能
* 案例:選擇字符集
通過對RadioButton控件的選擇監聽,實現對字符集的選擇
* 界面設計
這裏寫圖片描述

* 代碼實現

private void singleCheck() {
        final TextView charSet=(TextView)findViewById(R.id.charSet);
        RadioGroup radioGroup=(RadioGroup)findViewById(R.id.radioGroup1);
        //對單選組添加選擇監聽事件
        radioGroup.setOnCheckedChangeListener(new RadioGroup.
                OnCheckedChangeListener() {
            /** 選擇狀態改變監聽
             * RadioGroup:單選組
             * checkedId:被改變的控件的資源id
             */
        public void onCheckedChanged(RadioGroup group, int checkedId) {
                //通過通過findViewById找到所選擇的RadioButton
            RadioButton radioButton=(RadioButton)findViewById(checkedId);
                charSet.setText(radioButton.getText().toString());  
            }
        });

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