这是我的代码:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tuch_me);
btn = (Button) findViewById(R.id.bttuch);
timer = (TextView) findViewById(R.id.tvtimer);
timeout = (TextView) findViewById(R.id.tvtimeout);
level = (TextView) findViewById(R.id.tvlevel);
tv =(TextView) findViewById(R.id.textView1);
btn.setOnClickListener(this);
buttonWidth = 1;
buttonHight = 1;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_tuch_me, menu);
return true;
}
@SuppressLint({ "NewApi", "NewApi" })
@TargetApi(11)
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.bttuch:
Random r = new Random();
int xx=getResources().getDisplayMetrics().widthPixels;
int yy=getResources().getDisplayMetrics().heightPixels;
int x = r.nextInt(xx);
int y = r.nextInt(yy);
buttonWidth = btn.getWidth();
buttonHight = btn.getHeight();
btn.setX(xx-buttonWidth);
btn.setY(yy-buttonHight);
break;
}
}
}
当我单击按钮时,它会超出屏幕!我什至将实际分辨率设置为 xx 和 yy,但它仍然不起作用。我怎样才能限制它留在屏幕内?