2012-11-08 4 views
8

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

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 
    <TextView 
     android:text="temporary text view" 
     android:layout_width="wrap_content" 
     android:layout_height="100dip" 
     android:background="@drawable/blackselector" 
     />  
    <Button 
     android:text="temporary button" 
     android:layout_width="wrap_content" 
     android:layout_height="100dip" 
     android:background="@drawable/blackselector" 
     /> 
</LinearLayout> 

selector.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_pressed="true"> 
     <color android:color="#FF0000" /> 
    </item> 
    <item> 
     <color android:color="#00FF00" /> 
    </item> 
</selector> 

उत्तर

21

अपने TextView को यह निर्धारित करें:

android:clickable="true" 
+1

दिलचस्प है कि मेरा उत्तर पहले पोस्ट किया गया था, लेकिन स्वीकार नहीं किया गया था। हम्म। –

+0

@ पॉलबर्क हां, मैं भी सोच रहा हूं। लेकिन मेरी रक्षा में, मैंने नहीं देखा कि मेरे द्वारा पोस्ट किए जाने पर एक और जवाब था। मुझे लगता है कि ab11 ने नोटिस नहीं किया था कि कौन सा जवाब पहले पोस्ट किया गया था। लेकिन मैंने तुम्हारा उत्थान किया था। – Ahmad

+2

कोई चिंता नहीं। जाहिर है आपकी गलती नहीं (मैं वास्तव में सवाल पर टिप्पणी करने का इरादा रखता हूं)। अपवित्र के लिए धन्यवाद! :-) –

9

जोड़े

android:clickable="true" 
android:focusable="true" 
android:focusableInTouchMode="true" 
4

सच सही है करने के लिए क्लिक करने योग्य स्थापना के बारे में ऊपर। इसके अतिरिक्त, यदि आप ऑनक्लिक लिस्टनर एंड्रॉइड सेट को स्वचालित रूप से आपके लिए सही पर क्लिक करने के लिए जोड़ते हैं।

// From the View class. 
public void setOnClickListener(OnClickListener l) { 
    if (!isClickable()) { 
     setClickable(true); 
    } 
    getListenerInfo().mOnClickListener = l; 
} 
संबंधित मुद्दे