我是Android编程的新手,我制作的程序可以在按下按钮或单选按钮时更改textview的颜色。甚至我正在为this.Inow编写一个单独的Listner类。现在在activity和listner类和活动中它显示一个错误。我的活动类和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);
}
}
Listner-plz解释了如何编写这个seprate listner
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);
}
}
答案 0 :(得分:1)
我希望这是你所期望的。检查这种类型的代码。
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);
}
}
Button.OnClickListener btnOnClickListener=new Button.OnClickListener()
{
@Override
public void onClick(View v) {
if(v==b1)
{
//b1 code goes here
}
else if(v==cb2)
{
//b2 code goes here
}
};
答案 1 :(得分:0)
更改此
private Events1Example mainActivity;
public ColorSetter(int regionColor,Events1Example mainActivity) {
到
private MainActivity mainActivity;
public ColorSetter(int regionColor,MainActivity mainActivity) {