我是新來的android編程,我正在做一個程序,當按鈕或單選按鈕被按下時,改變textview的顏色。甚至我正在爲此編寫一個單獨的Listner類。現在,在活動和列表器類和活動中,它都顯示錯誤。我的活動類和Listner類已在此處駐留Android錯誤 - 使用分離Listner類
主要活動錯誤顯示在所有按鈕中和ColorSetter(b1.setOnClickListener(new ColorSetter(Color.RED, this));
public class MainActivity extends Activity {
private View mColorRegion;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mColorRegion = findViewById(R.id.color_region);
Button b1 = (Button)findViewById(R.id.button1);
Button b2 = (Button)findViewById(R.id.button2);
Button b3 = (Button)findViewById(R.id.button3);
RadioButton r1 = (RadioButton)findViewById(R.id.radio_button1);
RadioButton r2 = (RadioButton)findViewById(R.id.radio_button2);
RadioButton r3 = (RadioButton)findViewById(R.id.radio_button3);
b1.setOnClickListener(new ColorSetter(Color.RED, this));
b2.setOnClickListener(new ColorSetter(Color.BLUE, this));
b3.setOnClickListener(new ColorSetter(Color.YELLOW, this));
r1.setOnClickListener(new ColorSetter(Color.RED, this));
r2.setOnClickListener(new ColorSetter(Color.BLUE, this));
r3.setOnClickListener(new ColorSetter(Color.YELLOW, this));
}
public void setRegionColor(int color) {
mColorRegion.setBackgroundColor(color);
}
}
口糧按鈕聽者-PLZ講解如何寫這個seprate聽者
public class ColorSetter implements OnClickListener {
private int regionColor;
private Events1Example mainActivity;
public ColorSetter(int regionColor,Events1Example mainActivity) {
this.regionColor = regionColor;
this.mainActivity = mainActivity;
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
mainActivity.setRegionColor(regionColor);
}
}
你[R什麼錯誤gettig? – rajshree
你能試着解釋你想達到的目標嗎? – Blackbelt
@ user3041524我發佈了示例代碼,如果它不可理解,請讓我知道 – VenushkaT