单击时更改按钮的颜色

时间:2016-04-28 05:25:01

标签: android

大家好我正在制作一个项目我想通过按下来改变按钮颜色,例如假设我按下开始按钮然后它变成红色,当我再次按下它会变成绿色。 类:

public class MainActivity extends AppCompatActivity implements View.OnClickListener
{
    Button buttonStart, buttonStop,buttonAddContact;
int button_status=1;
    @Override
    public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        buttonStart = (Button) findViewById(R.id.button);

        buttonAddContact = (Button) findViewById(R.id.button6);
        buttonStart.setOnClickListener(this);
        buttonStop.setOnClickListener(this);
        onClickButtonListener();
    }


    public void onClickButtonListener () {
        buttonAddContact = (Button) findViewById(R.id.button6);
        buttonAddContact.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent("com.example.abhishek.detector.Main22Activity");
                        startActivity(intent);
                    }
                }
        );
    }
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        if(button_status == 1)//play the service
        {
            button_status=0;
            Intent i=new Intent(MainActivity.this,MyService.class);
            startService(i);
        }
        else//stop the service
        {
            button_status=1;
            Intent i=new Intent(MainActivity.this,MyService.class);
            stopService(i);
        }
    }

}

6 个答案:

答案 0 :(得分:3)

制作这个全局变量........ 在您的活动中以及所有方法之外

boolean flag = true;

这是在你的按钮点击事件..........

当标志改变它的值时,按钮的颜色会发生变化。

public void onClick(View arg0) {
        // TODO Auto-generated method stub
        if (flag) {
                arg0.setBackgroundColor(Color.GREEN);
                flag = false;
            } else {
                flag = true;
                 arg0.setBackgroundColor(Color.RED);
            }

    }

并尝试这个课程我编辑你的班级颜色变化......

    public class MainActivity extends AppCompatActivity implements View.OnClickListener
    {
        Button buttonStart,buttonAddContact;
   boolean flag = true;
        @Override
        public void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            buttonStart = (Button) findViewById(R.id.button);

            buttonAddContact = (Button) findViewById(R.id.button6);
            buttonStart.setOnClickListener(this);
            onClickButtonListener();
        }


        public void onClickButtonListener () {
            buttonAddContact = (Button) findViewById(R.id.button6);
            buttonAddContact.setOnClickListener(
                    new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent("com.example.abhishek.detector.Main22Activity");
                            startActivity(intent);
                        }
                    }
            );
        }
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            if(flag)//play the service
            {
                flag=false;
                Intent i=new Intent(MainActivity.this,MyService.class);
                startService(i);
                arg0.setBackgroundColor(Color.GREEN);
            }
            else//stop the service
            {
                flag=true;
                Intent i=new Intent(MainActivity.this,MyService.class);
                stopService(i);
                arg0.setBackgroundColor(Color.RED);

            }
        }

    }

享受编码...............

答案 1 :(得分:1)

您可以使用选择器按钮作为背景:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/green"
        android:state_pressed="true" />
    <item android:color="@color/green"
        android:state_focused="true" />
    <item android:color="@color/red"/>
</selector>

答案 2 :(得分:0)

使用此代码更改颜色

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true">
        <shape android:shape="rectangle">
            <stroke android:width="1dp" android:color="@color/colorPrimaryDark"/>
            <corners android:radius="5dp" />
            <gradient android:angle="-90" android:endColor="@color/colorPrimaryDark" android:startColor="@color/colorPrimaryDark"/>
        </shape>
    </item>

    <item android:state_focused="true">
        <shape android:shape="rectangle">
            <stroke android:width="1dp" android:color="@color/colorPrimaryDark"/>
            <corners android:radius="5dp" />
            <gradient android:angle="-90" android:endColor="@color/colorPrimaryDark" android:startColor="@color/colorPrimaryDark"/>
        </shape>
    </item>

    <item>
        <shape android:shape="rectangle">
            <stroke android:width="1dp" android:color="@color/colorPrimary"/>
            <corners android:radius="5dp" />
            <gradient android:angle="-90" android:endColor="@color/colorPrimary" android:startColor="@color/colorPrimary"/>
        </shape>
    </item>
</selector>

答案 3 :(得分:0)

你可以这样做,

int count =0; //Declare this as class level variable so that you don't need to make it final

   myBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View arg0) {
        if(count==0){
            myBtn.setBackgroundColor(Color.RED);
            count++;
        }
        else{
            myBtn.setBackgroundColor(Color.GREEN);
            count = 0;
        }
    }
});

更新:

根据您的代码编辑,

int button_status=0; //Declare this as class level variable so that you don't need 

public void onClick(View arg0) {
        // TODO Auto-generated method stub
        if(button_status == 0)//play the service
        {
            button_status++;
            arg0.setBackgroundColor(Color.RED);
            Intent i=new Intent(MainActivity.this,MyService.class);
            startService(i);
        }
        else//stop the service
        {
            button_status=0;
            arg0.setBackgroundColor(Color.GREEN);
            Intent i=new Intent(MainActivity.this,MyService.class);
            stopService(i);
        }
    }

答案 4 :(得分:0)

将此文件插入可绘制名称button_color.xml 然后在按钮中添加此行     机器人:背景= “@绘制/ button_color”

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
     
<item android:state_pressed="true">
             
    <shape android:shape="rectangle">
                     
        <corners android:radius="8dip" />
                    
                     
        <gradient android:angle="-90" android:endColor="#39C6BD" android:startColor="#01F9F7" />
                   
                 
    </shape>
         
</item>
    
<item android:state_focused="true">
             
    <shape android:shape="rectangle">
                     
        <corners android:radius="8dip" />
                    
                     <solid android:color="#35D1C8" />      
                 
    </shape>
         
</item>
 
    
<item>      
    <shape android:shape="rectangle">
    <corners android:radius="8dip" />
    <gradient
        android:angle="-90"
        android:endColor="@color/white"
        android:startColor="@color/white" />
    </shape>
</item>

答案 5 :(得分:0)

在drawable文件夹

中尝试保存文件名,如btn_style.xml
android:background="@drawable/btn_style"

btn_style.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <shape android:shape="rectangle">
            <solid android:color="@color/gray_btn_bg_pressed_color" />
            <corners android:radius="6dp" />
        </shape>
    </item>
    <item>
        <shape android:shape="rectangle">
            <solid android:color="@color/gray_btn_bg_color" />
            <corners android:radius="60dp" />
        </shape>
    </item>
</selector>