【问题标题】:Android. Button doesn't look pressed安卓。按钮看起来没有按下
【发布时间】:2013-02-26 13:39:07
【问题描述】:

我刚刚开始进行 Android 开发。我有一个像这样创建的非常简单的按钮:

<Button
    android:id="@+id/howItWorksButton"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="58dp"
    android:text="@string/how_it_works_button_title" />

在代码中我为其设置了背景颜色:

howItWorksButton.setBackgroundColor(Color.RED);

这是 onClickListener:

howItWorksButton.setOnClickListener(new OnClickListener()
    {
        public void onClick(View view)
        {
            Intent intent = new Intent(MainMenuActivity.this, HowItWorksActivity.class);
            MainMenuActivity.this.startActivity(intent);
        }
    });

问题是当我点击它时,它看起来不像是刚刚被按下,它只是保持原样,没有任何变化。它的 onClick 方法工作正常,功能一切正常,但我希望它看起来有所改变,就像所有按钮在按下时所做的那样。

【问题讨论】:

  • 除非您在活动中为此按钮添加 OnClickListener(),否则它不会看起来像被按下
  • @Numair 我已经做到了
  • @TanjaV 你能举个例子吗
  • @Numair 你错了,你不需要实现任何东西就能看到按下的效果。问题一定出在其他地方。 Andrey 确保你没有在代码中做一些奇怪的事情

标签: android button


【解决方案1】:

如果您手动设置颜色,Android 不会像 iOS 那样自动显示按下状态。这是推荐的方法。

在你的 values 文件夹中放置一个 colors.xml 文件夹。它的内容应该是这样的。

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="white">#ffffffff</color>
    <color name="darkwhite">#ffeeeeee</color>
    <color name="transparent">#00ffffff</color>
    <color name="semitransparent">#88000000</color>
    <color name="orange">#ffff8800</color>
    <color name="light_orange">#ffe27d18</color>
    <color name="light_blue">#ff00a2e8</color>
    <color name="black">#ff000000</color>
    <color name="menuButtonColor">#ffea8e44</color>
</resources>

例如在你的drawable中放置一个选择器文件

my_button_selector.xml

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

像这样使用你的选择器

<Button
    android:id="@+id/howItWorksButton"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="58dp"
    android:text="how_it_works_button_title"
    android:background="@drawable/my_button_selector" />

【讨论】:

    【解决方案2】:

    试试这个,

    使用Selector

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

    【讨论】:

      【解决方案3】:

      您需要将图像选择器设置为背景,以告诉 android 当按钮被按下、聚焦、禁用时,您希望显示哪种图像或颜色... 在这里你可以找到一个例子 http://www.mkyong.com/android/android-imagebutton-selector-example/

      【讨论】:

      • 只设置一种颜色是不对的。它将用于每个状态(默认、按下、聚焦等)。所以实际上没有什么会改变你对按钮所做的事情
      【解决方案4】:

      pressed 和其他state 中写入android:state_pressed=" "

      按下按钮时

      <item android:state_pressed="true">
          <shape>
              <solid android:color="@color/green" />
              <corners android:radius="@dimen/btn_corner_rds" />
          </shape>
      </item>
      

      未按下按钮时

      <item android:state_pressed="false">
          <shape>
              <solid android:color="@android:color/transparent" />
              <stroke android:width="@dimen/stroke_width_btn"  />
              <corners android:radius="@dimen/btn_corner_rds" />
          </shape>
      </item>
      

      【讨论】:

        猜你喜欢
        • 2019-06-01
        • 2010-09-05
        • 1970-01-01
        • 1970-01-01
        • 2020-10-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-29
        相关资源
        最近更新 更多