我正在使用Intents在我的简单的2视图篮球记分应用程序中切换视图。第一页和第一页是保留得分的页面,第二页是容纳5个玩家阵容(5个EditText)的页面。一切正常,但在切换视图后,乐谱和阵容不会保留其信息。例如,将分数设置为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"


>

<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>

最佳答案

尝试建立新的班级,如下所示

公开课成绩{

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

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

}

以及您在哪里使用homeScore来替换该变量

homeScore-> Score.homeScore

awayScore-> Score.awayScore

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

在onResume()方法中更新textviews

09-27 10:42