【问题标题】:How to change Textview text in Activity from class that extends RelativeLayout如何从扩展 RelativeLayout 的类更改 Activity 中的 Textview 文本
【发布时间】:2021-04-24 07:34:45
【问题描述】:

在我的 MainActivity 中,我有一个 TextView。 现在,在扩展相对布局的其他类中,我实现了一个 MessageListener(使用 onNewMessage 方法)每秒接收一些值。我想将此值放在 MainActivity 中的 TextView 中。 这是我班级的一个简单示例:

public class VirtualJoystickView extends RelativeLayout implements
    MessageListener<nav_msgs.Odometry>{

 public VirtualJoystickView(Context context) {
    super(context);
    initVirtualJoystick(context);

    topicName = "/pepper/cmd_vel";
  }

  public VirtualJoystickView(Context context, AttributeSet attrs) {
    super(context, attrs);
    initVirtualJoystick(context);
    topicName = "/pepper/cmd_vel";
  }

  public VirtualJoystickView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    topicName = "/pepper/cmd_vel";
  }

  @Override
  public void onNewMessage(final nav_msgs.Odometry message) {
    //I need to put this value inside TextView that are in MainActivity 
    double w = message.getPose().getPose().getOrientation().getW();
    double x = message.getPose().getPose().getOrientation().getX();
    double y = message.getPose().getPose().getOrientation().getZ();
    double z = message.getPose().getPose().getOrientation().getY();
}

感谢您的帮助。

【问题讨论】:

    标签: java android textview ros


    【解决方案1】:

    如果你在 MainActivity 类中调用这个类,那么你可以向你的构造函数添加另一个值,这将是你的 Textview

    TextView textview;
    
    public VirtualJoystickView(Context context, TextView textview) {
    super(context);
    initVirtualJoystick(context);
    
    topicName = "/pepper/cmd_vel";
    
    this.textview = textview;
    }
    

    现在你可以使用改变它的文本

    textview.setText("some text");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-28
      • 1970-01-01
      • 1970-01-01
      • 2012-03-21
      • 1970-01-01
      相关资源
      最近更新 更多