我正在进行Andorid测验,我想在点击按钮时突出显示按钮,但是当用户放开按钮时,它会将其变为原始颜色。你看我已经设置了按钮的背景,所以按钮可以是圆形的。我把它设置为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" />
roundedbutton.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>
答案 0 :(得分: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;
}
});
您也可以使用选择器。边框和圆角矩形。自定义相同。
可绘制文件夹中的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"
答案 1 :(得分:11)
使用这样的选择器并将按钮背景设置为drawable。
<?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>
答案 2 :(得分: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>
答案 3 :(得分:5)
如果您想以编程方式执行此操作,则还可以尝试以下两种方法之一: -
btn1.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0xFFAA0000));
或者这样
btn1.getBackground().setColorFilter(0xFFAA4400,PorterDuff.Mode.MULTIPLY);
只需将此代码放入onCreate活动方法中即可。您可以根据自己的选择更改颜色代码。
希望这有帮助。
答案 4 :(得分:1)
如果您不想使用选择器xml或2个形状创建2个drawable,或者甚至不想使用彩色滤镜以编程方式进行操作,则可以使用Android内置高亮显示通过使用 selectableItemBackground attibute:
<!-- 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"/>
答案 5 :(得分: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" />