안드로이드 개발 질문/답변
(글 수 45,052)
버튼을 눌러서 다른 액티비티로 이동할 때
지정된 액티비티 말고 여러가지 액티비티 중 랜덤으로 하나의 액티비티로 이동하게 하려면
어떤 방법을 써야하나요?
2011.09.01 23:51:47
괄호를 쳐보세요. (int)(Math.random()*3)
Math.random이 [0, 1)의 값을 생성하는데 int로 바꿔버리면 무조건 0이 되겠죠.
2011.09.05 10:18:21
답변감사합니다!
알려주신대로 해보니깐 랜덤이잘되네요^^
근데 뒤로갔다가 다시해보고 하면 프로그램이 오류가나네요ㅠㅠ
밑에 소스인데 어디를 수정해야 계속 눌러도 오류가 안날까요ㅠㅠ
Class부분이 주황색으로떳구요ㅠㅠ
public void init(){
btnNotice = (Button)act1.findViewById(R.id.today_button);
btnNotice.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v) {
final Class[] cls = {A.class, B.class, C.class};
Intent intent = new Intent(act, cls[(int)(Math.random()*3)]);
act.startActivity(intent);
}
});




final class[] cls = {A.class, B.calss, C.class}; //이동할 클래스 배열
Intent i = new Intent(Main.this, cls[(int)Math.rendom()*3]);
startActivity(i);
이런씩으로 하시면 되어요.