Android 在单击时突出显示图像按钮

发布于 2024-10-22 17:16:07 字数 125 浏览 3 评论 0原文

我正在使用 ImageButton。但点击时我没有突出显示。我用谷歌搜索,许多人建议使用显示另一张图像的选择器。有什么办法解决这个问题吗?仅使用一张图像并突出显示它或赋予它发光效果。以便用户知道该按钮已被单击。

I am using an ImageButton. But I don get the highlight when clicked. I googled and many suggested to use selector where another image is displayed. Is there any way around this. by using only one image and highlighting it or giving it a glow effect. so that the user knows that button has been clicked.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

删除→记忆 2024-10-29 17:16:07

这其实并不是很难做到的。您甚至不需要创建 2 个单独的 .png 文件或类似文件。例如,如果您想要一个具有渐变的按钮,然后在按下按钮时更改它:

第 1 步:
创建默认按钮渐变 (drawable/default_button.xml):

<shape xmlns:android="http://schemas.android.com/apk/res/android"
  android:shape="rectangle">
    <corners android:radius="3dp" />
    <gradient android:endColor="#8ba0bb" android:startColor="#43708f" android:angle="90" />
    <stroke android:width="1dp" android:color="#33364252" />
</shape>

步骤 2:创建默认按钮按下渐变 (drawable/default_button_pressed.xml):

<shape xmlns:android="http://schemas.android.com/apk/res/android"
  android:shape="rectangle">
    <corners android:radius="3dp" />
    <gradient android:endColor="#43708f" android:startColor="#8ba0bb" android:angle="90" />
    <stroke android:width="1dp" android:color="#33364252" />
</shape>

步骤 3:创建选择器 (drawable/default_button_selector.xml):

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="true" android:drawable="@drawable/default_button_pressed" /> 
    <item android:drawable="@drawable/default_button" /> 
</selector>

步骤 4 (可选)< /em>:为按钮创建样式 (values/style.xml):

<resources>
    <style name="DefaultButton">
        <item name="android:layout_width">wrap_content</item>   
        <item name="android:layout_height">wrap_content</item>
        <item name="android:background">@drawable/default_button_selector</item>
    </style>
</resources>

步骤 5:使用按钮 (layout/main.xml):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  android:layout_width="fill_parent" android:layout_height="fill_parent">

    <button style="@style/DefaultButton" />

</RelativeLayout>

如您所见,这并不是特别困难。

This is actually not very difficult to do. You don't even need to create 2 seperate .png files or anything like that. For instance, if you want to have a button which has a gradient, and then change it when the button is pressed:

Step 1:
Create default button gradient (drawable/default_button.xml):

<shape xmlns:android="http://schemas.android.com/apk/res/android"
  android:shape="rectangle">
    <corners android:radius="3dp" />
    <gradient android:endColor="#8ba0bb" android:startColor="#43708f" android:angle="90" />
    <stroke android:width="1dp" android:color="#33364252" />
</shape>

Step 2: Create default button pressed gradient (drawable/default_button_pressed.xml):

<shape xmlns:android="http://schemas.android.com/apk/res/android"
  android:shape="rectangle">
    <corners android:radius="3dp" />
    <gradient android:endColor="#43708f" android:startColor="#8ba0bb" android:angle="90" />
    <stroke android:width="1dp" android:color="#33364252" />
</shape>

Step 3: Create selector (drawable/default_button_selector.xml):

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="true" android:drawable="@drawable/default_button_pressed" /> 
    <item android:drawable="@drawable/default_button" /> 
</selector>

Step 4 (optional): Create style for the button (values/style.xml):

<resources>
    <style name="DefaultButton">
        <item name="android:layout_width">wrap_content</item>   
        <item name="android:layout_height">wrap_content</item>
        <item name="android:background">@drawable/default_button_selector</item>
    </style>
</resources>

Step 5: use the button (layout/main.xml):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  android:layout_width="fill_parent" android:layout_height="fill_parent">

    <button style="@style/DefaultButton" />

</RelativeLayout>

As you can see, it's not particularly difficult to do.

ぽ尐不点ル 2024-10-29 17:16:07

无需创建多个图像(按下、正常等),甚至无需创建选择器。使用 setOnTouchListener 而不是 setOnClickListener。下面的代码将为您提供单击项目上的灰色叠加层。

 ((ImageButton)findViewById(R.id.myImageBtn)).setOnTouchListener(new OnTouchListener() {

      @Override
        public boolean onTouch(View v, MotionEvent event) {
          switch (event.getAction()) {
          case MotionEvent.ACTION_DOWN: {
              ImageButton view = (ImageButton ) v;
              view.getBackground().setColorFilter(0x77000000, PorterDuff.Mode.SRC_ATOP);
              v.invalidate();
              break;
          }
          case MotionEvent.ACTION_UP:

              // Your action here on button click

          case MotionEvent.ACTION_CANCEL: {
              ImageButton view = (ImageButton) v;
              view.getBackground().clearColorFilter();
              view.invalidate();
              break;
          }
          }
          return true;
        }
    });

