如何让onClick事件重复

时间:2016-07-24 00:27:16

标签: java android

我有一些Java代码,我想作为多项选择琐事风格的游戏。随机颜色字符串显示在TextView中,当按下正确的相应颜色按钮时,TextView中的随机字符串将更新。但是,这仅适用一次。启动活动时,唯一可以更改TextView字符串的颜色按钮是与TextView中当前字符串对应的颜色按钮。然而,在此之后,这些功能似乎会锁定"并且第一个字符串的相应颜色按钮是唯一可以使TextView字符串更改的按钮。有没有办法重置onClick事件或让它们循环?非常感谢!!!!

public class MainActivity extends AppCompatActivity {

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

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Resources res = getResources();
    TextView tv = (TextView) findViewById(R.id.color_text);
    String[] myString = res.getStringArray(R.array.colorArray);
    String q = myString[r_generator.nextInt(myString.length)];
    tv.setText(q);
    textViewString = tv.getText().toString();


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

    Button red = (Button) findViewById(R.id.red_button);
    red.setOnClickListener(new View.OnClickListener() {
        @Override public void onClick(View v) {
            if (!textViewString.equals("Red")) 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 yellow = (Button) findViewById(R.id.yellow_button);
    yellow.setOnClickListener(new View.OnClickListener() {
        @Override public void onClick(View v) {
            if (!textViewString.equals("Yellow")) 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 white = (Button) findViewById(R.id.white_button);
    white.setOnClickListener(new View.OnClickListener() {
        @Override public void onClick(View v) {
            if (!textViewString.equals("White")) 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 orange = (Button) findViewById(R.id.orange_button);
    orange.setOnClickListener(new View.OnClickListener() {
        @Override public void onClick(View v) {
            if (!textViewString.equals("Orange")) 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 brown = (Button) findViewById(R.id.brown_button);
    brown.setOnClickListener(new View.OnClickListener() {
        @Override public void onClick(View v) {
            if (!textViewString.equals("Brown")) 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 pink = (Button) findViewById(R.id.pink_button);
    pink.setOnClickListener(new View.OnClickListener() {
        @Override public void onClick(View v) {
            if (!textViewString.equals("Pink")) 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 purple = (Button) findViewById(R.id.purple_button);
    purple.setOnClickListener(new View.OnClickListener() {
        @Override public void onClick(View v) {
            if (!textViewString.equals("Purple")) 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);

        }
    });



    }
}

2 个答案:

答案 0 :(得分:0)

你编码很冗余。如果你发现你已经重复了这一部分

        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);
        // here is what you forgot 
        textViewString = q;

你可以创建一个像reset()这样的方法,并在你的onClick()中调用这个方法。

你可以做的最好的方法是只使用像

这样的View参数的单一方法
public void myButtonClickMethod(View v)
{
    switch(v.getId())
    {
        // check here witch button was pressed
        case R.id.red_button:
            // do the job here for red button
            break;
        case R.id.blue_button: 
            // do the job here for blue button
            break;
        ....
        .....
    }
    // after job done call your rest() method
    reset(); 
}

现在在每个按钮的layout.xml上添加onClick属性

<Button
    ....
    ....
  android:onClick="myButtonClickMethod"

我希望这有帮助。

答案 1 :(得分:0)

所以Franco,您可以做两件事来使您的代码更简洁并解决您的问题。首先,我建议将onClick()函数合并为onClick()。首先,您需要更新XML代码。在每个Button中添加以下行

android:onClick="onClick"

这意味着使用此onClick函数定义的每个按钮都会在单击时执行onClick函数中的代码。您的onClick函数应该类似于以下内容。

public void onClick(View view){
    //This casts your view to be a button so you can access its features as a button
    Button btn = (Button) view;
    textViewString = tv.getText().toString();

    if(!btn.getText().toString().equals(textViewString)){
         //Perform action to notify user that they pressed the wrong button
         //Do not return here. This is what caused your program to lock up
         //Maybe add a TextView that says correct/incorrect and you can change that when the user is correct or incorrect
    }

    //Gets a random color from your string array and sets it to the TextView
    q = myString[r_generator.nextInt(myString.length)];
    tv.setText(q);
}