当按钮被按下时如何突出显示?

13

我正在制作一个安卓问答游戏,当用户点击一个按钮时,我希望能够将其突出显示,但当用户松开按钮后,它会恢复原来的颜色。你知道吗,我已经在按钮的背景中设置了圆角,我是在drawable中设置的。

<Button
    android:id="@+id/btn1"
    android:background="@drawable/roundedbutton"
    android:textColor="#ffffff"
    android:textStyle="italic"
    android:layout_width="225sp"
    android:layout_marginTop="23sp"
    android:layout_height="38sp"
    android:layout_alignLeft="@+id/btn2"
    android:layout_below="@+id/textView1"
    android:text="Button" />

圆形按钮.xml

<?xml version="1.0" encoding="utf-8"?>
 <shape xmlns:android="http://schemas.android.com/apk/res/android"
 android:shape="rectangle" android:padding="10dp">   
<solid android:color="#848482"/> <!-- this one is ths color of the Rounded Button -->

<corners
android:bottomRightRadius="6.5dp"
android:bottomLeftRadius="6.5dp"
android:topLeftRadius="6.5dp"
android:topRightRadius="6.5dp"/>
</shape>

使用类似于Vishwas的颜色过滤器的简单解决方案,并使用Raghunandan的更完整的onTouchListener解决方案描述:https://dev59.com/Jm855IYBdhLWcg3wz33o#14278790 它创建一个OnTouchListener,在触摸时修改颜色过滤器。 - L. G.
6个回答

16
你可以使用 OnTouchListener,或者使用一个选择器。
button.setOnTouchListener(new OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
            // change color
    }
    else if (event.getAction() == MotionEvent.ACTION_UP) {
            // set to normal color
    }

    return true;
}
});

您可以使用选择器。边框和圆角矩形。定制它们。
drawable文件夹中的bkg.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/pressed" />
    <item  android:state_focused="false" 
        android:drawable="@drawable/normal" />
</selector>

在drawable文件夹中的normal.xml

<?xml version="1.0" encoding="UTF-8"?> 
  <shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle"> 
  <solid android:color="#0AECBF"/>    
  <stroke android:width="3dp"
        android:color="#0FECFF" /> 
  <padding android:left="5dp"
         android:top="5dp"
         android:right="5dp"
         android:bottom="5dp"/> 
  <corners android:bottomRightRadius="7dp"
         android:bottomLeftRadius="7dp" 
         android:topLeftRadius="7dp"
         android:topRightRadius="7dp"/> 
  </shape>  

在drawable文件夹中找到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="#ff33ffff" />
 <padding android:left="5dp"
             android:top="5dp"
             android:right="5dp"
             android:bottom="5dp"/> 
    <corners android:bottomRightRadius="7dp"
             android:bottomLeftRadius="7dp" 
             android:topLeftRadius="7dp"
             android:topRightRadius="7dp"/> 
</shape>

现在,在xml中设置按钮的背景。
     android:background="@drawable/bkg"

为什么不用切换按钮?或者这本质上是一样的东西。 - A P
@AviParshan 取决于需求。Toggle 通常用于开/关条件。 - Raghunandan

11
使用类似这样的选择器,并将按钮背景设置为可绘制资源。
<?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/blue" /> <!-- pressed -->
        <item android:state_focused="true"
            android:drawable="@drawable/blue" /> <!-- focused -->
        <item android:drawable="@drawable/red" /> <!-- default -->
</selector>

7

修改roundedbutton.xml文件

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true"><shape android:padding="10dp" android:shape="rectangle">
            <solid android:color="#ff0000" />
            <!-- this one is ths color of the Rounded Button -->

            <corners android:bottomLeftRadius="6.5dp" android:bottomRightRadius="6.5dp" android:topLeftRadius="6.5dp" android:topRightRadius="6.5dp" />
        </shape></item>
    <item><shape android:padding="10dp" android:shape="rectangle">
            <solid android:color="#848482" />
            <!-- this one is ths color of the Rounded Button -->

            <corners android:bottomLeftRadius="6.5dp" android:bottomRightRadius="6.5dp" android:topLeftRadius="6.5dp" android:topRightRadius="6.5dp" />
        </shape></item>

</selector>

6

如果你想以编程方式完成它,那么你可以尝试以下两种方法之一:

btn1.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0xFFAA0000));

或者这样做:
btn1.getBackground().setColorFilter(0xFFAA4400,PorterDuff.Mode.MULTIPLY);

将以下代码放在您的Activity的onCreate方法中即可。您可以根据自己的选择更改颜色代码。


3

如果你不想创建2个具有选择器xml的可绘制对象,或者2个形状,甚至不想通过使用颜色过滤器以编程方式麻烦自己,你可以使用Android内置的高亮显示功能,方法是使用selectableItemBackground属性:

<!-- Background drawable for bordered standalone items that need focus/pressed states. -->
<attr name="selectableItemBackground" format="reference" />

在您的 XML 中。例如:

<ImageButton
   android:id="@+id/btn_help"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:src="@drawable/ic_help"
   android:background="?android:selectableItemBackground"/>

0

源代码

https://drive.google.com/open?id=0BzBKpZ4nzNzUQ3RKZjE0eG15Rlk

selector.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/pressed" />
    <item  android:state_focused="false"
        android:drawable="@drawable/normal" />
</selector>

normalpressed.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/colorPrimary"/>
    <stroke android:width="3dp"
        android:color="@color/colorPrimaryDark" />
    <padding android:left="5dp"
        android:top="5dp"
        android:right="5dp"
        android:bottom="5dp"/>
    <corners android:bottomRightRadius="7dp"
        android:bottomLeftRadius="7dp"
        android:topLeftRadius="7dp"
        android:topRightRadius="7dp"/>
</shape>

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="#ff33ffff" />-->
    <solid android:color="@color/colorHighLight" />
    <padding android:left="5dp"
        android:top="5dp"
        android:right="5dp"
        android:bottom="5dp"/>
    <corners android:bottomRightRadius="7dp"
        android:bottomLeftRadius="7dp"
        android:topLeftRadius="7dp"
        android:topRightRadius="7dp"/>
</shape>


**button** 

       <Button
        android:id="@+id/btn_sign_in"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:background="@drawable/selector"
        android:drawableLeft="@android:drawable/btn_star_big_on"
        android:drawablePadding="10dp"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="90dp"
        android:layout_marginRight="20dp"
        android:gravity="center"
        android:paddingLeft="10dp"
        android:paddingRight="30dp"
        android:text="Sign In"
        android:textColor="#ffffff"
        tools:layout_editor_absoluteY="0dp"
        tools:layout_editor_absoluteX="8dp" />

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接