2012-11-11 36 views
-1

它给了我一个错误,虽然它是按钮类型,但未定义setX(int)。如何我可以通过点击按钮在Android中随机更改按钮的位置

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)没有定义是按钮类型。 请帮助我。

回答

0

View.setX()和View.setY()仅适用于API级别11(Android 3.0 Honeycomb)。如果要使用这些方法,则必须相应地设置minSdkVersion and targetSdkVersion并针对API级别11进行编译。

+0

谢谢你,它的工作,非常感谢 –

+0

谢谢devconsole它的工作原理,但现在的按钮有一段时间外面的屏幕! –

+0

我尝试也但这么想的工作: buttonHeight = 1 INT buttonWidth = 1 buttonHeigt = button.getHeight(); buttinWidth = button.getWidth(); int x = r.nextInt(480 - buttonWidth); int y = r.nextInt(800 - buttonHeight); 当我这样做的按钮不改变它的位置! PLZ帮助我 –