简体   繁体   English

我如何通过单击按钮在 android 中随机更改按钮的位置

[英]How i can change postion of the button randomly in android by clicking the button

It gives me an error here that setX(int) is not defined though it is button type.它给了我一个错误,虽然 setX(int) 是按钮类型但未定义。

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.

            }

            }

It gives me error here that setX(int) is not defined is button type.它给我的错误是 setX(int) 未定义为按钮类型。 please help me.请帮我。

View.setX() and View.setY() are only available as of API level 11 (Android 3.0 Honeycomb). View.setX()和 View.setY() 仅在 API 级别 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.如果您想使用这些方法,您必须相应地设置minSdkVersion 和 targetSdkVersion并针对 API 级别 11 进行编译。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过单击Android中的按钮来更改布局中的视图? - How can I change Views in Layout by clicking a button in Android? 单击后如何更改按钮的位置? - How to change the postion of a button after click? 如何通过单击按钮更改布局的背景颜色 - How can i change background color of my layout by clicking a Button Android如何通过单击按钮更改Textview的数据 - Android How to change data of Textview by clicking on button 在另一个活动中单击某个按钮时,如何使该按钮可见? -安卓 - How can I make a button visible in one activity when clicking a button in another? - Android 如何通过单击按钮显示列表视图? - How can i display a listview by clicking on a button? 如何将按钮从一个位置转换到其他位置的android - How To Translate a button from one position to other postion android 我想在使用 kotlin 在 android 中单击按钮后更改背景 - i want to change background after clicking Button in android with kotlin 如何在单击按钮时更改按钮中的图像? - How to change an Image in a button on clicking the button? 如果按下按钮,如何更改页面? (Android,ViewPager) - How can I change the page if I press the button? (Android, ViewPager)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM