2013-07-31 1 views
10

나는 Andorid 퀴즈를 만들고 있는데, 클릭 할 때 버튼을 강조하고 싶지만 버튼을 놓으면 버튼이 원래 색으로 바뀝니다. 단추의 배경을 설정 했으므로 단추를 반올림 할 수 있습니다. 나는 드로어 블 (drawable)로 설정했습니다.버튼을 눌렀을 때 버튼을 강조 표시하는 방법은 무엇입니까?

<Button 
    android:id="@+id/btn1" 
    android:background="@drawable/roundedbutton" 
    android:textColor="#ffffff" 
    android:textStyle="italic" 
    android:layout_width="225sp" 
    android:layout_marginTop="23sp" 
    android:layout_height="38sp" 
    android:layout_alignLeft="@+id/btn2" 
    android:layout_below="@+id/textView1" 
    android:text="Button" /> 

roundedbutton.xml

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
android:shape="rectangle" android:padding="10dp"> 
<solid android:color="#848482"/> <!-- this one is ths color of the Rounded Button --> 

<corners 
android:bottomRightRadius="6.5dp" 
android:bottomLeftRadius="6.5dp" 
android:topLeftRadius="6.5dp" 
android:topRightRadius="6.5dp"/> 
</shape> 
+0

VISHWAS의 하나와 더 완전한와 마찬가지로 컬러 필터를 사용하여 간단한 솔루션 Raghunandan의 onTouchListener 솔루션 설명 : http://stackoverflow.com/a/14278790/891479 터치시 컬러 필터를 수정하는 OnTouchListener를 만듭니다. –

답변

13

당신은 OnTouchListener를 사용하거나 선택기를 사용할 수 있습니다.

button.setOnTouchListener(new OnTouchListener() { 

@Override 
public boolean onTouch(View v, MotionEvent event) { 
    if (event.getAction() == MotionEvent.ACTION_DOWN) { 
      // change color 
    } 
    else if (event.getAction() == MotionEvent.ACTION_UP) { 
      // set to normal color 
    } 

    return true; 
} 
}); 

선택기를 사용할 수도 있습니다. 테두리와 둥근 사각형. 동일하게 사용자 정의하십시오. 당김 폴더에

bkg.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/pressed" /> 
    <item android:state_focused="false" 
     android:drawable="@drawable/normal" /> 
</selector> 

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="#0AECBF"/>  
    <stroke android:width="3dp" 
     android:color="#0FECFF" /> 
    <padding android:left="5dp" 
     android:top="5dp" 
     android:right="5dp" 
     android:bottom="5dp"/> 
    <corners android:bottomRightRadius="7dp" 
     android:bottomLeftRadius="7dp" 
     android:topLeftRadius="7dp" 
     android:topRightRadius="7dp"/> 
    </shape> 

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="#ff33ffff" /> 
<padding android:left="5dp" 
      android:top="5dp" 
      android:right="5dp" 
      android:bottom="5dp"/> 
    <corners android:bottomRightRadius="7dp" 
      android:bottomLeftRadius="7dp" 
      android:topLeftRadius="7dp" 
      android:topRightRadius="7dp"/> 
</shape> 

는 이제 버튼을 이리저리 배경을 설정 in xml

 android:background="@drawable/bkg" 
+0

토글 버튼을 사용하지 않는 이유는 무엇입니까? 또는 이것은 본질적으로 동일합니다 –

+0

@AviParshan은 요구 사항에 따라 다릅니다. Toggle은 일반적으로 같은 on/off 조건에 사용됩니다. – Raghunandan

11

과 같은 선택기를 사용하여 단추 배경을 드로어 블로 설정하십시오.

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

수정 그런 다음 당신은 또한 두 가지 방법을 다음 중 하나를 시도 할 수 프로그래밍 방식으로 수행 할 경우

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

    <item android:state_pressed="true"><shape android:padding="10dp" android:shape="rectangle"> 
      <solid android:color="#ff0000" /> 
      <!-- this one is ths color of the Rounded Button --> 

      <corners android:bottomLeftRadius="6.5dp" android:bottomRightRadius="6.5dp" android:topLeftRadius="6.5dp" android:topRightRadius="6.5dp" /> 
     </shape></item> 
    <item><shape android:padding="10dp" android:shape="rectangle"> 
      <solid android:color="#848482" /> 
      <!-- this one is ths color of the Rounded Button --> 

      <corners android:bottomLeftRadius="6.5dp" android:bottomRightRadius="6.5dp" android:topLeftRadius="6.5dp" android:topRightRadius="6.5dp" /> 
     </shape></item> 

</selector> 
4

roundedbutton.xml : -

btn1.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0xFFAA0000)); 

또는이 방법

btn1.getBackground().setColorFilter(0xFFAA4400,PorterDuff.Mode.MULTIPLY); 

이 코드를 입력하십시오. 활동에 대한 방법을 만들고 그것을 할 것입니다. 원하는대로 색상 코드를 변경할 수 있습니다.

희망이 도움이됩니다.

0

선택기 xml 또는 2 개의 도형으로 드로어 블을 만들고 싶지 않거나 컬러 필터를 사용하여 프로그래밍 방식으로 프로그래밍하는 것을 원하지 않는 경우에는 다음을 사용하여 Android 내장 하이라이트를 사용할 수 있습니다. 당신의 XML에

<!-- Background drawable for bordered standalone items that need focus/pressed states. --> 
<attr name="selectableItemBackground" format="reference" /> 

: attibute를 selectableItemBackground. 예를 들어 :

<ImageButton 
    android:id="@+id/btn_help" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/ic_help" 
    android:background="?android:selectableItemBackground"/> 
0

소스 코드

https://drive.google.com/open?id=0BzBKpZ4nzNzUQ3RKZjE0eG15Rlk

선택.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/pressed" /> 
    <item android:state_focused="false" 
     android:drawable="@drawable/normal" /> 
</selector> 

normalpressed.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/colorPrimary"/> 
    <stroke android:width="3dp" 
     android:color="@color/colorPrimaryDark" /> 
    <padding android:left="5dp" 
     android:top="5dp" 
     android:right="5dp" 
     android:bottom="5dp"/> 
    <corners android:bottomRightRadius="7dp" 
     android:bottomLeftRadius="7dp" 
     android:topLeftRadius="7dp" 
     android:topRightRadius="7dp"/> 
</shape> 

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="#ff33ffff" />--> 
    <solid android:color="@color/colorHighLight" /> 
    <padding android:left="5dp" 
     android:top="5dp" 
     android:right="5dp" 
     android:bottom="5dp"/> 
    <corners android:bottomRightRadius="7dp" 
     android:bottomLeftRadius="7dp" 
     android:topLeftRadius="7dp" 
     android:topRightRadius="7dp"/> 
</shape> 


**button** 

     <Button 
     android:id="@+id/btn_sign_in" 
     android:layout_width="match_parent" 
     android:layout_height="48dp" 
     android:background="@drawable/selector" 
     android:drawableLeft="@android:drawable/btn_star_big_on" 
     android:drawablePadding="10dp" 
     android:layout_marginLeft="20dp" 
     android:layout_marginTop="90dp" 
     android:layout_marginRight="20dp" 
     android:gravity="center" 
     android:paddingLeft="10dp" 
     android:paddingRight="30dp" 
     android:text="Sign In" 
     android:textColor="#ffffff" 
     tools:layout_editor_absoluteY="0dp" 
     tools:layout_editor_absoluteX="8dp" /> 
관련 문제