我如何可以通过点击按钮,机器人随意改变按钮现在的位置 [英] How i can change postion of the button randomly in android by clicking the button

查看:145
本文介绍了我如何可以通过点击按钮,机器人随意改变按钮现在的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这给了我一个错误在这里,setX的(INT)没有定义,虽然它是按钮式。

It gives me an error here that setX(int) is not defined though it is button type.

public class TouchMe extends Activity implements View.OnClickListener {

    Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.tuchme);

            btn = (Button) findViewById(R.id.btn);

            btn.setOnClickListener(this);

    }

    public void onClick(View v) {
            // TODO Auto-generated method stub
            switch (v.getId()) {
            case (R.id.btn): {
                    Random r = new Random();
                    int x = r.nextInt(480 - buttonWidth);
                    int y = r.nextInt(800 - buttonHeight);

                    btn.setX(x);   // give me error here that setX(int) is not defined      
                    btn.setY(y);   // is button type.

            }

            }

这给了我错误在这里,setX的(INT)没有被定义为按钮式。 请帮助我。

It gives me error here that setX(int) is not defined is button type. please help me.

推荐答案

View.setX()和View.setY()仅作为API级别11(的Andr​​oid 3.0蜂巢)的。你必须设置的minSdkVersion和targetSdkVersion 相应的编译反对API级别,如果你想使用这些方法11。

View.setX() and View.setY() are only available as of API level 11 (Android 3.0 Honeycomb). You have to set minSdkVersion and targetSdkVersion accordingly and compile against API level 11 if you want to use these methods.

这篇关于我如何可以通过点击按钮,机器人随意改变按钮现在的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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