代码之家  ›  专栏  ›  技术社区  ›  lockgar

Java android,如何在活动之间传递值。主菜单、选项菜单

  •  0
  • lockgar  · 技术社区  · 10 年前

    尝试为android制作一个简单的游戏应用程序,设置选项时遇到问题。

    现在就做一个基本的设置。

    创建了一个全局类,因为只有在选项菜单中才能真正更改,只有在游戏开始时才能读取。

    播放器.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;}
    }
    

    主要活动.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);  
    }
    }
    

    选项.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();
        }
    }
    

    基本上,现在我只想拥有它,以便在options活动中,可以在那里设置名称。从我读到的内容来看,应该很简单,但无法理解。

    我知道为什么这是错误的,没有任何东西被真正设置为可以在Activity实例之间传递的值,但我不知道如何设置这样的值。

    此外,如果你正在玩,在我的xmls中,我使用android:onClick=“optionsGame”和android:on Click=“saveOptions”。

    所以我看了这些,但仍然找不到一个简单的方法。

    Sending data back to the Main Activity in android

    how to pass values ​​between activity

    1 回复  |  直到 7 年前
        1
  •  0
  •   lockgar    10 年前

    我想通了。 主要活动.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);  
    }
    

    和 选项.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();
    }
    

    然后返回,创建一个新函数。 主要活动.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