2014-03-28 26 views
0

我正在嘗試一個程序,即根據按鈕和單選按鈕按顏色或文本區域應該改變。程序是確定它正在運行的按鈕als0.but當我的單選按鈕嘗試它顯示了這部分方法setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener)不適用

r1.setOnCheckedChangeListener(this); 

以下活動類附着

public class MainActivity extends Activity implements OnClickListener { 
    Button b1; 
    Button b2; 
    Button b3; 
    RadioButton r1; 
    RadioButton r2; 
    RadioButton r3; 
    private View mColorRegion; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     mColorRegion = findViewById(R.id.textView1); 
         b1 = (Button)findViewById(R.id.button1); 
         b2 = (Button)findViewById(R.id.button2); 
         b3 = (Button)findViewById(R.id.button3); 
         r1 =(RadioButton)findViewById(R.id.radio0); 
        r2 =(RadioButton)findViewById(R.id.radio1); 
        r3 =(RadioButton)findViewById(R.id.radio2); 
       b1.setOnClickListener(this); 
       b2.setOnClickListener(this); 
       b3.setOnClickListener(this); 
       r1.setOnCheckedChangeListener(this);} 
     @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 
    @Override 
    public void onClick(View arg0) { 
     // TODO Auto-generated method stub 
     if(arg0==b1) 
     { 
     mColorRegion.setBackgroundColor(Color.RED); 
     } 
     if(arg0==b2) 
     { 
     mColorRegion.setBackgroundColor(Color.GREEN); 

     } 
     if(arg0==b3) 
     { 
     mColorRegion.setBackgroundColor(Color.BLUE); 
     }} 
     public void onRadioButtonClicked(View arg1) 
       { 
       if(arg1==r1) 
     { 
     mColorRegion.setBackgroundColor(Color.RED); 
     } 
       if(arg1==r2) 
     { 
     mColorRegion.setBackgroundColor(Color.GREEN); 
     }if(arg1==r3) 
     { 
     mColorRegion.setBackgroundColor(Color.BLUE); 
     }}} 
+1

變化'實現OnClickListener'到'實現OnClickListener,OnCh eckedChangeListener'並確保你已經導入了'OnCheckedChangeListener' –

回答

1

您需要導入

錯誤而不是0
import android.widget.RadioGroup.OnCheckedChangeListener; 

import android.widget.CompoundButton.OnCheckedChangeListener; 

編輯:

我忘了說這是繁榮ķ建議

public class MainActivity extends Activity implements OnClickListener,OnCheckedChangeListener 

,並覆蓋

@Override 
public void onCheckedChanged(RadioGroup arg0, int arg1) { 

} 
相關問題