gpt4 book ai didi

java - 使用 Intents 进行 View 切换后信息不会保存(android)

转载 作者:太空宇宙 更新时间:2023-11-04 12:45:56 25 4
gpt4 key购买 nike

我正在使用 Intents 在我的简单的 2 View 篮球记分应用程序中切换 View 。第一页也是主页,是保存分数的页面,第二页是保存 5 名玩家阵容的页面(5 个 EditText)。一切正常,但分数和阵容在 View 切换后不会保留其信息。例如,比分设置为7-0,用户进入阵容页面并更新球员姓名,然后用户返回比分记录页面,比分将为0-0。我知道 fragment 可能会解决这个问题,但我不知道如何应用 fragment (尽管我已阅读文档多次),因此可以给我的最好帮助就是我需要更改的具体示例。我的代码贴在下面,非常感谢您的帮助。

//////////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)静态int homeScore = 0;//主队得分

公共(public)静态intawayScore = 0;//客队得分

}

无论你在哪里使用 homeScore 替换该变量

homeScore -> Score.homeScore

awayScore -> Score.awayScore

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

更新 onResume() 方法中的 TextView

关于java - 使用 Intents 进行 View 切换后信息不会保存(android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36324920/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com