我想改变按钮的图像。当我点击图像

时间:2013-12-19 07:21:31

标签: android android-imageview

我想在点击时将按钮的图像更改为其他图像。

我知道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"  -->

3 个答案:

答案 0 :(得分:2)

以下是一个drawable的示例,默认情况下为白色,按下时为黑色:

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

每当您想要更改图像时,请使用以下代码。 btn_instance是你按钮的实例。

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