簡體   English   中英

無法查看是否單擊了按鈕

[英]Can't see if button is clicked

我正在Android上制作一個應用程序,我已經制作了幾個按鈕,按鈕是圖像,問題是,你知道當你點擊一個按鈕時它看起來有點像是被推了。 您無法確定按鈕何時是圖像。 如果您不知道我的意思是復制我的代碼然后只需將默認按鈕拖到圖形布局上並嘗試按下兩個按鈕。

 <Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/btnFindMe"
    android:background="@drawable/iw_p"
    android:text="@string/about_us" />

在drawable文件夾中創建一個名為button_effect.xml的新文件並添加以下內容

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item 
    android:state_enabled="false"
        android:drawable="@drawable/aud_disable" />
    <item 
        android:state_pressed="true" 
        android:state_enabled="true"
        android:drawable="@drawable/aud_center_gray" />
    <item 
        android:state_focused="true" 
        android:state_enabled="true"
        android:drawable="@drawable/aud_center_gray" />
    <item 
        android:state_enabled="true"
        android:drawable="@drawable/speaker_side" />
</selector>

這里確保為每個狀態添加圖像,例如按鈕被禁用,按下,聚焦等時的圖像是什么。

現在更改按鈕背景,如下所示

android:background="@drawable/button_effect"

把它們放在一起如下所示

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/btnFindMe"
android:background="@drawable/button_effect"
android:text="@string/about_us" />

發生這種情況的原因是因為您正在替換具有打開和關閉狀態的圖像按鈕的圖像。 您需要實現一個選擇器,然后將其應用於您的按鈕,以確定它是打開還是關閉。

參考: http//developer.android.com/guide/topics/ui/controls/button.html

我確定你想根據Button的不同狀態給出不同的顏色/ drawable,如果是這種情況,那么你必須為Button定義自定義選擇器(XML)文件

句法:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
    android:constantSize=["true" | "false"]
    android:dither=["true" | "false"]
    android:variablePadding=["true" | "false"] >
    <item
        android:drawable="@[package:]drawable/drawable_resource"
        android:state_pressed=["true" | "false"]
        android:state_focused=["true" | "false"]
        android:state_hovered=["true" | "false"]
        android:state_selected=["true" | "false"]
        android:state_checkable=["true" | "false"]
        android:state_checked=["true" | "false"]
        android:state_enabled=["true" | "false"]
        android:state_activated=["true" | "false"]
        android:state_window_focused=["true" | "false"] />
</selector>

您可以在以下位置閱讀更多相關信息: Drawable Resources

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM