【问题标题】:How to clear color of a button?如何清除按钮的颜色?
【发布时间】:2013-04-11 22:59:13
【问题描述】:

不仅如何将颜色清除为按钮的默认颜色,而且在我的代码中什么时候执行?我已经尝试了一切,但没有运气。当我单击一个按钮时,我设置了一些不透明度的绿色。现在,当我单击下一个按钮时,会发生同样的情况,但第一个按钮仍设置为绿色。我需要它恢复到原来的颜色。 我试过了:

button.getBackground().setColorFilter(null);

这是我的代码:

final OnClickListener clickListener = new OnClickListener() {

           private Button buttonClicked;

           public void onClick(View v) {
               Button button = (Button) v;
               button.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0x003333));

               if (buttonClicked == null) {
                   // first button is clicked
                   buttonClicked = button;
                   // only do stuff if buttons are in different layouts
               } else{
           if (!button.getParent ().equals(buttonClicked.getParent())) {
                // second button is clicked

            if(buttonClicked.getTag().equals(button.getTag()) ){

               // second button is clicked and same tag but different button

               Toast.makeText(Spojnice.this, "Correct", Toast.LENGTH_SHORT).show();
               button.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0x66FF33));
               buttonClicked.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0x66FF33));
               buttonClicked.setEnabled(false);
               button.setEnabled(false);
               buttonClicked = null;
               } else {
               //reset LightingColorFilter first
               Toast.makeText(Spojnice.this, "Wrong", Toast.LENGTH_SHORT).show();
               buttonClicked = null;

               }
              }else{

                  buttonClicked = button;
              }
           }
               }       
           };

【问题讨论】:

标签: java android


【解决方案1】:

我刚刚做了一个简单的程序,可以打开和关闭滤光片。

这是活动:

 Button buttonClicked = null;
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);
     }

     public void clickedButton(View v) {
         Button button = (Button)v;
         button.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF,
                                                                       0x66FF33));

         if (buttonClicked != null) {
             buttonClicked.getBackground().setColorFilter(null);
         }
         buttonClicked = button;

     }

这里是 XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
        >
    <TextView
            android:id="@+id/boss"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Hello World, MyActivity"
            />
    <Button
            android:id="@+id/buttsky"
            android:layout_below="@id/boss"
            android:onClick="clickedButton"
            android:layout_width="200dp"
            android:layout_height="100dp"
            android:text="pushMe"
            />
    <Button
            android:id="@+id/buttground"
            android:layout_below="@id/buttsky"
            android:onClick="clickedButton"
            android:layout_width="200dp"
            android:layout_height="100dp"
            android:text="no, pushMe"
            />
</RelativeLayout>

【讨论】:

  • 我收到错误“方法 getColorFilter 未定义为 Drawable 类型”。
  • 射击,我是从 ImageView 源中提取的,尽管这样可以。我会仔细看看。
  • 这里的主要问题是我不知道在哪里放置用于在第二次单击时清除第一个单击按钮的代码。
  • 你有没有想过只保留一个以null开头的活动变量(Button previousButton = null;)然后存储单击按钮的地址,然后当你再次点击该方法时,你检查previousButton,如果它不为空,你重置它的颜色过滤器?
  • 我确实尝试过,但这超出了我的知识范围。 :))) 你能帮我把这件事包起来吗?
猜你喜欢
  • 2020-03-27
  • 2015-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-15
  • 2018-04-12
  • 1970-01-01
相关资源
最近更新 更多