尝试为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();
}
}
基本上,现在我只想拥有它,以便在options活动中,可以在那里设置名称。从我读过的内容应该很简单,但不能得到它。
我知道为什么它的错误,没有任何东西真的被设置为可以在Activity实例之间传递的值,但我不知道如何制作这样的值。
另外,如果你想要在我的xmls中使用android:onClick =“optionsGame”和android:onClick =“saveOptions”。
所以我看了这些,但仍然找不到一个简单的方法来做到这一点。
答案 0 :(得分: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