在 android 中为我的按钮添加点击效果
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24711893/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
adding a click effect to my button in android
提问by Farhan patel
i have made a custom button for my layout but the problem is it doesnt show me a onclick effect. here are my three xml files that ive build i have given button an oval shape can you also tell me how to make the button a circle and smaller in size?? thnx in advance
我为我的布局制作了一个自定义按钮,但问题是它没有显示点击效果。这是我构建的三个 xml 文件,我给了按钮一个椭圆形你能告诉我如何使按钮变成圆形并且尺寸更小吗?提前谢谢
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="0"
android:gravity="center"
android:textSize="25sp" />
<Spinner
android:id="@+id/spin"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:spinnerMode="dropdown"
android:text="heyyy"
android:layout_below="@+id/textView1"
/>
<Button
android:layout_marginTop="20dp"
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="150sp"
android:text="add"
android:gravity="center"
android:layout_centerHorizontal="true"
android:layout_below="@+id/spin"
android:background="@drawable/button_xml"
/>
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView3"
android:layout_below="@+id/button1"
android:gravity="left"
android:textSize="25sp"
android:text="add" />
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/button1"
android:gravity="right"
android:textSize="25sp"
android:text="a" />
<Button
android:layout_marginTop="20dp"
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="50sp"
android:text="reset"
android:gravity="center"
android:layout_centerHorizontal="true"
android:layout_below="@+id/textView3"/>
</RelativeLayout>
</ScrollView>
button_xml.xml
button_xml.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval"
>
<solid
android:color="#03A89E"/>
<stroke android:width="2dp"
android:color="#ffffff"/>
</shape>
button_click.xml
button_click.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/button_xml"
/>
</selector>
回答by Pragnesh Ghoda シ
here is the full XML code for button selector
这是按钮选择器的完整 XML 代码
button_normal.xml
button_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="@color/button_light_green"/>
<corners android:radius="5dp" />
</shape>
button_selected.xml
button_selected.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/button_light_green_selected"/>
<corners android:radius="5dp" />
</shape>
button_bg.xml
button_bg.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/button_selected"/>
<item android:state_focused="true" android:drawable="@drawable/button_selected"/>
<item android:drawable="@drawable/button_normal"/>
</selector>
And Finally Add it into Your button background...
最后将其添加到您的按钮背景中...
<Button
android:layout_marginTop="20dp"
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="150sp"
android:text="add"
android:gravity="center"
android:layout_centerHorizontal="true"
android:layout_below="@+id/spin"
android:background="@drawable/button_bg"/> <!-- Add Selector File here to have click effect -->
回答by Simas
Set the buttons background to the xml containing states:
将按钮背景设置为包含状态的 xml:
android:background="@drawable/button_click"
回答by ramaral
The android:background
for your button should be set to your selector (button_click.xml).
将android:background
您的按钮应该被设置为你的选择(button_click.xml)。
<Button
android:layout_marginTop="20dp"
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="150sp"
android:text="add"
android:gravity="center"
android:layout_centerHorizontal="true"
android:layout_below="@+id/spin"
android:background="@drawable/button_click"/>
回答by A_rmas
I think my selector would be useful for you. I'd made my own button selector in my projects. "item_pressed", "item_focused" and "item_normal" are the images used to display for buttons.
我认为我的选择器对你有用。我在我的项目中制作了自己的按钮选择器。“item_pressed”、“item_focused”和“item_normal”是用于显示按钮的图像。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:dither="true">
<item
android:state_pressed="true"
android:drawable="@drawable/item_pressed"/>
<item
android:state_focused="true"
android:drawable="@drawable/item_focused"/>
<item
android:drawable="@drawable/item_normal"/>
</selector>