2011-05-24 37 views
1

在我的佈局xml文件中,我創建了一個屏幕,其中有三個按鈕,背景爲 ,現在當用戶單擊它時。它沒有顯示任何被點擊的標誌 我怎樣才能達到這個目的?按鈕如何專注於點擊在Android

在此先感謝

+0

你設置onClickListener? – BadSkillz 2011-05-24 12:47:58

+1

我認爲你應該在發佈問題前谷歌。這已在很多不同的地方得到解答。 – Hades 2011-05-24 12:49:43

+0

你好哈迪斯我已經搜索了很多次,但我沒有得到任何滿意的答案。其實我的問題有些不同。我有一個帶有背景圖像的按鈕。所以當我點擊它時,它不會顯示任何焦點或任何顏色變化。希望你得到我的問題。 – sandy 2011-05-26 09:36:31

回答

1

這是答案。

以表示不同的按鈕狀態(突出重點,選擇等),你可以定義不同的圖像的每個狀態

保存在您的項目RES /繪製/文件夾,然後引用它作爲一個XML文件可繪製的ImageButton的源代碼(在android:src屬性中)。 Android將根據按鈕的狀態和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_pressed" /> <!-- pressed --> 
    <item android:state_focused="true" 
     android:drawable="@drawable/button_focused" /> <!-- focused --> 
    <item android:drawable="@drawable/button_normal" /> <!-- default --> 
</selector> 
3

不知道,但,我們需要通過XML設置懸停

<item 
     android:color="hex_color" 
     android:state_pressed=["true" | "false"] 
     android:state_focused=["true" | "false"] 
     android:state_selected=["true" | "false"] 
     android:state_checkable=["true" | "false"] 
     android:state_checked=["true" | "false"] 
     android:state_enabled=["true" | "false"] 
     android:state_window_focused=["true" | "false"] /> 
+0

嗨hasrhagile,我試過了,但它不工作。 – sandy 2011-05-26 09:37:29

0

大家好我設法找到自己的答案。 我做了什麼

final Button home = (Button) findViewById(R.id.btnmaphome); 
    home.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      home.setBackgroundResource(R.drawable.lodclick); 
      // rest of the code 
     } 
    }); 

在這裏我還有一個形象,我把它作爲背景,當按鈕按下得到另一個圖像設置爲按鈕背景和它看起來像按鈕得到聚焦。對所有人都是。

0

使這三個xml文件在可繪製的文件夾中。 custom_background_focus.xml

{

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

    <stroke 
     android:width="1dp" 
     android:color="#111111" /> 

    <gradient 
     android:angle="225" 
     android:endColor="#fcfcfc" 
     android:startColor="#fcfcfc" /> 

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

</shape> 

custom_background_normal.xml

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

     <stroke 
      android:width="1dp" 
      android:color="#333333" /> 

     <gradient 
      android:angle="225" 
      android:endColor="#999999" 
      android:startColor="#999999" /> 

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

    </shape> 

}

<?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/custom_background_focus" /> <!-- pressed --> 
    <item android:state_focused="true" 
     android:drawable="@drawable/custom_background_focus" /> <!-- focused --> 
    <item android:drawable="@drawable/custom_background_normal" /> <!-- default --> 
</selector>