I have a button defined in my XML file. The button works exactly like you would expect...that is until I add the line at the bottom (android:background="drawable/leftarrow1"). Then the button is no longer clickable in开发者_JS百科 the activity, but the new background shows up like I want.
What gives?
<Button
android:id="@+id/switch_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:background="@drawable/leftarrow1" />
Here is the click code for reference
Button switchLeft = (Button) findViewById(R.id.switch_left);
switchLeft.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.w(this.getClass().getName(), "clicked left arrow");
}
});
I works totally normal. Every click gets recorded.
My guess is that you use a solid image file (.png, .jpeg) as your background. If you just use such an image it will never change when clicked or selected. For that there is the state-list drawable described here.
In that XML file you define the images to show upon a specific state like selected, clicked and so on.
Another tip. If you want to check out the functionality of buttons for example use this kind of code.
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class TestButton extends Activity {
Context ctx = null;
Button btn = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.buttonbackground);
ctx = getApplication();
btn = (Button) findViewById(R.id.switch_left);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(ctx, "Button clicked", 5000).show();
// Log.w(this.getClass().getName(), "clicked left arrow");
}
});
}
}
It will be easier for you to recognize the event as a toast message is a more visual feedback.
hey buddy for showing buttons with images there is a ImageButton available. try this in xml file.
精彩评论