【问题标题】:onclick button replace buttons placesonclick 按钮替换按钮位置
【发布时间】:2014-06-17 12:07:49
【问题描述】:

我创建了一个有 6 个按钮的游戏,当您单击按钮 2 时按钮应该改变它们的位置,这是我所做的脚本:

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

我的问题是:按钮改变了它们的位置,因此您无法按下按钮,我该怎么做才能解决这个问题?

我想过这个选项:

得到按钮和顶部的Y坐标,除以6,然后我知道我应该给每个按钮的Y坐标范围是多少,我该怎么做?或者你能告诉我一个更好的方法吗?

【问题讨论】:

    标签: android button random action coordinate


    【解决方案1】:

    您的代码中有几个问题。首先,您应该重用任何可以重用的资源。在这种情况下,您应该存储对 Buttons 和 Random 的引用。您的数字始终相同,因此您可以将它们声明为类变量。最后,你可以通过记住“占用”的位置来解决你的问题。

    附:不要忘记不同的屏幕有不同的分辨率。

    工作代码!= 好代码。此外,还有一个名为 DRY - Don't Repeat Yourself 的编程指南。您的代码远非 DRY。通常,如果您多次复制代码 - 将其包装在方法中,而不是复制正文。您的 6 种操作方法可以用 5 倍的行数编写:

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

    将 Buttons 和 Random 定义为类变量,以便您可以重用它们:

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

    创建:

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

    当你点击它们时:

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

    要保持按钮分离,请检查随机值是否不与所有按钮重叠:

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

    【讨论】:

    • 我的代码运行良好,为什么会出现问题?数字不一样..每个按钮都转到另一个地方..解决方案是 - 按钮 1 将设置到位,而按钮 2 将无法在此位置设置?我该怎么做?
    • 对我来说太难了:(我的第一个应用程序,错误..在所有提到的 r 中:r 无法解析为变量并且在“if(button1.getY()>val&&button1 .getY()+width
    • 在这种情况下,您应该从一些基本的编程课程开始。任何语言,尽管较低级别或静态类型都会更好更难。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-13
    • 1970-01-01
    • 1970-01-01
    • 2021-12-18
    • 2021-07-04
    • 1970-01-01
    • 2016-09-09
    相关资源
    最近更新 更多