0

私はAndroidの初心者です。AndroidアプリのメインActivityクラスには、TextView他のクラスからのさまざまなステータスメッセージを表示するためのが含まれています。他のクラスのステータス値TextViewでmainを更新したい。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

ファーストクラスでは、このようなステータスを送信します

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

uはtextviewのテキストをステータスに設定できます

textview.setText(status);

これを試して

于 2012-06-19T04:59:50.853 に答える
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 に答える
1

他のクラスがurアクティビティによって開始されるアクティビティである場合は、次のようなものを使用します

主な活動

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
}

他のクラスでは、新しいステータスを含むインテントでメインアクティビティを開始するだけです。これにより、メインアクティビティがすでに実行されている場合は、受信した新しいインテントのデータを使用するだけで済みます。

編集(投稿後に更新を見た):

他のクラスがアクティビティでもサービスでもない場合、uはこれを行うことができます。

このクラスを作成するときは、親クラスのコンテキスト(サービスまたはアクティビティのいずれか)をそのクラスに渡し、このコンテキストを使用して、startActivity()で使用されるインテントを作成します。または、BroadcastListenersを使用して通信するだけです。しかし、これがそれを行うための最良の方法であるかどうかはわかりません

于 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

これはうまくいくかもしれません.......

   //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 に答える