使用Intents(android)查看切换后信息不保存

时间:2016-03-31 05:32:38

标签: java android android-layout android-fragments android-intent

我正在使用Intents在我简单的2视图篮球计分应用中切换视图。第一页和主页是保持分数的页面,第二页是保持5个玩家阵容的页面(5个EditTexts)。一切正常,但在切换视图后,得分和阵容不会保留其信息。例如,得分设置为7-0,用户进入阵容页面并用球员名称更新,然后用户返回得分保持页面,得分将为0-0。我知道片段可以解决这个问题,但我不知道如何应用片段(虽然我已经多次阅读过文档),所以人们可以给我的最好帮助就是我需要改变的一个具体例子。我的代码发布在下面,非常感谢您的帮助。

//////////MainActivity.java (score keeping view)//////

package com.example.ryan.basketballscorekeeper;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.content.Intent;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

    Button lineupButton= (Button) findViewById(R.id.lineup_button);

        lineupButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this,       Main2Activity.class);
                startActivity(intent);
            }

        });
    TextView textView = (TextView) findViewById(R.id.homeTeamScore);
    textView.setText(homeScore+"");
    TextView textView2 = (TextView) findViewById(R.id.awayTeamScore);
    textView.setText(awayScore+"");



}


int homeScore=0; //home team score
int awayScore=0; //away team score
String awayScoreString=awayScore+"";
String homeScoreString=homeScore+"";



public void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
}

public void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
}

public void onStart(Bundle savedInstanceState){
    setContentView(R.layout.activity_main);
    onRestoreInstanceState(savedInstanceState);
}

public void onPause (Bundle savedInstanceState){
    onSaveInstanceState(savedInstanceState);
}
public void onDestroy (Bundle savedInstanceState){
    onSaveInstanceState(savedInstanceState);
}

public void lineupsView(View view){
    Intent intent = new Intent(this, Main2Activity.class);
    setContentView(R.layout.lineups);

}

public void mainView(View view){
    setContentView(R.layout.activity_main);
}

  ///////////////beginning of score-updating code////////////////

public void updateStrings(){
    homeScoreString=homeScore+"";
    awayScoreString=awayScore+"";
}

public void threePointerAway(View view){
    TextView textView = (TextView) findViewById(R.id.awayTeamScore);
    awayScore+=3;
    updateStrings();
    textView.setText(awayScoreString);
}
public void threePointerHome(View view){
    TextView textView = (TextView) findViewById(R.id.homeTeamScore);
    homeScore+=3;
    updateStrings();
    textView.setText(homeScoreString);
}
public void twoPointerHome(View view){
    TextView textView = (TextView) findViewById(R.id.homeTeamScore);
    homeScore+=2;
    updateStrings();
    textView.setText(homeScoreString);
}
public void twoPointerAway(View view){
    TextView textView = (TextView) findViewById(R.id.awayTeamScore);
    awayScore+=2;
    updateStrings();
    textView.setText(awayScoreString);
}
public void freeThrowHome(View view){
    TextView textView = (TextView) findViewById(R.id.homeTeamScore);
    homeScore+=1;
    updateStrings();
    textView.setText(homeScoreString);
}
public void freeThrowAway(View view){
    TextView textView = (TextView) findViewById(R.id.awayTeamScore);
    awayScore+=1;
    updateStrings();
    textView.setText(awayScoreString);
}

public void reset(View view){
    TextView awayView = (TextView) findViewById(R.id.awayTeamScore);
    TextView homeView = (TextView) findViewById(R.id.homeTeamScore);
    homeScore=0;
    awayScore=0;
    updateStrings();
    awayView.setText(awayScoreString);
    homeView.setText(homeScoreString);
}


}
 ///////activity_main.xml (score keeping xml file)//////

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout       xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.ryan.basketballscorekeeper.MainActivity"

&GT;

<ImageView
    android:layout_width="match_parent"
   android:layout_height="match_parent"
    android:src="@drawable/bballcourt"
    android:scaleType="centerCrop"
    />


