これが私のコードです:
@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 として実際の解像度を指定していますが、それでも機能しません。画面内にとどまるように制限するにはどうすればよいですか?