Android 错误 - 使用单独的 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);
    }
}

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);
}
}

我希望这是你所期望的.检查这种类型的代码。

  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
                    }

               };

更改此设置

private Events1Example mainActivity;
public ColorSetter(int regionColor,Events1Example mainActivity) {

private MainActivity mainActivity;
public ColorSetter(int regionColor,MainActivity mainActivity) {

最新更新