当前位置: 动力学知识库 > 问答 > 编程问答 >

android - I want to chang image of button. when I click image

问题描述:

I want to change the image of a button to another image when clicked.

I know about setImageResource method but I don't know how to apply it in this piece of code.

this is my source code:

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);

}

});

}

}

and the XML code:

<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" -->

网友答案:

Here is an example of a drawable that will be white by default, black when pressed:

<?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>

Define it in the Folder /res/drawable as xml and attach it to your button background. You are able to define this <item android:drawable="@android:color/white" /> also as drawable like <item android:drawable="@drawable/my_button_pic" />

网友答案:
  button.setBackground(context.getResources().getDrawable(R.drawable.menu_bg));
  there is another way to set image
  button.setBackgroundResource(R.drawable.menu_bg);
网友答案:

Use the following code whenever you want to change the Image. btn_instance is tha instance of your button.

btn_instance.setBackgroundResource(R.drawable.btn_image_ro);
分享给朋友:
您可能感兴趣的文章:
随机阅读: