我想单击时将按钮的图像更改为另一个图像。我想改变按钮的图像。当我点击图片
我知道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" -->
其中是此布局中的按钮?我无法看到上面的任何按钮代码..plz突出显示它 – KOTIOS