2010-09-09 8 views
16

यह थोड़ा लंगड़ा सवाल होने जा रहा है। मेरे पास निम्न कोड है:एंड्रॉइड :: findViewByID - मैं किसी अन्य UI तत्व के श्रोता के माध्यम से टेक्स्ट व्यू का दृश्य कैसे प्राप्त कर सकता हूं?

.............. 
public void onCreate (Bundle bundle) 
{ 
    super.onCreate(bundle); 
    this.setContentView(R.layout.main2); 
    Button bnt = (Button) this.findViewById(R.id.browser); 
    bnt.setOnClickListener(new ButtonListener()); 

} 
.............. 
class ButtonListener implements android.view.View.OnClickListener 
{ 

public void onClick(View v) 
{ 
    // I have a TextView in my xml layout file. 
    // I'd like to get it and change my text when I click this button. 
    // But I can't get it (the TextView) unless I make it as a value of a static member of this class and pass it to the constructor. 
    //I believe I am missing a big point here, so i'd be very thankful if you could explain how this is meant to be done ? 

} 
} 

किसी भी मदद की सराहना की जाती है।

उत्तर

24

की तरह कोशिश कर सकते हैं अगर आप सिर्फ उर TextView से पाठ बाहर निकलने के लिए चाहते हैं तो इस कोशिश कर सकते:

class ButtonListener implements android.view.View.OnClickListener { 
    public void onClick(View v) { 
     View parent = (View)v.getParent(); 
     if (parent != null) { 
      TextView txtView = parent.findViewById(R.id.mytextview); 
      txtView.setText(...); 
     } 
    } 
} 

उपयोग अपने लेआउट पर निर्भर करता है। इसकी भी संभव हो, अपने TextView की नहीं अपने बटन के माता पिता है कि माता-पिता तो सावधान रहना ...

+0

मेरा बिंदु बिल्कुल जांचें। देखें माता-पिता = (देखें) v.getParent() - मैंने इसे इस तरह लिखा है - .getParent() एक ViewParent ऑब्जेक्ट देता है। फिर भी, यह वही था जो मैं खोज रहा था। धन्यवाद। – George

+0

आपके रूट लेआउट तत्व को एक आईडी देने का और अधिक मजबूत तरीका होगा और आपके 'onCreate() 'में' findViewById()' करें। तो आपके पास अपने लेआउट और अंदर के सभी विचारों तक पूर्ण पहुंच है। तो राहुल ने सही जवाब दिया है ... टेक्स्टव्यू – WarrenFaith

0

EDIT मुझे नहीं लगता कि यह संभव होगा। दृश्य वी केवल बटन उस में देखने की है ....

यदि आप अपने टाइपकास्ट आपको पता चल जाएगा एक ही

class ButtonListener implements android.view.View.OnClickListener 
    { 

    public void onClick(View v) 
    { 

     Button vg=(Button)v; 
     Log.e("", "views are "+vg.getText()); 

// हालांकि यू ue TextView txt.setText के लिए यहाँ परीक्षण सेट कर सकते हैं ("पाठ बदलें");

 } 
    } 

मैं ठीक से अपने प्रश्न समझ में नहीं आया। लेकिन आप इस

TextView txt; 

public void onCreate (Bundle bundle) 
{ 
    super.onCreate(bundle); 
    this.setContentView(R.layout.main2); 
    Button bnt = (Button) this.findViewById(R.id.browser); 
    txt=(TextView)this.findViewById(R.id.urtextview); 
    bnt.setOnClickListener(new ButtonListener()); 

} 
.............. 
class ButtonListener implements android.view.View.OnClickListener 
{ 

public void onClick(View v) 
{ 
    // I have a TextView in my xml layout file. 
    // I'd like to get it and change my text when I click this button. 
    // But I can't get it (the TextView) unless I make it as a value of a static member of this class and pass it to the constructor. 
    //I believe I am missing a big point here, so i'd be very thankful if you could explain how this is meant to be done ? 
//I think this should get u the string... 
System.out.println("text is "+txt.getText().toString()); 

} 
} 
+0

देखें, मेरे पास एक बटन है, और जब मैं इसे क्लिक करता हूं, तो मैं टेक्स्टव्यू के टेक्स्ट को बदलना चाहता हूं। तो मेरा सवाल यह है कि "क्या ऐसा करने का कोई तरीका है जब केवल ऑनक्लिक विधि में कोडिंग हो?" मैंने कहा था कि यह एक लंगड़ा सवाल होगा ... कल्पना कीजिए कि मेरे पास केवल (देखें v) जानकारी है और मैं लेआउट में यूआई तत्व के साथ बातचीत करना चाहता हूं। क्या मुझे लेआउट में सभी यूआई तत्व मिल सकते हैं यदि मेरे पास केवल उनमें से एक का संदर्भ है? – George

+0

आपके मामले में मुझे नहीं लगता कि यह संभव होगा। यदि आपको लेआउट्स मिल रहे हैं तो शायद आप इसे अपने मामले में बच्चों से प्राप्त कर सकते हैं, यह सिर्फ एक बटन होगा। मेरी अद्यतन पोस्ट – DeRagan

6
class ButtonListener implements android.view.View.OnClickListener { 
    public void onClick(View v) { 
     View p = (View) view.getRootView();   
     if (p != null) { 
      TextView txtView = (TextView) p.findViewById(R.id.mytextview); 
      txtView.setText(...); 
     } 
    } 
} 

मैं एक ही माता पिता से दिखाई एक तत्व निर्धारित करने की आवश्यकता इसलिए मैं इस कोड का इस्तेमाल किया :) और यह

+0

के बजाय रूट तत्व का उपयोग करें। यह getParent() का उपयोग करते समय एक संवाद में एक दृश्य के लिए काम करता था। – Noumenon

संबंधित मुद्दे

 संबंधित मुद्दे