2011-03-16 2 views
14

ImageButton을 사용 중입니다. 그러나 나는 클릭 할 때 강조 표시를 얻지 않는다. 내가 봤 거든 많은 다른 이미지가 표시됩니다 선택기를 사용하도록 제안. 이 문제를 해결할 방법이 있습니까? 하나의 이미지 만 사용하고 강조 표시하거나 광선 효과를 주면됩니다. 사용자는 버튼이 클릭되었음을 알 수있다.Android에서 클릭 할 때 이미지 버튼 강조 표시

+0

당신이 이미지 버튼을 만들 때 사용하는 코드를 게재 할 수 있습니까? – Cristian

답변

29

실제로 이것은 그리 어렵지 않습니다. 2 개의 별도의 .png 파일이나 그와 비슷한 파일을 만들지 않아도됩니다. 당신은 그라데이션이있는 버튼이 다음을 변경하려는 경우 예를 들어, 버튼을 누를 때 :

1 단계 :

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"> 
    <corners android:radius="3dp" /> 
    <gradient android:endColor="#8ba0bb" android:startColor="#43708f" android:angle="90" /> 
    <stroke android:width="1dp" android:color="#33364252" /> 
</shape> 
: 이 기본 버튼 구배 (드로어 블/사진 default_button.xml) 만들기

2 단계 : 디폴트 버튼을 누르면 생성 구배 (그리기/default_button_pressed.xml)

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"> 
    <corners android:radius="3dp" /> 
    <gradient android:endColor="#43708f" android:startColor="#8ba0bb" android:angle="90" /> 
    <stroke android:width="1dp" android:color="#33364252" /> 
</shape> 

3 단계 : 선택기 (그리기/default_button_selector.xml) 만들기 :

,363,210
<selector xmlns:android="http://schemas.android.com/apk/res/android" > 
    <item android:state_pressed="true" android:drawable="@drawable/default_button_pressed" /> 
    <item android:drawable="@drawable/default_button" /> 
</selector> 

4 단계 (선택 사항) : 단추의 스타일 만들기 (값/style.xml) :

<resources> 
    <style name="DefaultButton"> 
     <item name="android:layout_width">wrap_content</item> 
     <item name="android:layout_height">wrap_content</item> 
     <item name="android:background">@drawable/default_button_selector</item> 
    </style> 
</resources> 

5 단계 : 버튼 (레이아웃/main.xml에)를 사용 :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" android:layout_height="fill_parent"> 

    <button style="@style/DefaultButton" /> 

</RelativeLayout> 

특히 알 수 있듯이 그렇게하는 것은 그리 어렵지 않습니다.

+19

lol @Zack'알 수 있듯이 특히 어렵지 않습니다. ' –

+2

이 스타일을 사용하여 여러 버튼을 쉽게 추가 할 수 있기 때문에 좋습니다. –

+4

배경 이미지가 다른 버튼이 필요한 경우 어떻게해야합니까? 이 고용체에 대해서는 – Kishore

2

각 버튼마다 여러 드로어 블을 설정할 필요가 없도록 터치 리스너에서 이미지 버튼의 색상 필터 속성을 설정합니다. 다른 게시물에 여기

참조 코드 : 여러 이미지 (누르면, 일반 등)을 만들 필요조차 선택기를 만들 필요가 없습니다없이

https://stackoverflow.com/a/14278790/891479

+0

+1. –

18

. setOnClickListener 대신 setOnTouchListener를 사용하십시오. 아래 코드는 클릭 된 항목에 회색 오버레이를 제공합니다.

((ImageButton)findViewById(R.id.myImageBtn)).setOnTouchListener(new OnTouchListener() { 

     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      switch (event.getAction()) { 
      case MotionEvent.ACTION_DOWN: { 
       ImageButton view = (ImageButton) v; 
       view.getBackground().setColorFilter(0x77000000, PorterDuff.Mode.SRC_ATOP); 
       v.invalidate(); 
       break; 
      } 
      case MotionEvent.ACTION_UP: 

       // Your action here on button click 

      case MotionEvent.ACTION_CANCEL: { 
       ImageButton view = (ImageButton) v; 
       view.getBackground().clearColorFilter(); 
       view.invalidate(); 
       break; 
      } 
      } 
      return true; 
     } 
    }); 
+1

이것은 나를 위해 일했습니다. ACTION_UP에서도 색상 필터를 지우십시오. –

+0

내 ADT는 'PorterDuff'이 무엇인지 모르기 때문에 암시 코드 완성을위한 'android.graphics'(android.graphics.PorterDuff.Mode.SRC_ATOP) 접미사 – 1owk3y

+0

도와 줘서 고마워! –

2

나는 이것을 할 수 있었다 !! 먼저 buttonStyle.xml을 드로어 블 폴더에 선언하십시오.

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

    <item 
     android:state_focused="false" 
     android:state_pressed="true" 
     android:drawable="@drawable/button_pressed" /> 

    <item android:drawable="@drawable/button_normal" /> 
</selector> 

는 그런 다음 당김 폴더에 button_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="@color/semiTransparentGnfrBlueColor" /> 
    <stroke android:width="10dp" android:color="@android:color/transparent" /> 
</shape> 

그 후, 당신은 당김 폴더에 button_normal.xml을 만들 수 있습니다.

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
     android:innerRadiusRatio="3" 
    android:shape="rectangle"> 
    <solid android:color="@color/gnfrBlueColor"/> 
    <stroke android:width="10dp" android:color="@android:color/transparent" /> 
</shape> 

당신은 기본 색상을 사용할 수 있지만 당신이 나처럼 그것을 수행하려는 경우, 당신은 색상라는 이름의 파일이 필요합니다.값 폴더에 XML 내부에이 값 :

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
<item name="gnfrBlueColor" type="color">#2A3748</item> 
    <item name="semiTransparentGnfrBlueColor" type="color">#602A3748</item> 
<integer-array name="androidcolors"> 
<item>@color/gnfrBlueColor</item> 
    <item>@color/semiTransparentGnfrBlueColor</item> 
    </integer-array> 
</resources> 

마지막으로 당신이 당신의 버튼이든로 설정 :

savedAccountLoginButton.SetBackgroundResource(Resource.Drawable.buttonStyle); 

내가 때를하기 때문에 색상 투명스트로크를 설정 것을 알 수 backgroundresource를 설정하면 단추가 커지고이 트릭으로 원래대로 유지됩니다.

이것은 프로그래밍 방식으로 달성 할 수있는 유일한 방법입니다.

당신은 XML이 작업을 수행하려면 다음

<Button 
      android:text="ENTRAR" 
      android:layout_width="match_parent" 
      android:layout_height="60dp" 
      android:background="@drawable/buttonstyle" 
      android:textColor="@color/white" 
      android:textSize="18sp" 
      android:id="@+id/button1" /> 
관련 문제