2013-12-19 119 views
0

我想单击时将按钮的图像更改为另一个图像。我想改变按钮的图像。当我点击图片

我知道setImageResource方法,但我不知道如何将它应用在这段代码中。

这是我的源代码:

import android.content.Context; 
import android.content.Intent; 
import android.content.res.TypedArray; 
import android.util.AttributeSet; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.widget.LinearLayout; 
import android.widget.TextView; 

public class Toolbar extends LinearLayout { 

public Toolbar(final Context context) { 
    super(context); 
} 

public Toolbar(final Context con, AttributeSet attrs) { 
    super(con,attrs);  
    setOrientation(HORIZONTAL); 
    setBackgroundColor(getResources(). 
      getColor(android.R.color.transparent)); 

    LayoutInflater inflater = (LayoutInflater) 
    con.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    inflater.inflate(R.layout.navigation, this); 

    TypedArray a = con.obtainStyledAttributes(attrs, 
      R.styleable.Toolbar); 
    String option = a.getString(R.styleable.Toolbar_tab_id); 

    String resourceId = "com.paxmodept.demo:id/"+option; 
    int optionId = getResources().getIdentifier(resourceId,null,null);    
    TextView currentOption = (TextView) findViewById(optionId); 
    currentOption.setBackgroundColor(getResources(). 
      getColor(android.R.color.black)); 
    currentOption.setTextColor(getResources(). 
      getColor(android.R.color.black)); 
    currentOption.requestFocus(optionId); 
    currentOption.setFocusable(false); 
    currentOption.setClickable(false); 

    TextView tab1 = (TextView) findViewById(R.id.tab1); 
    tab1.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 
      Intent intent = new Intent(con, Tab1.class); 
      con.startActivity(intent); 
     } 
    }); 

    TextView tab2 = (TextView) findViewById(R.id.tab2); 
    tab2.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 
      Intent intent = new Intent(con, Tab2.class); 
      con.startActivity(intent); 
     } 
    }); 

    TextView tab3 = (TextView) findViewById(R.id.tab3); 
    tab3.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 
      Intent intent = new Intent(con, Tab3.class); 
      con.startActivity(intent); 
     } 
    }); 

    TextView tab4 = (TextView) findViewById(R.id.tab4); 
    tab4.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 
      Intent intent = new Intent(con, Tab4.class); 
      con.startActivity(intent); 
     } 
    }); 
} 
} 

和XML代码:

<merge 
    xmlns:android="http://schemas.android.com/apk/res/android"> 

<TextView 
    android:id="@+id/tab1" 
    android:layout_width="66dp" 
    android:layout_height="66dp" 
    android:layout_weight="1" 
    android:clickable="true" 
    android:background="@drawable/backgound_states" 
    android:drawableTop="@drawable/eventoff" 
    android:focusable="true" 
    android:gravity="center" /> 

<TextView 
    android:id="@+id/tab2" 
    android:layout_width="66dp" 
    android:layout_height="66dp" 
    android:layout_weight="1" 
    android:clickable="true" 
    android:background="@drawable/backgound_states" 
    android:drawableTop="@drawable/galleryoff" 
    android:focusable="true" 
    android:gravity="center" /> 

<TextView 
    android:id="@+id/tab3" 
    android:layout_width="66dp" 
    android:layout_height="66dp" 
    android:layout_weight="1" 
    android:clickable="true" 
    android:background="@drawable/backgound_states" 
    android:drawableTop="@drawable/menuoff" 
    android:focusable="true" 
    android:gravity="center" /> 

<TextView 
    android:id="@+id/tab4" 
    android:layout_width="66dp" 
    android:layout_height="66dp" 
    android:layout_weight="1" 
    android:clickable="true" 
    android:background="@drawable/backgound_states" 
    android:drawableTop="@drawable/strockoff" 
    android:focusable="true" 
    android:gravity="center" /> 

    <!-- android:background="@drawable/contact" --> 

+0

其中是此布局中的按钮?我无法看到上面的任何按钮代码..plz突出显示它 – KOTIOS

回答

2

这里是一个绘制的一个例子,这将是由默认的白色,黑色,当按下:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_pressed="true" 
      android:drawable="@android:color/black" /> <!-- pressed --> 
    <item android:drawable="@android:color/white" /> <!-- default --> 
</selector> 

在Folder/res/drawable中将其定义为xml,并将其附加到您的按钮背景。每当你想改变图像您能也定义这个<item android:drawable="@android:color/white" />为绘制像 <item android:drawable="@drawable/my_button_pic" />

0
button.setBackground(context.getResources().getDrawable(R.drawable.menu_bg)); 
    there is another way to set image 
    button.setBackgroundResource(R.drawable.menu_bg); 
0

使用下面的代码。 btn_instance是你的按钮的实例。

btn_instance.setBackgroundResource(R.drawable.btn_image_ro); 
相关问题