2012-02-25 10 views

答えて

5

スクリーンとは、アクティビティのことですか?それからあなたはおそらくあなたの意図の中で余分にそれらを渡したいと思うでしょう。

活動1:

int score; 

    ... 
    Intent Intent = new Intent(...); 
    intent.putExtra("score_key", score); 
    startActivity(intent); 

活動2のonCreate()

int score; 
    ... 

    Bundle extras = getIntent().getExtras(); 

    // Read the extras data if it's available. 
    if (extras != null) 
    { 
     score = extras.getInt("score_key"); 
    } 
+0

、置き換えることができます」 score_key "を任意の文字列で置き換えます。余分なものを識別するのが鍵です。同じデータを配置して設定するときは、2つのキーが一致していることを確認してください。 – triad

1

あなたはあなたの意思を持つバンドルに数値、文字列などを送信することができます。

Bundle b = new Bundle(); 
b.putInt("testScore", numCorrect); 
Intent i = new Intent(this, MyClass.class); 
i.putExtras(b); 
startActivity(intent) 

あなたも、あなたは、他のクラス/活動から、プロジェクト全体の中で

public class mainClass 
{ 
    private static int sharedVariable = 0; 


    public static int getSharedVariable() 
    { 
      return sharedVariable; 
    } 
} 

をあなたのデータを共有することができStringArraysおよびその他のいくつかの簡単なVARSこの方法の

0

つを置くことができますclassnameとを使用して直接アクセスできます。 (ドット)演算子。例えばmainClass.getSharedVariable();

0

Activitiysに変数を格納するための良い方法は、アプリケーションクラスの独自の実装を使用することです。

public class MyApp extends android.app.Application { 

private String myVariable; 

public String getMyVariable() { 
    return myVariable; 
} 

public void setMyVariable(String var) { 
    this.myVariable = var; 
} 

アプリケーションタグ内のManifest.xmlに新しいクラスを追加します。

<application android:name="MyApp" android:icon="@drawable/icon" android:label="@string/app_name"> 

次のように今、あなたは、すべてのアクティビティで変数を操作することができます。

MyApp ctx = (MyApp)getApplicationContext(); 
String var = ctx.getMyVariable(); 
関連する問題