gpt4 book ai didi

java - 如何使用按钮和字符串创建扩展的 if-then-else 语句

转载 作者:行者123 更新时间:2023-12-01 09:39:51 24 4
gpt4 key购买 nike

我正在尝试创建一个 Activity ,其中有九个不同颜色的按钮。我希望这些按钮仅在按下相应的颜色文本字符串时才随机更改其上方显示的文本字符串。但是,当我运行该应用程序时,只有绿色按钮会更改文本字符串,即使文本字符串显示绿色以外的颜色,它也会更改它。我的 if-then-else 代码是否做错了什么? (我还没有完成其余六个颜色按钮的代码。)非常感谢!!!

包 com.example.franco.sampleapp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.res.Resources;
import android.view.View;
import android.widget.TextView;
import android.widget.Button;
import java.lang.String;
import java.util.Random;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {

private static final Random r_generator = new Random();
String textViewString;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv = (TextView) findViewById(R.id.color_text);

Button green= (Button) findViewById(R.id.green_button);
green.setOnClickListener(this);
Button blue = (Button) findViewById(R.id.blue_button);
blue.setOnClickListener(this);
Button red = (Button) findViewById(R.id.red_button);
red.setOnClickListener(this);
Button yellow= (Button) findViewById(R.id.yellow_button);
yellow.setOnClickListener(this);
Button white = (Button) findViewById(R.id.white_button);
white.setOnClickListener(this);
Button orange = (Button) findViewById(R.id.orange_button);
orange.setOnClickListener(this);
Button brown= (Button) findViewById(R.id.brown_button);
brown.setOnClickListener(this);
Button pink = (Button) findViewById(R.id.pink_button);
pink.setOnClickListener(this);
Button purple = (Button) findViewById(R.id.purple_button);
purple.setOnClickListener(this);
textViewString = tv.getText().toString();

}

@Override
public void onClick(View v) {

if (v.getId() == R.id.green_button && textViewString.equals("Green")) {

Resources res = getResources();
String[] myString = res.getStringArray(R.array.colorArray);
String q = myString[r_generator.nextInt(myString.length)];
TextView tv = (TextView) findViewById(R.id.color_text);
tv.setText(q);

} else if (v.getId() == R.id.blue_button && textViewString.equals("Blue")) {

Resources res = getResources();
String[] myString = res.getStringArray(R.array.colorArray);
String q = myString[r_generator.nextInt(myString.length)];
TextView tv = (TextView) findViewById(R.id.color_text);
tv.setText(q);

} else if (v.getId() == R.id.red_button && textViewString.equals("Red")){

Resources res = getResources();
String[] myString = res.getStringArray(R.array.colorArray);
String q = myString[r_generator.nextInt(myString.length)];
TextView tv = (TextView) findViewById(R.id.color_text);
tv.setText(q);

}
}

}

最佳答案

不要将您的 MainActivity 设为 OnClickListener

相反,创建匿名 OnClickListener 来添加到每个按钮:

Button green= (Button) findViewById(R.id.green_button);
green.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
if (!textViewString.equals("Green")) return;
Resources res = getResources();
String[] myString = res.getStringArray(R.array.colorArray);
String q = myString[r_generator.nextInt(myString.length)];
TextView tv = (TextView) findViewById(R.id.color_text);
tv.setText(q);
}
});

Button blue = (Button) findViewById(R.id.blue_button);
blue.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
if (!textViewString.equals("Blue")) return;
Resources res = getResources();
String[] myString = res.getStringArray(R.array.colorArray);
String q = myString[r_generator.nextInt(myString.length)];
TextView tv = (TextView) findViewById(R.id.color_text);
tv.setText(q);
}
});

等等

那么就不需要很长的 if/else 链了。

关于java - 如何使用按钮和字符串创建扩展的 if-then-else 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38546450/

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