更改Imagebutton的背景图像

用户名

很抱歉,如果以前没有发布此消息,但我遇到几个imagebutton的问题,当我按下它们时,它们不会更改图像。我使用了与选项卡相同的逻辑,在该选项卡上调用了存储在可绘制文件夹中的x.xml(x是示例名称)文件,并从相应的imagebutton调用了该文件夹。

这是用于更改我使用的imagebutton的代码(drawable / dbmdtndeck.xml):

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


<!-- When selected -->
<item android:drawable="@drawable/ydeck2" 
      android:state_selected="true"
      android:state_focused="true" 
      android:state_pressed="true" />


<!-- When not selected -->
<item android:drawable="@drawable/ydeck"  />


</selector>

这是我使用的存储在布局文件夹中的文件(layout / frmdbm.xml):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/dbmbackground"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" >

<ScrollView
    android:id="@+id/dbmscrollView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/dbmllbackground1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

<LinearLayout
    android:id="@+id/dbmllbackground2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:orientation="horizontal" >

    <ImageButton
        android:id="@+id/imgbtnedit"
        android:layout_width="0dp"
        android:layout_height="150dp"
        android:layout_weight="1"
        android:background="@drawable/dbmbtnedit"
        android:scaleType="fitXY" />

    <LinearLayout
        android:id="@+id/dbmllblankspace2"
        android:layout_width="0dp"
        android:layout_height="150dp"
        android:layout_alignParentBottom="true"
        android:layout_weight="1"
        android:orientation="horizontal" >
    </LinearLayout>

    <ImageButton
        android:id="@+id/imgbtndeck"
        android:layout_width="0dp"
        android:layout_height="150dp"
        android:layout_weight="1"
        android:background="@drawable/dbmbtndeck" />

</LinearLayout>

<LinearLayout
    android:id="@+id/dbmllblankspacemid"
    android:layout_width="match_parent"
    android:layout_height="150dp"
    android:orientation="vertical" >
    <LinearLayout
        android:id="@+id/dbmllbackground6"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <LinearLayout
        android:id="@+id/dbmllblankspace4"
        android:layout_width="0dp"
        android:layout_height="150dp"
        android:layout_weight="1"
        android:orientation="horizontal" >
    </LinearLayout>
    <ImageButton
        android:id="@+id/imgbtnsearch"
        android:layout_width="0dp"
        android:layout_height="150dp"
        android:layout_weight="1"
        android:background="@drawable/dbmbtnsearch" />

    <LinearLayout
        android:id="@+id/dbmllblankspace5"
        android:layout_width="0dp"
        android:layout_height="150dp"
        android:layout_weight="1"
        android:orientation="horizontal" >
    </LinearLayout>
  </LinearLayout>
</LinearLayout>

<LinearLayout
    android:id="@+id/dbmllbackground3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <ImageButton
        android:id="@+id/imgbtnwish"
        android:layout_width="0dp"
        android:layout_height="150dp"
        android:layout_weight="1"
        android:background="@drawable/dbmbtnwish"
        android:scaleType="fitXY" />

    <LinearLayout
        android:id="@+id/dbmllblankspace"
        android:layout_width="0dp"
        android:layout_height="150dp"
        android:layout_alignParentBottom="true"
        android:layout_weight="1"
        android:orientation="horizontal" >
    </LinearLayout>

    <ImageButton
        android:id="@+id/imgbtnrestrict"
        android:layout_width="0dp"
        android:layout_height="150dp"
        android:layout_weight="1"
        android:background="@drawable/dbmbtnrestrict" />
    </LinearLayout>

        </LinearLayout>
    </ScrollView>

</RelativeLayout>

这是我的Java类代码(dbm.java):

package com.example.yao;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
/* FOR TOAST (POP MESSAGE),  BUTTON,  AND IMAGE BUTTON*/
import android.app.AlertDialog;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.Toast;
import android.widget.ImageButton;
/*   FOR TAB RESOUCE */
import android.app.TabActivity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Resources;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class dbm extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.frmdbm);
      /* Calling TOAST MESSAGE
       * 
       * 
       */ 
       addListenerOnButton();

    }

   /* TOAST MESSAGE (POP MESSAGE)
    * 
    */
    public void addListenerOnButton() {

    /*
     * ASSINING OBJECTS TO BUTTON VERIABLES
     */

    ImageButton imageButtonedit = (ImageButton) findViewById(R.id.imgbtnedit);
    ImageButton imageButtondeck = (ImageButton) findViewById(R.id.imgbtndeck);
    ImageButton imageButtonwish = (ImageButton) findViewById(R.id.imgbtnwish);
    ImageButton imageButtonsearch = (ImageButton) findViewById(R.id.imgbtnsearch);
    ImageButton imageButtonrestrict = (ImageButton) findViewById(R.id.imgbtnrestrict);

    /*
     * WHEN CLICK ON THE OBJECT POP MESSAGE APPEARS
     */

    imageButtonedit.setOnClickListener(new OnClickListener()
    {   
        @Override
        public void onClick(View arg0) 
        {
           Toast.makeText(dbm.this,
            "DECK EDITOR", Toast.LENGTH_SHORT).show();
        }   
    });

    imageButtondeck.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View arg0) 
        {
           Toast.makeText(dbm.this,
            "DECK RECEPICES", Toast.LENGTH_SHORT).show();
        }
    });

    imageButtonwish.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View arg0) 
        {
           Toast.makeText(dbm.this,
            "WISH LIST", Toast.LENGTH_SHORT).show();
        }
    });

    imageButtonsearch.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View arg0) 
        {
           Toast.makeText(dbm.this,
            "CARD SEARCHER", Toast.LENGTH_SHORT).show();        
        } 
    });

    imageButtonrestrict.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View arg0) 
        {
           Toast.makeText(dbm.this,
            "FORBIDDEN / LIMITED LIST", Toast.LENGTH_SHORT).show();   
        }
    });

    }
}

此外,我需要说的是,此布局(frmdbm.xml)被调用为一个框架布局,该框架布局我称为我的activity_main.xml中的tabcontent。当用户使用相应的选项卡时,该布局(frmdbm.xml)将出现在框架布局中。此外,我没有收到任何错误,但我注意到它不会更改图像,而是保留了原始图像。我是这个Android开发的新手,我不确定发生了什么事情,因为我在选项卡上执行了此操作,并且似乎运行良好。不知道我在这里做什么不同。

谢谢

瑞克(Rakeeb Rajbhandari)

用这个 :

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:drawable="@drawable/button_idle"
        android:state_focused="false"
        android:state_pressed="false"
        android:state_selected="false"/>
    <item android:drawable="@drawable/button_pressed"
        android:state_focused="false"
        android:state_pressed="true"
        android:state_selected="false"/>

</selector>

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章