0

我是 android 的新手,在我的 android 应用程序中,有一个主Activity类,其中包含一个TextView用于显示来自其他类的各种状态消息。我想用其他类的状态值更新TextViewmain 。Activity主要活动类与其他类之间没有直接联系。安卓有可能吗?如果是,我不知道要这样做。请提供解决方案

代码片段

//main activity
public class MainMenu extends Activity {

    static String status = "Hello Friends";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    TextView tv = (TextView)findViewById(R.id.mytext);
    tv.setText(status);

        MyOtherClass myclass = new MyOtherClass();
        myclass.connect();
}

其他类不是活动类

 // Other class
 public class MyOtherClass {
  public MyOtherClass(){
  }

   public void connect(){
    String strIP = Mtx.confRead("IPAddress");
    String strPort = Mtx.confRead("Port");

            String msg = "Connecting...";
            // i want to show value of msg varible in Textview of main activity from here
   }

感谢您

4

6 回答 6

1

如果其他类是由你的活动启动的活动,那么使用类似这样的东西

主要活动

private void some_function() {
    startActivityForResult(intent_to_pass, SOME_REQUEST_CODE);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if((requestCode == SOME_REQUEST_CODE) && (resultCode == RESULT_OK)) {
        // extract status from data and use setText() to set the new status
    }
}

其他活动

// Prepare an intent, say result, with the status to be sent to main activity and use this to send back the new status
setResult(RESULT_OK, result);

如果其他类是独立的服务和/或活动,则在主活动中,使用

@Override
protected void onNewIntent(Intent intent) {
    // Extract new status from intent now and use it
}

在其他类中,只需使用包含新状态的意图启动主要活动。这确保了如果主要活动已经在运行,只需使用接收到的新意图中的数据

编辑(发布后看到你的更新):

如果另一个类既不是活动也不是服务,那么你可以这样做:

当您创建此类时,将父类的上下文(可以是服务或活动)传递给它,并使用此上下文创建与 startActivity() 一起使用的意图。或者,简单地使用广播监听器进行通信。但我不确定这是否是最好的方法

于 2012-06-19T05:34:01.560 回答
1

您可以通过在代码中进行这些更改来做到这一点

 public String connect(){
String strIP = Mtx.confRead("IPAddress");
String strPort = Mtx.confRead("Port");

        String msg = "Connecting...";
        return msg;

        // i want to show value of msg varible in Textview of main activity from here

}

在主班

String status=myclass.connect();
textview.setText(status);

尝试这个

于 2012-06-19T05:35:53.257 回答
1

在头等舱发送这样的状态

s=new Intent(this, nextClassName.class);
                    d=new Bundle();
                    d.putString("status", status);
                    s.putExtras(d);
                    startActivity(s);

然后在 newClassName 中,您可以通过此代码获取它

Intent t=getIntent();
    k=t.getExtras();
    status=k.getString("status");

您可以将 textview 的文本设置为状态

textview.setText(status);

尝试这个

于 2012-06-19T04:59:50.853 回答
1

也许这可以工作.......

   //main activity
public class MainMenu extends Activity {

static String status = "Hello Friends";
static TextView tv;
 /** Called when the activity is first created. */
 @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

tv = (TextView)findViewById(R.id.mytext);
tv.setText(status);

    MyOtherClass myclass = new MyOtherClass();
    myclass.connect();
 }

在其他班级:

   // Other class
     public class MyOtherClass {
   public MyOtherClass(){
     }

       public void connect(){
      String strIP = Mtx.confRead("IPAddress");
        String strPort = Mtx.confRead("Port");

        String msg = "Connecting...";
          MainMenu.tv.setText(msg);
           }
于 2012-06-19T05:46:05.437 回答
1

是的,您可能需要从其他类传递这些状态值,然后使用

         textView.setText(your_status);

值可以通过 putExtra() 和 getExtra() 的意图传递

于 2012-06-19T04:41:13.343 回答
1

在您的主要活动中创建一个状态实例字段

public static status = "initial status";

将其设置为 TextView

TextView tv = (TextView) findViewById(R.id.youtTextViewId);

tv.setText(status);

并在调用其他活动时使用其他活动中的值对其进行更新。

于 2012-06-19T04:41:16.590 回答