[英]A TextView's String is being set wrong - How can I fix it?
I have been working with android studio for about five weeks. 我已经在android studio工作了大约五个星期。 In this time, I have learned how to set a TextView's string and set a string variable containing that string. 这一次,我学习了如何设置TextView的字符串以及设置包含该字符串的字符串变量。 I have been trying to make an app game for android devices, and have encountered a problem whilst changing some code. 我一直在尝试为Android设备制作应用程序游戏,并且在更改某些代码时遇到了问题。
The textView myText is supposed to specify a coloured button to press, but when I press the start button in the android emulator, the randomly generated text and button never correlate with each other, even though I have a series of if statements to make sure that they do. textView myText应该指定要按下的彩色按钮,但是当我按下android仿真器中的“开始”按钮时,即使我有一系列if语句来确保该操作,随机生成的文本和按钮也从未相互关联。他们是这样。
for some reason when I run the code the first colour button is always the correct button, and then runs the method colourSwitch as it should if it is correct, but I do not know what the problem is regarding connecting the correct coloured button to the correct text. 由于某些原因,当我运行代码时,第一个颜色按钮始终是正确的按钮,然后在正确的情况下运行方法colourSwitch,但是我不知道将正确的颜色按钮连接到正确的问题是什么文本。 Oh, and I am connecting this activity with a losing game activity which is called when any wrong button is pressed. 哦,我将这个活动与一个失败的游戏活动联系在一起,当按下任何错误的按钮时,该活动就会被调用。 If you want the code for the other activity, I am happy to show. 如果您需要其他活动的代码,我很乐意展示。
Here is my xml file for the game activity: 这是我用于游戏活动的xml文件:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/game_activity"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.a2118667.red.MainActivity"
tools:layout_editor_absoluteX="0dp"
android:layout_marginTop="0dp"
app:layout_constraintTop_toBottomOf="@+id/indicatorText">
<Button
android:id="@+id/startButton"
android:layout_width="175dp"
android:layout_height="75dp"
android:layout_marginTop="8dp"
android:text="@string/Game_Start_Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.156" />
<GridLayout
android:id="@+id/gridLayout"
android:layout_width="299dp"
android:layout_height="301dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.505"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.88">
<Button
android:id="@+id/button1"
android:layout_width="150dp"
android:layout_height="150dp" />
<Button
android:id="@+id/button3"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_column="0"
android:layout_row="1" />
<Button
android:id="@+id/button2"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_column="1"
android:layout_row="0" />
<Button
android:id="@+id/button4"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_column="1"
android:layout_row="1" />
</GridLayout>
<TextView
android:id="@+id/indicatorText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:text=" "
android:textAppearance="@style/TextAppearance.AppCompat.Button"
android:textSize="34sp"
app:layout_constraintBottom_toTopOf="@+id/startButton"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHorizontal_bias="0.501"
app:layout_constraintVertical_bias="1.0" />
</android.support.constraint.ConstraintLayout>
Here is my Java file for the game activity. 这是我用于游戏活动的Java文件。
package com.example.a2118667.red;
//imports
import android.content.Intent;
import android.support.v4.media.session.MediaSessionCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.view.MotionEvent;
import android.view.GestureDetector;
import android.support.v4.view.GestureDetectorCompat;
import android.widget.TextView;
import java.util.Random;
import static com.example.a2118667.red.R.id.startButton;
public class MainActivity extends AppCompatActivity {
final int[] cowbears = {0};
final int[] cowbears2 = {0};
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView myText = (TextView) findViewById(R.id.indicatorText);
final Button myButton = (Button) findViewById(R.id.startButton);
final Button button1 = (Button) findViewById(R.id.button1);
final Button button2 = (Button) findViewById(R.id.button2);
final Button button3 = (Button) findViewById(R.id.button3);
final Button button4 = (Button) findViewById(R.id.button4);
myButton.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) {
((ViewGroup) myButton.getParent()).removeView(myButton);
colourSwitch();
}
}
);
button4.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) {
myText.setText("cowBears rock");
// if button says yellow and button is yellow
if (cowbears[0] == 2 && cowbears2[0] <= 1 || cowbears[0] == 6 && cowbears2[0] <= 1) {
myText.setText("You did it, mommy!");
}
// if button says blue and button blue
if (cowbears[0] == 3 && cowbears2[0] == 2 || cowbears[0] == 7 && cowbears2[0] == 2) {
myText.setText("You did it, mommy!");
}
// if button says green and button green
if (cowbears[0] == 4 && cowbears2[0] == 3 || cowbears[0] == 8 && cowbears2[0] == 3) {
myText.setText("You did it, mommy!");
}
// if button says red and button red
if (cowbears[0] <= 1 && cowbears2[0] == 4 || cowbears[0] == 5 && cowbears2[0] == 4) {
myText.setText("You did it, mommy!");
}
String verify = myText.getText().toString();
if (verify.equals("cowBears rock")){
losedThegameBoi();
}
String check = myText.getText().toString();
if (check.equals("You did it, mommy!")) {
colourSwitch();
}
}
}
);
button3.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) {
myText.setText("cowBears rock");
// if button says yellow and button is yellow
if (cowbears[0] == 3 && cowbears2[0] <= 1 || cowbears[0] == 8 && cowbears2[0] <= 1) {
myText.setText("You did it, mommy!");
}
// if button says blue and button blue
if (cowbears[0] == 4 && cowbears2[0] == 2 || cowbears[0] == 5 && cowbears2[0] == 2) {
myText.setText("You did it, mommy!");
}
// if button says green and button green
if (cowbears[0] <= 1 && cowbears2[0] == 3 || cowbears[0] == 6 && cowbears2[0] == 3) {
myText.setText("You did it, mommy!");
}
// if button says red and button red
if (cowbears[0] == 2 && cowbears2[0] == 4 || cowbears[0] == 7 && cowbears2[0] == 4) {
myText.setText("You did it, mommy!");
}
String verify = myText.getText().toString();
if (verify.equals("cowBears rock")){
losedThegameBoi();
}
String check = myText.getText().toString();
if (check.equals("You did it, mommy!")) {
colourSwitch();
}
}
}
);
button2.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) {
myText.setText("cowBears rock");
// if button says yellow and button yellow
if (cowbears[0] == 4 && cowbears2[0] <= 1 || cowbears[0] == 7 && cowbears2[0] <= 1) {
myText.setText("You did it, mommy!");
}
// if button says blue and button blue
if (cowbears[0] <= 1 && cowbears2[0] == 2 || cowbears[0] == 8 && cowbears2[0] == 2) {
myText.setText("You did it, mommy!");
}
// if button says green and button green
if (cowbears[0] == 2 && cowbears2[0] == 3 || cowbears[0] == 5 && cowbears2[0] == 3) {
myText.setText("You did it, mommy!");
}
// if button says red and button red
if (cowbears[0] == 3 && cowbears2[0] == 4 || cowbears[0] == 6 && cowbears2[0] == 4) {
myText.setText("You did it, mommy!");
}
String verify = myText.getText().toString();
if (verify.equals("cowBears rock")){
losedThegameBoi();
}
String check = myText.getText().toString();
if (check.equals("You did it, mommy!")) {
colourSwitch();
}
}
}
);
// onClick Listeners for buttons
button1.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) {
//debugged
myText.setText("cowBears rock");
// if button says yellow and button yellow
if (cowbears[0] <= 1 && cowbears2[0] <= 1 || cowbears[0] == 5 && cowbears2[0] <= 1) {
myText.setText("You did it, mommy!");
}
// if button says blue and button blue
if (cowbears[0] == 2 && cowbears2[0] == 2 || cowbears[0] == 6 && cowbears2[0] == 2) {
myText.setText("You did it, mommy!");
}
// if button says green and button green
if (cowbears[0] == 3 && cowbears2[0] == 3 || cowbears[0] == 7 && cowbears2[0] == 3) {
myText.setText("You did it, mommy!");
}
// if button says red and button red
if (cowbears[0] == 4 && cowbears2[0] == 4 || cowbears[0] == 8 && cowbears2[0] == 4) {
myText.setText("You did it, mommy!");
}
String verify = myText.getText().toString();
if (verify.equals("cowBears rock")){
losedThegameBoi();
}
String check = myText.getText().toString();
if (check.equals("You did it, mommy!")) {
colourSwitch();
}
}
}
);
}
private void losedThegameBoi() {
Intent intent = new Intent(this, Loser_Activity.class);
startActivity(intent);
}
private void colourSwitch() {
final TextView myText = (TextView) findViewById(R.id.indicatorText);
final Button button1 = (Button) findViewById(R.id.button1);
final Button button2 = (Button) findViewById(R.id.button2);
final Button button3 = (Button) findViewById(R.id.button3);
final Button button4 = (Button) findViewById(R.id.button4);
final int[] cowBears = {0};
final int[] cowBears2 = {0};
final int[] cowbears = {0};
final int[] cowbears2 = {0};
//Random number generator for randomizing colour layout
Random randomGenerator = new Random();
//For generating what colour pattern to be displayed
int randomInt = randomGenerator.nextInt(9);
myText.setText("Generated : " + randomInt);
cowBears[0] = randomInt;
cowbears[0] = randomInt;
//Colour patterns based on what integer is generated
//Random number generator for randomizing button click stuff
Random randomGenerator2 = new Random();
//For generating which text to be displayed
int randomInt2 = randomGenerator2.nextInt(5);
myText.setText("Generated : " + randomInt2);
cowBears2[0] = randomInt2;
cowbears2[0] = randomInt2;
// if statements for the colour scheme
if (cowBears[0] <= 1) {
button1.setBackgroundResource(R.drawable.yellow);
button2.setBackgroundResource(R.drawable.blue);
button3.setBackgroundResource(R.drawable.green);
button4.setBackgroundResource(R.drawable.download);
}
if (cowBears[0] == 2) {
button4.setBackgroundResource(R.drawable.yellow);
button1.setBackgroundResource(R.drawable.blue);
button2.setBackgroundResource(R.drawable.green);
button3.setBackgroundResource(R.drawable.download);
}
if (cowBears[0] == 3) {
button3.setBackgroundResource(R.drawable.yellow);
button4.setBackgroundResource(R.drawable.blue);
button1.setBackgroundResource(R.drawable.green);
button2.setBackgroundResource(R.drawable.download);
}
if (cowBears[0] == 4) {
button2.setBackgroundResource(R.drawable.yellow);
button3.setBackgroundResource(R.drawable.blue);
button4.setBackgroundResource(R.drawable.green);
button1.setBackgroundResource(R.drawable.download);
}
if (cowBears[0] == 5) {
button1.setBackgroundResource(R.drawable.yellow);
button3.setBackgroundResource(R.drawable.blue);
button2.setBackgroundResource(R.drawable.green);
button4.setBackgroundResource(R.drawable.download);
}
if (cowBears[0] == 6) {
button4.setBackgroundResource(R.drawable.yellow);
button1.setBackgroundResource(R.drawable.blue);
button3.setBackgroundResource(R.drawable.green);
button2.setBackgroundResource(R.drawable.download);
}
if (cowBears[0] == 7) {
button2.setBackgroundResource(R.drawable.yellow);
button4.setBackgroundResource(R.drawable.blue);
button1.setBackgroundResource(R.drawable.green);
button3.setBackgroundResource(R.drawable.download);
}
if (cowBears[0] == 8) {
button3.setBackgroundResource(R.drawable.yellow);
button2.setBackgroundResource(R.drawable.blue);
button4.setBackgroundResource(R.drawable.green);
button1.setBackgroundResource(R.drawable.download);
}
//Here we have the randomizing of the button text based on what integer is genetated for cowbears2
if (cowBears2[0] <= 1) {
myText.setText("yellow" + cowBears[0] + cowBears2[0]);
}
if (cowBears2[0] == 2) {
myText.setText("Blue" + cowBears[0] + cowBears2[0]);
}
if (cowBears2[0] == 3) {
myText.setText("Green" + cowBears[0] + cowBears2[0]);
}
if (cowBears2[0] == 4) {
myText.setText("Red" + cowBears[0] + cowBears2[0]);
}
}
}
If anyone could figure out why my code behaves like this, that would be appreciated. 如果有人能弄清楚为什么我的代码如此行事,那将不胜感激。
I found the answer. 我找到了答案。 You see my cowbears, and cowBears integers were being randomized differently. 您会看到我的Cowbears,并且CowBears整数的分配方式有所不同。
The cowBears, cowBears2, cowbears, and cowbears2 final integers must be declared directly above the onCreate method, as the upper case cowBear integers were being redeclared during a button press. 必须在onCreate方法上方直接声明cowBears,cowBears2,cobbears和cowbears2最终整数,因为在按下按钮时会重新声明大写的cowBear整数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.