2013-08-07 81 views
0

即時通訊創建一個高分但是我的問題是我不能在我的levelcomplete.class的按鈕中創建一個settext到我的highscore.class中。我的觀點是我想,如果我的高分(按鈕)是從我的levelcomplete.class點擊它將有一個textview settext自動在我的highscore.classsettext to highscore form other class

我的解釋流程levelcomplete.class = highscore(button)= highscore。類=的setText 10/10

只是想節省你的分數排行榜

levelcomplete.class

public class levelcomplete extends Activity { 
Button highscore; 
int highestScore; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_main); 
    // TODO Auto-generated method stub 
highscore = (Button) findViewById(R.id.save); 
highscore.setOnClickListener(new View.OnClickListener() { 

    @Override 
     public void onClick(View v) { 
     //Pass your score to other activity through Android's intent. 
     Intent intent = new Intent(getApplicationContext(), 
        highscore.class); 
     //THis highestScore variable will actually hold the score you get in this activity. 
     intent.putExtra("score", highestScore); 
     startActivity(intent); 
    } 
}); 
} 
} 

highscore.class

public class highscore extends Activity { 
TextView score; 
Button back; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.highscore); 
score = (TextView) findViewById(R.id.score); 
    int highestScore = -1; 
//Now use this score variable to set anywhere. 
    Bundle extras = getIntent().getExtras(); 

    if (extras != null) { 
     highestScore = extras.getInt("score"); 
     } 



    back = (Button) findViewById(R.id.btn_back); 
    back.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Toast.makeText(getApplicationContext(),"Back", 
        Toast.LENGTH_SHORT).show(); 

    } 
});  
    // TODO Auto-generated method stub 
} 
} 

回答

0

替換代碼:

Bundle extras = getIntent().getExtras(); 
if (extras != null) { 
    highestScore = extras.getInt("score"); 
} 

與此代碼才能獲得高分整數

getIntent().getIntExtra("score", 0); 

它可以幫助你與此有關。

1

有3方式做到這一點:

第一種方式:(不推薦):

public static int HighScore; 

您highscore.class創建這個靜態變量,你將在你的levelcomplete.class中設置這個公共變量,然後你可以使用這個變量。但是這種方法不是一個好方法。因爲如果你的設備需要運行大量的應用程序,這個靜態變量被垃圾收集刪除。而我們的DATAS將雲數據:)

方式二:(對於這個stiation這個最適合你)

使用捆綁傳遞活動之間的DATAS。這是你想要的最佳選擇。 發送方:

intent.putExtra("OUR_SCORE_TAG", INTEGER_SCORE); 

Bundle e = getIntent().getExtras(); 
if (e!= null) { 
    ourSCORE = extras.getInt("OUR_SCORE_TAG"); 
} 

第三條道路:(最佳方式,但不是requered您例如,這可以使用更復雜的類型):

在你levelcomplete.class,

public interface MyHighScoreClickListener 
{ 
    public void onMyScoreButtonClickListener(int p_score); 
} 

並在levelcomplete.class中創建該接口的一個實例,例如:

public MyHighScoreClickListener listener; 

highscore.setOnClickListener(new View.OnClickListener() { 

@Override 
    public void onClick(View v) { 
    //Pass your score to other activity through Android's intent. 
    Intent intent = new Intent(getApplicationContext(), 
       highscore.class); 
    //THis highestScore variable will actually hold the score you get in this activity. 
    intent.putExtra("score", highestScore); 
    startActivity(intent); 
    listener.onMyScoreButtonClickListener(highestScore); // This is deadline :) 
} 
}); 

你highscore.class必須ipmlement MyHighScoreClickListener,如果你不這樣做,這patternd不運行。

public class highscore extends Activity implements MyHighScoreClickListener ... 

然後覆蓋我們onMyScoreButtonClickListener方法在我們的highscore.class和編寫代碼,將設置的文本。這樣的工具:

public class highscore extends Activity { 
TextView score; 
Button back; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.highscore); 
score = (TextView) findViewById(R.id.score); 
int highestScore = -1; 

@Override 
public void MyHighScoreClickListener(int p_score) 
{ 
this.highestScore = p_score; 
score.setText(Integer.toString(this.hightScore)); 

} 

這是一個設計模式。你不必在這個例子中使用這種模式,但是,這將有所幫助。

+0

你能用你的代碼編輯我的代碼嗎?哈哈哈它給我錯誤 – nice

+0

這個邏輯編碼爲您的代碼。只需要瞭解邏輯。這看起來很難,但它非常簡單。在抓住要點之後,您將在複雜的應用程序中使用3.示例。 – Twinsens

+0

公共類highscore擴展活動實現MyHighScoreClickListener { 我在MyHighScoreClickListener的錯誤 – nice