<LinearLayout

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    >
    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:layout_gravity="center"
        android:orientation="vertical"
        >

        <TextView
            android:textColor="#ffffff"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Home Team"
            android:layout_margin="10dp"
            android:layout_gravity="center"
            android:textSize="15dp"
            />
        <TextView
            android:textColor="#ffffff"
            android:id="@+id/homeTeamScore"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="0"
            android:textSize="50dp"
            android:layout_gravity="center"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="+3 Points"
            android:background="#ffbf80"
            android:layout_gravity="center"
            android:onClick="threePointerHome"
            android:layout_marginTop="20dp"
            android:textSize="20dp"
            android:padding="10dp"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="+2 Points"
            android:background="#ffbf80"
            android:layout_gravity="center"
            android:onClick="twoPointerHome"
            android:layout_marginTop="20dp"
            android:textSize="20dp"
            android:padding="10dp"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Free Throw"
            android:background="#ffbf80"
            android:layout_gravity="center"
            android:onClick="freeThrowHome"
            android:layout_marginTop="20dp"
            android:textSize="17dp"
            android:padding="10dp"
            />
    </LinearLayout>

    <View
        android:layout_width="5dp"
        android:layout_height="340dp"
        android:background="#d9d9d9"
        android:layout_marginTop="40dp"

        />

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:layout_gravity="center"
        android:orientation="vertical"
        >

        <TextView
            android:textColor="#ffffff"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Away Team"
            android:layout_margin="10dp"
            android:layout_gravity="center"
            android:textSize="15dp"
            />
        <TextView
            android:textColor="#ffffff"
            android:id="@+id/awayTeamScore"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="0"
            android:textSize="50dp"
            android:layout_gravity="center"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="+3 Points"
            android:background="#ffbf80"
            android:layout_gravity="center"
            android:onClick="threePointerAway"
            android:layout_marginTop="20dp"
            android:textSize="20dp"
            android:padding="10dp"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="+2 Points"
            android:background="#ffbf80"
            android:layout_gravity="center"
            android:onClick="twoPointerAway"
            android:layout_marginTop="20dp"
            android:textSize="20dp"
            android:padding="10dp"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Free Throw"
            android:background="#ffbf80"
            android:layout_gravity="center"
            android:onClick="freeThrowAway"
            android:layout_marginTop="20dp"
            android:textSize="17dp"
            android:padding="10dp"
            />
    </LinearLayout>

 </LinearLayout>

 <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="reset"
    android:textAllCaps="true"
    android:layout_centerHorizontal="true"
    android:layout_alignParentBottom="true"
    android:padding="15dp"
    android:layout_marginBottom="40dp"
    android:textSize="20dp"
    android:background="#ffbf80"
    android:onClick="reset"

    />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Lineups"
    android:textAllCaps="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentBottom="true"
    android:padding="15dp"
    android:layout_margin="10dp"
    android:textSize="20dp"
    android:background="#ff8000"
    android:id="@+id/lineup_button" />

</RelativeLayout>


///////Main2Activity.java (lineup view)//////


  package com.example.ryan.basketballscorekeeper;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class Main2Activity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);

    Button mainButton= (Button) findViewById(R.id.main_button);

    mainButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            Intent intent = new Intent(Main2Activity.this,    MainActivity.class);
            startActivity(intent);
        }

    });

    EditText editText1 = (EditText) findViewById(R.id.one);
    one=editText1.getText().toString();
    EditText editText2 = (EditText) findViewById(R.id.two);
    two=editText1.getText().toString();
    EditText editText3 = (EditText) findViewById(R.id.three);
    three=editText1.getText().toString();
    EditText editText4 = (EditText) findViewById(R.id.four);
    four=editText1.getText().toString();
    EditText editText5 = (EditText) findViewById(R.id.five);
    five=editText1.getText().toString();

}
String one,two,three,four,five; //this represents the five players
}

////////activity_main2.xml (lineup view xml file)///////
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout   xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.ryan.basketballscorekeeper.Main2Activity"
android:orientation="vertical"
>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/bballcourt"
        android:scaleType="centerCrop"
        />

    <TextView
        android:id="@+id/lineups"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#ffffff"
        android:text="Lineups"
        android:textSize="30dp"
        android:padding="30dp"
        />

    <EditText
        android:textColor="#ffffff"
        android:id="@+id/one"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textNoSuggestions"
        android:maxLines="1"
        android:layout_below="@id/lineups"
        />
    <EditText
        android:textColor="#ffffff"
        android:id="@+id/two"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textNoSuggestions"
        android:maxLines="1"
        android:layout_below="@id/one"
        />
    <EditText
        android:textColor="#ffffff"
        android:id="@+id/three"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textNoSuggestions"
        android:maxLines="1"
        android:layout_below="@id/two"
        />
    <EditText
        android:textColor="#ffffff"
        android:id="@+id/four"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textNoSuggestions"
        android:maxLines="1"
        android:layout_below="@id/three"
        />
    <EditText
        android:textColor="#ffffff"
        android:id="@+id/five"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textNoSuggestions"
        android:maxLines="1"
        android:layout_below="@id/four"
        />
    <Button
        android:id="@+id/main_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Score"
        android:textAllCaps="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentBottom="true"
        android:padding="15dp"
        android:layout_margin="10dp"
        android:textSize="20dp"
        android:background="#ff8000"

        />
    </RelativeLayout>

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

尝试创建新类,如下所示

公共课成绩{

public static int homeScore = 0; //主队得分

public static int awayScore = 0; //客场得分

}

你在哪里使用homeScore用

重新表示该变量

homeScore - &gt; Score.homeScore

awayScore - &gt; Score.awayScore

并删除MainActivity中的本地分数变量

更新onResume()方法

中的textviews