繁体   English   中英

Android ImageButton - 如何在单击按钮时更改图像?

[英]Android ImageButton - How to change the image when button is clicked?

如果单击按钮我想打开wifi并更改ImageButton的图像,但我不知道如何更改它

我还尝试过如何通过每次点击更改按钮的图像? 但是不起作用:

boolean isPressed=false
button.setOnClickListener(buttonListener);

OnClickListener buttonListener= new OnClickListener() {
@Override
public void onClick(View v) {
    if(isPressed){
    button.setBackgroundResource(R.drawable.icon1);
    }else{
    button.setBackgroundResource(R.drawable.icon2);
    }
    isPressed=!isPressed;
}};

当我编写上面的代码时,android studio显示:无法解析符号setOnClickListener

我还创建了一个button_wifi_selector xml,它看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<item android:drawable="@drawable/wifi_on"
    android:state_pressed="true" />
<item android:drawable="@drawable/ic_launcher"
    android:state_focused="true" />
<item android:drawable="@drawable/wifi" />

</selector>

在我的活动中我有这个

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageButton_wifi"
    android:layout_below="@+id/toggleButton_wifi"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:onClick="turnOffWifiDemo"
    android:src="@drawable/button_wifi_selector" />

但它没有做,我想要的可以有人帮我吗? 谢谢

编辑:它适用于第一个代码。 我只需要从xml中的ImageButton中删除onClick但是:当我启动应用程序时,他正在第二次更改图片。 之后他每次都改变它

这段代码对我有用; 测试一下

   final ImageButton btnTest =(ImageButton) findViewById(R.id.btnexctract);
    btnTest.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            btnTest.setSelected(!btnextra.isPressed());

            if (btnTest.isPressed()) {
                btnextra.setImageResource(R.drawable.yourImage);
            }
            else {
                btnTest.setImageResource(R.drawable.yourImage2);
            }
        }
    });

你可以像这样使用togglebutton

<ToggleButton
  android:id="@+id/toggleButton1"
  android:layout_width="74dp"
  android:layout_height="26dp"
  android:background="@drawable/toggle"
  android:checked="false"
  android:paddingRight="10dp"
  android:text="ON"
  android:textColor="#ffffff"
  android:textSize="13sp"
  android:textStyle="bold" />

并在java文件中

final ToggleButton toggleButton=(ToggleButton)findViewById(R.id.toggleButton1);

toggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // TODO Auto-generated method stub

            if(isChecked){
                //
                toggleButton.setBackgroundResource(R.drawable.icon1);
            }else{
                //
                toggleButton.setBackgroundResource(R.drawable.icon2);
            }
        }
});

您可以定义布尔标志,只需更改项目的可见性。 说明:changeBack是一个按钮。 此代码用于更改图像您的onClick不知道单击了哪个ID?

private boolean bgcolor = false;

public void onClick(View view) {
    switch(view.getId())
    {
        case R.id.changeBack:

        bgcolor = !bgcolor;
        if (bgcolor) {
                imv.setVisibility(View.VISIBLE);
                imv2.setVisibility(View.INVISIBLE);
        } 
        else {
              imv2.setVisibility(View.VISIBLE);
            imv.setVisibility(View.INVISIBLE);
        }

        break;
    }
}

这就是你必须要做的

选择器xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item 
       android:state_selected="true" 
       android:drawable="@drawable/info_icon_solid_with_shadow" />
   <item 
       android:drawable="@drawable/info_icon_outline_with_shadow" />
</selector>

然后在java中:

//assign the image in code (or you can do this in your layout xml)
imageButton.setImageDrawable(getBaseContext().getResources().getDrawable(R.drawable....));

//set the click listener
imageButton.setOnClickListener(new OnClickListener() {

    public void onClick(View button) {
        //Set the button's appearance
        button.setSelected(!button.isSelected());

        if (button.isSelected()) {
            //Handle selected state change
        } 
        else {
            //Handle de-select state change
        }

    }

});

我修改了它,对于开/关它可能会帮助你

<FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
<ImageButton
            android:id="@+id/imageButton_wifi2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="clickEvent"
            android:visibility="invisible"
            android:src="@drawable/on" />
        <ImageButton
            android:id="@+id/imageButton_wifi1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="clickEvent"
            android:src="@drawable/off" />


    </FrameLayout>

就像你需要用view参数创建方法一样

{ 
 c=false;
          im1=(ImageButton)findViewById(R.id.imageButton_wifi1);
          im2=(ImageButton)findViewById(R.id.imageButton_wifi2);
}

public void  clickEvent(View v)
{
//Code to implement 
if(c) {
    im2.setVisibility(View.VISIBLE);
    im1.setVisibility(View.INVISIBLE);
    c=false;
}
else {

    im1.setVisibility(View.VISIBLE);
    im2.setVisibility(View.INVISIBLE);

    c=true;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM