按钮state_pressed =“true”从未触发过,为什么?

时间:2013-12-10 17:06:02

标签: android android-layout android-ui

我在活动布局中定义了以下按钮:

<Button
    android:id="@+id/button_Collect"
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:layout_marginBottom="16dp"
    android:layout_gravity="center_horizontal"
    android:background="@drawable/selector_CollectButton" />

有了它,我将这个选择器应用于按钮:

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
      <item android:state_pressed="true">
        <!-- PRESSED STATE (Opacity change to 75) -->
        <!-- ==================================== -->
        <layer-list>
          <!-- Shape for the outer circle -->
          <item opacity="75">
            <shape android:shape="oval">
              <solid android:color="#FFBDC3C7" />
            </shape>
          </item>

          <!-- Shape for the circle -->
          <item android:top="18dp" android:left="18dp" android:right="18dp" android:bottom="18dp">
            <shape android:shape="oval">
              <!--<solid android:color="#FF3498DB" />-->
              <solid android:color="#FF22DD44" />
            </shape>
          </item>
        </layer-list>
      </item>

      <item>
        <!-- NORMAL STATE -->
        <!-- ============ -->
        <layer-list>
          <!-- Shape for the outer circle -->
          <item>
            <shape android:shape="oval">
              <solid android:color="#FFBDC3C7" />
            </shape>
          </item>

          <!-- Shape for the circle -->
          <item android:top="18dp" android:left="18dp" android:right="18dp" android:bottom="18dp">
            <shape android:shape="oval">
              <solid android:color="#FF3498DB" />
            </shape>
          </item>
        </layer-list>
      </item>
    </selector>

为什么每当我触摸应用中的按钮时,按下状态都不会触发?

我也尝试将我的“状态”拆分为单独的xml可绘制文件,但问题仍然存在。

至于活动,我的活动中有以下内容:

Button btnCollect = FindViewById<Button>(Resource.Id.button_Collect);
btnCollect.Touch += btnCollect_Touch;

这是C#代码,因为我正在使用Xamarin。这段代码执行没有问题,所以我知道触摸事件正在执行。

更新

我想出了一些东西,它仅在我删除上述事件处理程序时才有效:

我有以下代码:

private void btnCollect_Touch(object sender, View.TouchEventArgs e)
    {
        if (e.Event.Action == MotionEventActions.Up)
            ScoreManager.Instance.CalculatePoints();
    }

如果我没有为这个方法添加处理程序,那么选择器工作正常,任何人都知道为什么???

2 个答案:

答案 0 :(得分:1)

固定!!

我将事件从Touch更改为Click for the button,现在一切正常。

另外需要注意的是,很多人都说过(在其他帖子中)带有图层列表的选择器应该拆分成单独的文件。这使得该项目充斥着大量的xml文件。这不是必需的,您可以在同一个选择器文件中使用状态,并且它可以正常工作。

答案 1 :(得分:0)