Подтвердить что ты не робот

Android:: findViewByID - как я могу получить представление о TextView через прослушиватель другого элемента пользовательского интерфейса?

Это будет немного хромой вопрос. У меня есть следующий код:

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

 }
}

Любая помощь приветствуется.

4b9b3361

Ответ 1

Вы можете попробовать следующее:

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

использование зависит от вашего макета. Возможно, родитель вашей кнопки не является родителем вашего текста, поэтому будьте осторожны...

Ответ 2

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

Мне нужно установить видимый элемент из одного и того же родителя, поэтому я использовал этот код:) и он работал

Ответ 3

ИЗМЕНИТЬ Я не думаю, что это будет возможно. Вид V только имеет вид кнопок в нем....

Вы будете знать, если вы выберете тот же самый

  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( "изменить текст" );

     }
    }

Я правильно не понял ваш вопрос. Но если вы просто хотите получить текст из ur TextView, вы можете попробовать это как

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

 }
}