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

时间:2016-07-23 21:09:53

标签: java android

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

package 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);

        }
    }

    }

1 个答案:

答案 0 :(得分:1)

不要让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链。