2014-09-11 37 views
0

你好嗎?我是Android編程新手。到目前爲止,我已經使用4.1 API 16在我的手機上運行了一個應用程序。 我的問題是,當釋放點擊事件時,下面代碼中的按鈕圖像應該恢復到以前的狀態。Android Onclick

button.setOnClickListener(new OnClickListener() { 

    @Override 
    public void onClick(View v) { 
     x = x + 1; 
     edittext.setText("" + x); 
     button.setBackgroundResource(R.drawable.buttonpressed1); 
    } 
}); 
+0

能否請您從您繪製文件夾共享buttonpressed1.xml? – VikramV 2014-09-11 08:22:21

+0

會發生什麼?它不工作?文本是否設置爲edittext? – Opiatefuchs 2014-09-11 08:26:07

回答

0

有你一個完整的解決方案:

ImageButton guessButton; 

guessButton = (ImageButton) findViewById(R.id.Knorr_Game_Guess_Button); 
     guessButton.setOnTouchListener(new View.OnTouchListener() 
     { 
      @Override 
      public boolean onTouch(View v, MotionEvent event) 
      { 
       if (event.getAction() == MotionEvent.ACTION_DOWN) 
       { 
        v.setBackgroundResource(R.drawable.ic_picture1); 
       } 
       else if (event.getAction() == MotionEvent.ACTION_UP) 
       { 
        v.setBackgroundResource(R.drawable.ic_picture2); 
       } 
       return false; 
      } 
     }); 

它的工作原理:)

+0

它確實有效。謝謝。 – 2014-09-13 10:06:51

0

如果你想捕捉按鈕釋放,則需要使用OnTouchListener

下面是一個例子:

yourButton.setOnTouchListener(new OnTouchListener() { 
    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
     if(event.getAction() == MotionEvent.ACTION_DOWN) { 
      //What happens when preessed 
     } else if (event.getAction() == MotionEvent.ACTION_UP) { 
      //What happens when released   } 
    } 
}; 

希望這有助於!

+0

謝謝。這真的有幫助。 – 2014-09-13 10:08:04

2

您還可以使用可繪製觸摸狀態。在印刷機上的顯示顏色變化例如:

在按鈕XML

使用android:background="@drawable/touchstates"

和touchstates.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:shape="rectangle"> 
      <stroke android:color="#cc0099CC" android:width="1dip" /> 
      <solid android:color="#660099CC" /> 
     </shape> 
    </item> 
</selector> 
+0

謝謝。觸摸監聽器是必需的,它的工作。 – 2014-09-13 10:10:14

1

對於你必須像把所有的按鈕來查看 的背景屬性使用選擇正常表現這個文件到文件夾繪製 selector.xml:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:drawable="@drawable/bg_pressed" android:state_pressed="true" /> 
    <item android:drawable="@drawable/br_unpressed" /> 
</selector> 

bg_pressed.xml

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="oval"> 
    <stroke 
     android:width="@dimen/circle_padding" 
     android:color="@android:color/transparent" /> 
    <solid android:color="@android:color/white" /> 
</shape> 

bg_unpressed.xml

​​

,並在佈局設置按鈕背景android:background="@drawable/selector"

+0

謝謝。它與Touch偵聽器一起工作。 – 2014-09-13 10:10:53

0

您需要使用選擇器。選擇器需要是分配給按鈕背景的可繪製文件。一個選擇器的例子

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

    <selector xmlns:android="http://schemas.android.com/apk/res/android"> 

      <!-- focused --> 
     <item 
      android:state_focused="true" 
      android:drawable="@drawable/rounded_button_focused" 
      /> 
     <!--pressed --> 
     <item 
      android:state_pressed="true" 
      android:drawable="@drawable/rounded_button_pressed" 
      /> 


     <item 
      android:drawable="@drawable/rounded_button_unfocused"/> 


    </selector> 

選擇器將基本上分配正確的背景,取決於您的按鈕的狀態。如果按鈕被按下,則它將文件「rounded_button_pressed」分配給按鈕的背景。該rounded_button_pressed是例如另一個繪製文件:

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

<shape xmlns:android="http://schemas.android.com/apk/res/android"> 


    <gradient android:startColor="@color/WhiteSmoke" 
     android:endColor="@color/LightGrey" 
     android:angle="270"/> 
    <stroke 
     android:width="2dp" 
     android:color="@color/CornflowerBlue" /> 
    <corners 
     android:radius="4dp" 

     /> 
    <padding 
     android:left="10dp" 
     android:top="10dp" 
     android:right="10dp" 
     android:bottom="10dp" /> 
</shape> 

希望它可以解決您的問題

+0

謝謝。真的想要一些Java編碼。觸摸監聽器爲此工作。 – 2014-09-13 10:09:25