【问题标题】:Android Button color changing on onClick?onClick上的Android按钮颜色改变?
【发布时间】:2013-06-19 11:12:59
【问题描述】:

我遇到了问题。

我有两个Button 对象。

ButtonAButtonB

要求:-

当我按下ButtonA 时,按钮的颜色应该会改变并且应该保持不变,直到我点击ButtonB

点击ButtonB后,同样的事情应该工作,即ButtonA

if (v == btn)
{
    btn.setBackground(mActivity.getResources().getDrawable(R.drawable.button_color_chnager));
}

XML:

<item android:state_focused="true" android:state_pressed="true" 
            android:drawable="@drawable/ic_launcher" /> 

【问题讨论】:

  • 什么问题?代码在哪里?
  • 发布您尝试过的代码,以便我们帮助您判断该代码的问题

标签: android button


【解决方案1】:

buttoncolor.xml

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

现在使用如下:

b1 = (Button) findViewById(R.id.b1);
b1.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {
         b2.setBackgroundDrawable(getResources().getDrawable(R.drawable.whatever));
    }
});

b2 = (Button) findViewById(R.id.b2);
b2.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {
         b1.setBackgroundDrawable(getResources().getDrawable(R.drawable.whatever));
    }
});

【讨论】:

  • 如果我想改变背景颜色,而不是背景图像,我应该怎么做?
【解决方案2】:

试试这个:

final Button buttonA = (Button) findViewById(R.id.ButtonA);
final Button buttonB = (Button) findViewById(R.id.ButtonB);

buttonA.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
buttonB.setBackgroundColor(Color.CYAN);
}
});

buttonB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
buttonA.setBackgroundColor(Color.RED);
}
});

在 drawables 文件夹中创建 selector.xml 并在其中复制以下代码:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
  <item android:state_focused="true" android:state_pressed="true" 
    android:drawable="@drawable/focused_pressed" /> 
  <item android:state_focused="false" android:state_pressed="true" 
    android:drawable="@drawable/pressed" /> 
  <item android:drawable="@drawable/normal" /> 
</selector>

在你的布局中复制按钮A的代码

<Button
    android:id="@+id/buttonA"
    style="@drawable/test"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:text="Button A" />

【讨论】:

    【解决方案3】:

    使用下面的代码..

    Boolean isOnePressed = false, isSecondPlace = false;
    b1.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    isOnePressed = true;
                    b1.setBackgroundColor(Color.BLUE);
                    if (isSecondPlace) {
                        b2.setBackgroundColor(Color.WHITE);
                        isSecondPlace = false;
                    }
    
                }
            });
            b2.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    b2.setBackgroundColor(Color.BLUE);
                    isSecondPlace = true;
                    if (isOnePressed) {
                        b1.setBackgroundColor(Color.WHITE);
                        isOnePressed = false;
                    }
    
                }
            });
    

    希望对你有帮助...

    【讨论】:

      【解决方案4】:

      我没有在下面的代码中进行测试。但我认为这会对你有所帮助。

      private Drawable defaultDrawableA;
      private Drawable defaultDrawableB;
      private Button buttonA , buttonB;
      
      
      buttonA = (Button) findViewById(R.id.buttonA);
      buttonB= (Button) findViewById(R.id.buttonB);
          buttonA .setOnClickListener(this);
          buttonB.setOnClickListener(this);
      
      
      @Override
      public void onClick(View v) {
          if (v == buttonA)
      
      if(defaultDrawableA== null)
      {
          defaultDrawableA=buttonA.getDrawable();
          buttonA.setBackgroundColor(Color.BLUE);
      }
      else
      {
          buttonA.setBackgroundDrawable(defaultDrawableA);
          defaultDrawableA=null;
      }
          else if (v == buttonB)
      
      if(defaultDrawableB == null)
      {
          defaultDrawableB=buttonB.getDrawable();
          buttonB.setBackgroundColor(Color.RED);
      }
      else
      {
          buttonB.setBackgroundDrawable(defaultDrawableB);
          defaultDrawableB=null;
      }
      
          return;
      }
      

      【讨论】:

        【解决方案5】:

        试试这个...

        button_bg.xml

        <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_pressed="true"
            android:drawable="@color/button_pressed"/> <!-- pressed -->
        <item android:drawable="@color/button_default"/> <!-- default -->
        

        activity_main.xml

         <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".MainActivity">
        
        <Button
            android:id="@+id/button1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/button_bg"
            android:text="Click Me"
            android:textColor="#fff" />
        

        【讨论】:

          【解决方案6】:

          最简单的方法

          随心所欲地改变颜色

          button_design.xml

          <?xml version="1.0" encoding="utf-8"?>
          
          <item android:state_pressed="true">
              <shape android:shape="rectangle">
                  <solid android:color="@color/colorPurple"></solid>
                  <corners android:radius="100dp" />
              </shape>
          </item>
          
          <item>
              <shape android:shape="rectangle">
                  <gradient android:endColor="@color/colorPurple" android:startColor="@color/colorBlue" />
                  <corners android:radius="100dp" />
              </shape>
          </item>
          

          【讨论】:

            猜你喜欢
            • 2013-09-24
            • 1970-01-01
            • 2012-05-02
            • 1970-01-01
            • 2015-01-12
            • 1970-01-01
            • 1970-01-01
            • 2014-05-06
            • 1970-01-01
            相关资源
            最近更新 更多