【问题标题】:Cannot resolve constructor 'ImageButton()'无法解析构造函数“ImageButton()”
【发布时间】:2021-09-01 05:34:36
【问题描述】:

我的目标是创建一个图像按钮,并能够在每次单击按钮时更改背景图像。我在访问我在 activity_main.xml 文件中设计的 ImageButton 时遇到了问题。

这是 MainActivity.java 文件

package com.example.imageButtonTest;

import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageButton;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void foo(View v) {

        ImageButton myButton = new ImageButton(); //error here, might have to pass something inside Imagebutton()

        if(v.getId() == R.id.image_1) { //check if button is clicked
            myButton.setImageResource(R.drawable.imageName); //update to new image (imageName)
        }
    }
}

这是声明 ImageButton 的 activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <ImageButton
        android:id="@+id/image_1"
        android:tag="12"
        android:onClick="foo"
        android:background="@drawable/image_1"
        android:layout_width="50dp"
        android:layout_height="50dp">
    </ImageButton>

 </LinearLayout>

这是错误:

Cannot resolve constructor 'ImageButton()'

如何正确初始化 ImageButton,以便在每次单击按钮时更改其 android:background?

【问题讨论】:

    标签: java android xml android-studio


    【解决方案1】:

    ImageButton 没有 0 参数构造函数。没有视图。他们都至少需要一个上下文。 new ImageButton(this) 将编译。它仍然不会做你想做的事。根本没有理由在这里创建一个新视图。相反,您想要做的是

    public void foo(View v) {
        ImageButton myButton = (ImageButton)v;
        myButton.setImageResource(R.drawable.imageName)
    }
    

    这将改变现有视图的图像。创建一个新按钮并在其上设置图像不会有任何作用,因为您的新视图不在显示的视图根中。

    【讨论】:

      【解决方案2】:

      这是对 Gabe 对错误的详细回答的进一步说明。

      我的方法假设您想在两个图像之间更改 ImageButton 的背景,例如 image_oneimage_two

      您需要声明一个变量来保存按钮的状态并切换按钮 - 想想onoff 开关。

          public class MainActivity extends AppCompatActivity {
          //declare ImageButton here
          ImageButton imageButton;
      
          //variable for toggling state
          boolean isClicked = false;
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
      
              //initialize ImageButton here
              imageButton = findViewById(R.id.image_1);
          }
      
          public void foo(View v) {
      
              //if statement to check state of the button
              if (isClicked) {
      
                  imageButton.setImageResource(R.drawable.image_one);
      
                  //reverse button state
                  isClicked = false;
              } else {
                  imageButton.setImageResource(R.drawable.image_two);
      
                  //reverse button state
                  isClicked = true;
              }
      
          }
      
      }
      

      如果您要在按钮上设置多个图像,您可以使用Switch Statement 而不是If-Statement

      【讨论】:

      • 读懂我的想法!我想在两个图像之间切换
      • 是的,这是一个常见的问题。
      猜你喜欢
      • 2015-05-29
      • 1970-01-01
      • 2015-05-05
      • 2017-02-28
      • 2016-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多