切换内容视图时,信息将无法保存

时间:2016-03-30 06:39:03

标签: java android android-layout android-studio

我是Android开发人员的新手,我正在尝试创建一个简单的应用程序,以保持篮球比赛和球队阵容的得分。问题是每当我在视图之间来回切换时,得分(或阵容)都会显示为零(或没有玩家)的默认设置。我添加了代码(我尽可能地将其格式化)但我相信,因为它可能是一个逻辑错误,唯一重要的部分是顶部的MainActivity类。谢谢你的帮助!

package com.example.ryan.basketballscorekeeper;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    homeScore=0;
    awayScore=0;
}

int homeScore;
int awayScore;


public void lineupsView(View view){
    TextView textView = (TextView) findViewById(R.id.homeTeamScore);
    textView.setText(homeScore+"");
    TextView textView2 = (TextView) findViewById(R.id.awayTeamScore);
    textView.setText(awayScore+"");
    setContentView(R.layout.lineups);
}
public void mainView(View view){

    EditText editText1 = (EditText) findViewById(R.id.one);
    editText1.setText(editText1.getText());
    EditText editText2 = (EditText) findViewById(R.id.two);
    editText2.setText(editText2.getText());
    EditText editText3 = (EditText) findViewById(R.id.three);
    editText3.setText(editText3.getText());
    EditText editText4 = (EditText) findViewById(R.id.four);
    editText4.setText(editText4.getText());
    EditText editText5 = (EditText) findViewById(R.id.five);
    editText5.setText(editText5.getText());
    setContentView(R.layout.activity_main);
}


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

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


}

////////////// .java文件的结尾/////////////////////////

//////////////分数开始观看//////////////////////////          

<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:onClick="lineupsView" />

</RelativeLayout>
//////////End Of Scorekeeping View/////////////////////////////////

//////////Beginning of Lineups view/////////////////////////////////
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<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: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"
    android:onClick="mainView"
    />
</RelativeLayout>



</LinearLayout

1 个答案:

答案 0 :(得分:1)

尝试保存实例的状态,然后在再次显示活动时重置以覆盖这两种方法:

public void onSaveInstanceState(Bundle savedInstanceState) {}
public void onRestoreInstanceState(Bundle savedInstanceState) {}

因此,当恢复活动时,您可以恢复相应包中保留的所有数据。

例如:

static final String STATE_SCORE = "playerScore";
static final String STATE_LEVEL = "playerLevel";
...

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    // Save the user's current game state
    savedInstanceState.putInt(STATE_SCORE, mCurrentScore);
    savedInstanceState.putInt(STATE_LEVEL, mCurrentLevel);

    // Always call the superclass so it can save the view hierarchy state
    super.onSaveInstanceState(savedInstanceState);
}

然后,您可以覆盖OnCreate或onRestoreInstanceState以获取旧状态:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); // Always call the superclass first

    // Check whether we're recreating a previously destroyed instance
    if (savedInstanceState != null) {
        // Restore value of members from saved state
        mCurrentScore = savedInstanceState.getInt(STATE_SCORE);
        mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL);
    } else {
        // Probably initialize members with default values for a new instance
    }
    ...
}  
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
    // Always call the superclass so it can restore the view hierarchy
    super.onRestoreInstanceState(savedInstanceState);

    // Restore state members from saved instance
    mCurrentScore = savedInstanceState.getInt(STATE_SCORE);
    mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL);
}