Without having to create multiple images (pressed, normal etc) and even don't have to create selector. Use setOnTouchListener rather than setOnClickListener. The below code will give you the grey overlay on the clicked item.

 ((ImageButton)findViewById(R.id.myImageBtn)).setOnTouchListener(new OnTouchListener() {

      @Override
        public boolean onTouch(View v, MotionEvent event) {
          switch (event.getAction()) {
          case MotionEvent.ACTION_DOWN: {
              ImageButton view = (ImageButton ) v;
              view.getBackground().setColorFilter(0x77000000, PorterDuff.Mode.SRC_ATOP);
              v.invalidate();
              break;
          }
          case MotionEvent.ACTION_UP:

              // Your action here on button click

          case MotionEvent.ACTION_CANCEL: {
              ImageButton view = (ImageButton) v;
              view.getBackground().clearColorFilter();
              view.invalidate();
              break;
          }
          }
          return true;
        }
    });
萌能量女王 2024-10-29 17:16:07

您只需在 View 中使用 android:foreground 即可实现可点击效果:

android:foreground="?android:attr/selectableItemBackground"

对于深色主题,还需添加主题到你的布局(使可点击效果清晰):

android:theme="@android:style/ThemeOverlay.Material.Dark"

You can simply use android:foreground for your View to achieve clickable effect:

android:foreground="?android:attr/selectableItemBackground"

For use with dark theme add also theme to your layout (to make clickable effect clear):

android:theme="@android:style/ThemeOverlay.Material.Dark"
世俗缘 2024-10-29 17:16:07

为了不必为每个按钮设置多个可绘制对象,我在触摸侦听器中设置了图像按钮的滤色器属性。

请参阅另一篇文章中的代码:

https://stackoverflow.com/a/14278790/891479

In order not to have to set several drawable for each button I set the color filter attribute of the image button in an on touch listener.

See code here in another post:

https://stackoverflow.com/a/14278790/891479

狼性发作 2024-10-29 17:16:07

我成功做到了这一点!
首先,在drawable文件夹中声明buttonStyle.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/button_pressed" />

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

  <item android:drawable="@drawable/button_normal" />
</selector>

然后在drawable文件夹中创建button_pressed.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
  <solid android:color="@color/semiTransparentGnfrBlueColor" />
  <stroke android:width="10dp" android:color="@android:color/transparent" />
</shape>

之后,您在drawable文件夹中创建button_normal.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:innerRadiusRatio="3"
  android:shape="rectangle">
  <solid android:color="@color/gnfrBlueColor"/>
  <stroke android:width="10dp" android:color="@android:color/transparent" />
</shape>

您可以使用默认颜色,但如果您想像我一样这样做,您需要在值文件夹中有一个名为 colors.xml 的文件,并在其中包含此值:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="gnfrBlueColor" type="color">#2A3748</item>
  <item name="semiTransparentGnfrBlueColor" type="color">#602A3748</item>
<integer-array name="androidcolors">
<item>@color/gnfrBlueColor</item>
    <item>@color/semiTransparentGnfrBlueColor</item>
  </integer-array>
</resources>

最后,您将其设置为按钮或其他内容:

savedAccountLoginButton.SetBackgroundResource(Resource.Drawable.buttonStyle);

注意我设置了颜色透明的描边,因为当您设置backgroundresource时,您的按钮会变得更大,并且通过这个技巧它仍然保持原始状态。

这是我能够以编程方式实现此目的的唯一方法。

如果您想通过 XML 来执行此操作:

<Button
            android:text="ENTRAR"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:background="@drawable/buttonstyle"
            android:textColor="@color/white"
            android:textSize="18sp"
            android:id="@+id/button1" />

I managed to do this!!
First you declare buttonStyle.xml in drawable folder.

    <?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/button_pressed" />

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

  <item android:drawable="@drawable/button_normal" />
</selector>

Then you create button_pressed.xml in drawable folder.

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
  <solid android:color="@color/semiTransparentGnfrBlueColor" />
  <stroke android:width="10dp" android:color="@android:color/transparent" />
</shape>

After that, you create button_normal.xml in drawable folder.

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:innerRadiusRatio="3"
  android:shape="rectangle">
  <solid android:color="@color/gnfrBlueColor"/>
  <stroke android:width="10dp" android:color="@android:color/transparent" />
</shape>

You can use default colors but if you want to do it like me, you need to have a file named colors.xml in values folder and this values inside:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="gnfrBlueColor" type="color">#2A3748</item>
  <item name="semiTransparentGnfrBlueColor" type="color">#602A3748</item>
<integer-array name="androidcolors">
<item>@color/gnfrBlueColor</item>
    <item>@color/semiTransparentGnfrBlueColor</item>
  </integer-array>
</resources>

Finally you set this to your button or whatever:

savedAccountLoginButton.SetBackgroundResource(Resource.Drawable.buttonStyle);

NOTICE THAT I set a stroke with color transparent because when you set backgroundresource your button becomes bigger and with this trick it remains original.

This is the only way I could archieve this programmatically.

If you want to do this by XML:

<Button
            android:text="ENTRAR"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:background="@drawable/buttonstyle"
            android:textColor="@color/white"
            android:textSize="18sp"
            android:id="@+id/button1" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文