2014-02-14 99 views
0

試圖爲android製作一個簡單的遊戲應用程序,無法設置選項。Java的android,如何在活動之間傳遞值。主菜單,選項菜單

有一個非常基本的設置現在。

做了一個全球一流的,因爲它曾經真正改變的唯一時間是在選項菜單,它究竟能不能讀的唯一時間是在比賽開始的時候。

Player.java

package com.example.gametest; 

import android.app.Activity; 

public class player extends Activity 
{ 
    int pID; 
    String PlayerName; 

    public player() 
    { 

    } 

    public void setpID(int ID){pID = ID;} 
    public int getpID (int ID){return pID;} 
    public void setPlayerName(String Name){PlayerName = Name;} 
    public String getPlayerName (int ID){return PlayerName;} 
} 

MainActivity.java

public class MainActivity extends Activity { 
player player1 = new player(); 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    player1.pID = 1; 
    player1.PlayerName = "NoName"; 
    setContentView(R.layout.activity_main); 
    TextView textbox1 = (TextView) findViewById(R.id.textView1); 
    textbox1.setText("Hello " + player1.PlayerName); 
} 
    public void optionsGame(View view) throws FileNotFoundException 
{ 
    // Do something in response to button 
    Intent intent = new Intent(this, options.class); 
    //EditText editText = (EditText) findViewById(R.id.editText1); 
    //String message = editText.getText().toString(); 
    //intent.putExtra(EXTRA_MESSAGE, message); 
    startActivity(intent); 
} 
} 

Options.java

import java.io.FileNotFoundException; 
import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.TextView; 

public class options extends Activity{ 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.optionpage); 
     //Intent intent = getIntent(); 
    } 

    public void backMain(View view) throws FileNotFoundException 
    { 
     super.onBackPressed(); 
    } 

    public void saveOptions(View view) throws FileNotFoundException 
    { 
     TextView editText1 = (TextView) findViewById(R.id.editText1); 
     Intent ActivityTwo = new Intent(); 

     Bundle bundle = new Bundle(); 

     bundle.putString("Player_Name", editText1.getText().toString()); 
     ActivityTwo.putExtras(bundle); 
     setResult(RESULT_OK, ActivityTwo); 
     finish(); 
    } 
} 

基本上,現在我只是想擁有它,這樣在選擇活動,名字可以在那裏設置。應該從我讀過的內容簡單,但不能得到它。

我知道爲什麼它的錯,沒有什麼是曾經真正被設置爲可以在活動實例之間旅行的值,但我不知道如何作出這樣的價值。

另外,如果你是woundering,在我的個XML我使用Android:的onClick = 「optionsGame」 和android:的onClick = 「saveOptions」。

所以,我看着這些,但還是沒能找到一個簡單的方法來做到這一點。

Sending data back to the Main Activity in android

how to pass values ​​between activity

回答

0

我想通了。 MainActivity.java

public void optionsGame(View view) throws FileNotFoundException 
{ 
    // Do something in response to button 
    Intent intent = new Intent(this, options.class); 
    Intent i = new Intent(this, options.class); 
    startActivityForResult(i, 1); 
    String message = player1.PlayerName; 
    intent.putExtra(EXTRA_MESSAGE, message); 
} 

和 Options.java

public void saveOptions(View view) throws FileNotFoundException 
{ 
    TextView editText1 = (TextView) findViewById(R.id.editText1); 
    Intent returnIntent = new Intent(); 
    returnIntent.putExtra("result" , editText1.getText().toString()); 
    setResult(RESULT_OK,returnIntent);  
    finish(); 
} 

,背,創建一個新的功能。 MainActivity.java

protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{ 

     if (requestCode == 1) 
     { 

     if(resultCode == RESULT_OK) 
     {  
      String result=data.getStringExtra("result"); 
      player1.PlayerName = result; 
      TextView textbox1 = (TextView) findViewById(R.id.textView1); 
      textbox1.setText("Hello " + player1.PlayerName); 
     } 
     if (resultCode == RESULT_CANCELED) 
     {  
      //Write your code if there's no result 
     } 
     } 
    }//onActivityResult