2013-07-31 12 views
10

मैं एंड्रॉइड क्विज़ बना रहा हूं और जब मैं क्लिक करता हूं तो मैं बटन को हाइलाइट करना चाहता हूं लेकिन जब उपयोगकर्ता बटन को जाने देता है तो यह मूल रंग में बदल जाता है। आप देखते हैं कि मैंने बटन की पृष्ठभूमि सेट की है ताकि बटन गोल किए जा सकें। मैंने इसे खींचने में सेट कर दिया है।दबाए जाने पर बटन को हाइलाइट कैसे करें?

<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

एक सरल समाधान विश्वास से एक के लिए और अधिक पूर्ण के साथ इसी तरह एक रंग फिल्टर का उपयोग कर रघुनाथन में से एक के रूप में टच लिस्टनर समाधान विवरण: http://stackoverflow.com/a/14278790/891479 यह स्पर्श पर रंग फ़िल्टर को संशोधित करने पर एक ऑन टचलिस्टर बनाता है। –

उत्तर

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

आप एक चयनकर्ता का भी उपयोग कर सकते हैं। सीमाओं और गोलाकार आयताकार। इसे अनुकूलित करें। drawable फ़ोल्डर में

bkg.xml drawable फ़ोल्डर में

<?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 drawable फ़ोल्डर में

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

अब आप अपने बटन इधर-उधर पृष्ठभूमि सेट xml

012 में
+0

टॉगल बटन क्यों नहीं? या यह अनिवार्य रूप से वही बात है –

+0

@AviParshan आवश्यकता पर निर्भर करता है। टॉगल आमतौर पर चालू/बंद स्थितियों के लिए प्रयोग किया जाता है – 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

संशोधित 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

आप तो आप भी दो तरीकों निम्न में से एक कोशिश कर सकते हैं यह प्रोग्राम के रूप में करना चाहते हैं तो: -

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

या इस तरह

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

बस इस कोड को आप में डालें गतिविधि की विधि बनाएं और यह करेगा। आप अपनी पसंद के अनुसार रंग कोड बदल सकते हैं।

उम्मीद है कि इससे मदद मिलती है।

0

यदि आप एक चयनकर्ता एक्सएमएल, या 2 आकार के साथ 2 ड्रॉबल्स बनाना नहीं चाहते हैं या यहां तक ​​कि रंग फ़िल्टर के साथ प्रोग्रामेटिक रूप से ऐसा करना परेशान नहीं करना चाहते हैं, तो आप एंड्रॉइड अंतर्निर्मित हाइलाइट का उपयोग करके इसका उपयोग कर सकते हैं अपने 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 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" /> 
संबंधित मुद्दे