To będzie trochę kiepskie pytanie. Mam następujący kod:Android :: findViewByID - jak mogę uzyskać widok TextView za pośrednictwem odbiornika innego elementu 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 ?
}
}
Każda pomoc jest doceniana.
Dokładnie. Zobacz parent = (View) v.getParent() - Tak napisałem - .getParent() zwraca obiekt ViewParent. Mimo to tego właśnie szukałem. Dzięki. – George
bardziej niezawodnym sposobem byłoby nadanie elementowi katalogu głównego identyfikatora i wykonanie 'findViewById()' w twoim 'onCreate()'. Dzięki temu masz pełny łatwy dostęp do swojego układu i wszystkich widoków w środku. Tak więc Rahul podał poprawną odpowiedź ... po prostu użyj elementu głównego zamiast widoku tekstowego. – WarrenFaith