2013-05-30 41 views
0

我想创建一个按钮,它在第一次按下时变红,然后在第二次变回正常灰色(并执行一些操作,如删除文件)。这是为了确认用户真的想要开始删除操作。更改可绘制时,Android按钮仍然处于按下状态

为此,我将背景可绘制更改为具有默认可绘制顶部的额外ColorDrawable的LayerDrawable。根据状态将ColorDrawable的alpha值设置为0或255。

在第一次点击时切换为红色,但第二次点击该按钮会变成黄色,如同按下状态,同时应该恢复为正常灰色。

演示代码:

package com.example.test; 

import android.os.Bundle; 
import android.app.Activity; 
import android.graphics.drawable.ColorDrawable; 
import android.graphics.drawable.Drawable; 
import android.graphics.drawable.LayerDrawable; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 

public class TestActivity extends Activity { 

    Button button; 
    boolean showRed; 
    ColorDrawable cd; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     button = new Button(this); 
     button.setText("Delete"); 

     // Following two lines don't matter, focus isn't the problem 
     button.setFocusable(false); 
     button.setFocusableInTouchMode(false); 

     cd = new ColorDrawable(0xffff0000); 
     cd.setAlpha(0); 

     button.setBackgroundDrawable(new LayerDrawable(new Drawable[] { 
       button.getBackground(), cd})); 

     setContentView(button); 


     button.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       showRed = !showRed; 

       if (showRed) 
        cd.setAlpha(255); 
       else 
        cd.setAlpha(0); 

       // Following line doesn't matter 
       button.setSelected(false); 

       button.getBackground().invalidateSelf(); 
      } 
     }); 
    } 
} 

回答

0

现在我发现自己的答案:每个绘制对象的引用是否需要重画被通知一个回调对象。如果drawable被分配给一个View,那么这个View就是回调,如果它是复合drawable的一部分(就像LayerDrawable),这个化合物就是回调。

的代码行

button.setBackgroundDrawable(new LayerDrawable(new Drawable[] { 
      button.getBackground(), cd})); 

现在做了按钮的默认绘制成LayerDrawable(所以默认背景的回调被设置为LayerDrawable)首位。然后用新创建的Drawable替换按钮的默认背景。发生这种情况时,先前的Drawable的回调被设置为null。

最后,默认背景没有回调,通知状态变化后必要的重绘,并保持(视觉上)处于错误的状态。

上面做正确的做法是:

Drawable oldBg = button.getBackground(); 
    button.setBackgroundDrawable(null); 
    button.setBackgroundDrawable(new LayerDrawable(
      new Drawable[] {oldBg, cd})); 

为先“断开”的默认背景,然后创建并设置新的一个。

0

我认为你正在努力实现可以通过使用切换按钮,而不是正常的按钮,并给它的背景很容易做的事情。

切换按钮可以处理您的第一次点击和第二次点击问题,因为它们是要切换。

我想说的是使用一个拨动开关,而不是正常的按钮,然后根据你的情况,你可以做两件事情:

1>分配切换按钮背景这将处理颜色的选择xml文件针对不同的状态(按下,聚焦,默认等)。对于州审查开发人员网站上的文档。

2.>你可以给背景一个默认的颜色,那么你可以做的是改变切换选中和未选中功能的颜色。

我建议切换按钮的唯一原因是它很容易处理第一次点击和第二次点击选项。

此外,只要将切换按钮背景更改为任何颜色或从默认值拖动,就不会再出现像开关一样的效果。您也可以删除和关闭写保持纹元和你为什么不使用选择为宗旨,将您选择的文件夹绘制属性textOff空白

<ToggleButton 
    android:id="@+id/toggleButton1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignRight="@+id/textView1" 
    android:layout_below="@+id/textView1" 
    android:layout_marginTop="42dp" 
    android:text="ToggleButton" 
    android:background="@android:color/white" 
    android:textOn="" 
    android:textOff="" /> 
+0

我不想让它看起来像一个切换按钮(因为它不打开和关闭某些东西),所以我需要让它看起来像一个普通的按钮,通过正常按钮的可绘制资源。由于这些资源在不同的Android版本(和不同的设备制造商)之间可能会有所不同,甚至连他们的名字似乎都没有标准化,所以这会变得复杂(可能我忽略了一些东西) –

+0

我编辑了答案检查是否可以帮助您 – Ravi

+0

和为按钮背景创建9个补丁图像 – Ravi

0

。 这是假设你的button_state.xml在可绘制文件夹中。

<?xml version="1.0" encoding="utf-8"?> 
    <selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_pressed="true" 
    android:drawable="@drawable/button_pressed" /> <!-- pressed --> 
    <item android:state_focused="true" 
    android:drawable="@drawable/button_focused" /> <!-- focused --> 
    <item android:drawable="@drawable/button_normal" /> <!-- default --> 
</selector> 

然后在你的按钮,你可以补充一点:

<Button 
     android:id="@+id/homeButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="10px" 
     android:background="@drawable/button_state" /> 
+0

如果我理解正确,只能使用自定义的可绘制按钮进行按下或聚焦状态。但我实际上需要一个新的状态(“action_requested”左右)。 –

相关问题