我一直在使用android studio約五個星期。在這段時間裏,我學習瞭如何設置TextView的字符串並設置一個包含該字符串的字符串變量。我一直在嘗試爲Android設備製作應用程序遊戲,並在更改某些代碼時遇到問題。TextView的字符串設置錯誤 - 我該如何解決它?
textView myText應該指定一個彩色按鈕來按下,但是當我按下android模擬器中的開始按鈕時,隨機生成的文本和按鈕從不相互關聯,即使我有一系列if語句確保他們這樣做。
由於某些原因,當我運行代碼時,第一個顏色按鈕始終是正確的按鈕,然後運行方法colourSwitch,因爲它應該是正確的,但我不知道連接正確的彩色按鈕更改爲正確的文本。哦,我將這個活動與一個失敗的遊戲活動聯繫起來,當任何錯誤的按鈕被按下時,這個活動就會被調用。如果您想要其他活動的代碼,我很樂意展示。
下面是遊戲活動我的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>
下面是遊戲活動我的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]);
}
}
}
如果有人可以找出爲什麼我的代碼的行爲是這樣的,那將不勝感激。
我不確定它是否能正常工作,但更改'private'(或'public')語句的'final'語句並再次運行該應用程序。代碼對我來說似乎沒問題。 –