的onclick按钮替换按钮的地方 [英] onclick button replace buttons places

查看:191
本文介绍了的onclick按钮替换按钮的地方的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个游戏,有6个按键,应该当您单击按钮2改变自己的位置的按钮,这里是我做的脚本:

I created a game which has 6 buttons, the buttons supposed to change their places when you click button2, here is the script I did:

if (id == R.id.button2) {


            action1();
            action2();
            action3();
            action4();
            action5();
            action6();

            }

和这里是动作:

private void action1() {
    // TODO Auto-generated method stub
    Button button1 = (Button)findViewById(R.id.button1);
    button1.setX(50);
    int min = 0;
    int max = 200;
    Random r = new Random();
    int i1 = r.nextInt(max - min + 1) + min;
    button1.setY(i1);
    }   
private void action2() {
    // TODO Auto-generated method stub
    Button button2 = (Button)findViewById(R.id.button2);
    button2.setX(50);
    int min = 0;
    int max = 1200;
    Random r = new Random();
    int i1 = r.nextInt(max - min + 1) + min;
    button2.setY(i1);       }  
private void action3() {
    // TODO Auto-generated method stub
    Button button3 = (Button)findViewById(R.id.button3);
    button3.setX(50);
    int min = 400;
    int max = 600;
    Random r = new Random();
    int i1 = r.nextInt(max - min + 1) + min;
    button3.setY(i1);       }  
private void action4() {
    // TODO Auto-generated method stub
    Button button4 = (Button)findViewById(R.id.button4);
    button4.setX(50);
    int min = 600;
    int max = 800;
    Random r = new Random();
    int i1 = r.nextInt(max - min + 1) + min;
    button4.setY(i1);       }  
private void action5() {
    // TODO Auto-generated method stub
    Button button5 = (Button)findViewById(R.id.button5);
    button5.setX(50);
    int min = 800;
    int max = 1000;
    Random r = new Random();
    int i1 = r.nextInt(max - min + 1) + min;
    button5.setY(i1);       }  
private void action6() {
    // TODO Auto-generated method stub
    Button button6 = (Button)findViewById(R.id.button6);
    button6.setX(50);
    int min = 1000;
    int max = 1200;
    Random r = new Random();
    int i1 = r.nextInt(max - min + 1) + min;
    button6.setY(i1);       
    }  

我的问题是:这些按钮改变他们的地方,一个在其他的,你不能preSS,因为它的按钮,我能做些什么来解决这个问题。

My problem is: the buttons changing their places to one on the other and you can't press the buttons because of it, what can I do to fix this?

我想过这个选项:

得到BUTTOM和顶部,除以6的Y坐标,然后我才知道什么是Y坐标我应该给每个按钮的范围内,我该怎么办呢?或者你可以告诉我一个更好的办法?

get the Y coordinate of the buttom and the top, divide by 6, and then I know what is the range of the Y coordinate I should give for each button, how can I do it? or can you tell me about a better way?

推荐答案

有在code几个问题。首先,你应该重用任何资源可以重复使用。在这种情况下,你应该存储到您的按钮和随机引用。你的号码都是一样的,所以你可以声明为类变量。最后,您可以通过记住占领阵地解决您的问题。

There are several problems in your code. First, You should reuse whatever resources you can reuse. In this case, you should store references to your Buttons and Random. Your numbers are always the same, so you may declare them as class variables. Lastly, you can solve your problems by remembering "occupied" positions.

P.S。不要忘了,不同的屏幕有不同的分辨率。

P.S. don't forget that different screens have different resolutions.

工作code!=良好的code。此外,还有所谓干的编程指南 - 不要重复自己。您code是远远还没有干。一般来说,如果您复制code几次 - 敷在方法,而不是复制体。你6的操作方法可以写在5次少行:

Working code != good code. Also, there is programming guideline called DRY - Don't Repeat Yourself. Your code is far from DRY. In general, if you copy your code several times - wrap it in method, rather than copy the body. Your 6 action methods could be written in 5 times less lines:

private void action(Button b, int min, int max){
    b.setX(50); 
    b.setY(r.nextInt(range));
}

定义按键和随机为类变量,这样你就可以重新使用它们:

Define Buttons and Random as class variables, so you can reuse them:

public class MyClass extends Activity{
Random rl
Button button1;
...
Button button6;
//List<Button> buttons = new ArrayList<Button>(); might be even better

的onCreate:

onCreate:

r = new Random();
button1 = (Button)findViewById(R.id.button1);
...
button6 = (Button)findViewById(R.id.button6);

当你点击它们:

if(id==R.id.button2){
    action(button1,0,200);
    ...
}

要保持分离按钮,检查是否随机值并不适用于所有的按钮重叠:

To keep buttons separated, check if random value does not overlap with all buttons:

int getRandom(int min,int max){
    int val = r.nextInt(range);
    if(button1.getY()>val&&button1.getY()+width<val){
        //buttons overlap, try another random value
    } else {
        //check other buttons
    }
}

这篇关于的onclick按钮替换按钮的地方的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