2013-07-31 15 views
10

Wykonuję quiz Andorida i chcę podświetlić przycisk po kliknięciu, ale kiedy użytkownik puści przycisk, który zmieni kolor na oryginalny. Widzisz, ustawiłem tło przycisku, aby przyciski mogły być zaokrąglone. Ustawiłem to w drawowalnym.Jak wyróżnić przycisk po naciśnięciu?

<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

Proste rozwiązanie z użyciem filtra koloru podobną do tej z Vishwas i pełniejsze Opis rozwiązania onTouchListener, jak w Raghunandan: http://stackoverflow.com/a/14278790/891479 Tworzy OnTouchListener modyfikując filtr koloru na dotyk. –

Odpowiedz

13

Można użyć OnTouchListener lub użyć selektora.

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; 
} 
}); 

Możesz również użyć selektora. Granice i zaokrąglony prostokąt. Dostosuj to samo.

bkg.xml w rozciągliwej folderze

<?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 w rozciągliwej folderze

<?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 w rozciągliwej folderze

<?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> 

teraz ustawić tło fro swojej przycisku w xml

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

Dlaczego nie przycisk przełączania? czy jest to zasadniczo to samo, co –

+0

@AviParshan zależy od wymagań. Przełącznik jest zwykle używany do takich samych warunków włączania/wyłączania – Raghunandan

11

użyj selektora takiego jak ten i ustaw tło przycisków na losowanie.

<?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

Zmienić roundedbutton.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: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

Jeśli chcesz to zrobić programowo następnie można także spróbować jednego z następujących dwóch metod: -

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

lub tędy

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

po prostu wpisz ten kod w siebie r onTworzenie metody działania i to zrobi. Możesz zmienić kody kolorów według swojego wyboru.

Mam nadzieję, że to pomoże.

0

Jeśli nie chcesz, aby utworzyć 2 kanału alfa z xml wybieraka lub 2 kształty, a nawet nie chce niepokoić robi to programowo z filtrem kolorów, można użyć Android wbudowaną podświetleniem przez ussing selectableItemBackground attibute:

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

w xml. Na przykład:

<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

Source Code

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

selektor.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" /> 
Powiązane problemy