帮助Eclipse中的图像按钮

时间:2011-08-04 15:27:35

标签: android eclipse

嘿,我想知道你是怎么做的,所以当按下一个图像按钮时,它会改变颜色以显示它被按下了。

DragonFruitActivity.java

package com.Dragon_Fruit;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
public class DragonFruitActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ImageButton playbutton = (ImageButton) findViewById(R.id.playbutton); playbutton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                setBackgroundResource(R.drawable.playbuttonselected);
                // TODO Auto-generated method stub
                startActivity(new Intent(DragonFruitActivity.this, playbutton.class));
            }

            private void setBackgroundResource(int playbuttonselected) {
                // TODO Auto-generated method stub

            }


});
        ImageButton settingsbutton = (ImageButton) findViewById(R.id.settingsbutton); settingsbutton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                startActivity(new Intent(DragonFruitActivity.this, settingsbutton.class));
            }


});
    }
}

所以新活动应该是:

package com.Dragon_Fruit;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
public class DragonFruitActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ImageButton playbutton = (ImageButton) findViewById(R.id.playbutton); playbutton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                arg0.setBackgroundResource(R.drawable.playbuttonselected);
                // TODO Auto-generated method stub
                startActivity(new Intent(DragonFruitActivity.this, playbutton.class));
            }


});
        ImageButton settingsbutton = (ImageButton) findViewById(R.id.settingsbutton); settingsbutton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                startActivity(new Intent(DragonFruitActivity.this, settingsbutton.class));
            }


});
    }
}

4 个答案:

答案 0 :(得分:1)

请使用StateListDrawable

答案 1 :(得分:0)

您可以查看this。您可以下载该示例,并将android:drawable替换为您的图片。

答案 2 :(得分:0)

您正在寻找的是选择器http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList

使用它们更改图像按钮颜色。

答案 3 :(得分:0)

onClick(...)方法中,您可以setBackgroundResource(...)

ImageButton playbutton = (ImageButton) findViewById(R.id.playbutton); 
playbutton.setOnClickListener(new View.OnClickListener() { 
    @Override public void onClick(View arg0) {
        setBackgroundResource(...);
        startActivity(new Intent(DragonFruitActivity.this, playbutton.class));
    }
}

(您还需要定义drawable以用作背景颜色)。