更改按钮的 OnClickListener 时出现匿名 class 错误

anonymous class error when changing OnClickListener of button

我想在我的 android 应用程序中更改按钮的操作,当我编写这段代码时,出现了一个匿名 class 错误,我不知道为什么。那么你能帮我吗? 错误是:

View.OnClickListener is an anonymous class

代码为:

color_Button.setOnClickListener(new View.OnClickListener(){
   public void old_background(View v_1) {
     if (v_1.getId() == R.id.button2) {        
        RelativeLayout background = (RelativeLayout) findViewById(R.id.bg);
        ImageButton newButton = (ImageButton)findViewById(R.id.imageButton);
        Button color_Button = (Button) findViewById(R.id.button2);
        EditText newUsername = (EditText) findViewById(R.id.KidUname);
        background.setBackgroundResource(R.drawable.dora);
        newButton.setImageResource(R.drawable.flower_2);
        color_Button.setBackgroundResource(R.drawable.circle_button);
        color_Button.setText("Diego !!");
        color_Button.setTextColor(Color.parseColor("#FFFFFFFF"));
        newUsername.setHint("Click the flower");
     }
   }
});

您应该在 onClick() 中编写单击按钮时必须执行的所有代码。在您的代码中,您缺少 onClick(),因此出现错误。

根据 docs

onClick() is called when a view has been clicked.

color_Button.setOnClickListener(new View.OnClickListener(){
       public void onClick(View v_1) {
            if (v_1.getId() == R.id.button2) {
               RelativeLayout background = (RelativeLayout) findViewById(R.id.bg);
               ImageButton newButton = (ImageButton)findViewById(R.id.imageButton);
               Button color_Button = (Button) findViewById(R.id.button2);
               EditText newUsername = (EditText) findViewById(R.id.KidUname);
               background.setBackgroundResource(R.drawable.dora);
               newButton.setImageResource(R.drawable.flower_2);
               color_Button.setBackgroundResource(R.drawable.circle_button);
               color_Button.setText("Diego !!");
               color_Button.setTextColor(Color.parseColor("#FFFFFFFF"));
               newUsername.setHint("Click the flower");
            }
        }
});