Android::findViewByID - 如何通过另一个 UI 元素的侦听器获取 TextView 的视图?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3675238/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:18:36  来源:igfitidea点击:

Android::findViewByID - how can I get View of a TextView through a Listener of another UI element?

android

提问by George

This is going to be a bit lame question. I have the following code:

这将是一个有点蹩脚的问题。我有以下代码:

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

 }
}

Any help is appreciated.

任何帮助表示赞赏。

回答by WarrenFaith

You could try this:

你可以试试这个:

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

the usage depends on your layout. Its possible, that the parent of your button is not the parent of your textview so be careful...

用法取决于您的布局。有可能,你的按钮的父级不是你的 textview 的父级,所以要小心......

回答by David

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

I need to set visible an element from the same parent so I used this code :) and it worked

我需要设置来自同一个父元素的可见元素,所以我使用了这个代码 :) 并且它起作用了

回答by DeRagan

EDITI dont think it will be possible. The view V only has the buttons view in it....

编辑我不认为这是可能的。视图 V 中只有按钮视图....

You will know if you typecast the same

你会知道如果你打字相同

  class ButtonListener implements android.view.View.OnClickListener
    {

     public void onClick(View v) 
     {

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

//However u can set the test here for ue textview txt.setText("change text");

//但是你可以在这里设置测试 ue textview txt.setText("change text");

     }
    }


I did not understand your question properly. But if you just want to get the text out from ur TextView you can try like this

我没有正确理解你的问题。但是如果你只是想从你的 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());

 }
}