안드로이드 개발 질문/답변
(글 수 45,052)
package com.android.Test5;
import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView;
public class Test5 extends Activity {
//public double ran;
public static final int[] ran= {
R.drawable.snc00581, R.drawable.snc00582, R.drawable.snc00583,
R.drawable.snc00584, R.drawable.snc00585, R.drawable.snc00586,
R.drawable.snc00587, R.drawable.snc00588, R.drawable.snc00590, R.drawable.snc00560
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ran = Math.random();
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
TextView t = (TextView)findViewById(R.id.text);
t.setText("클릭됨~");
ImageView test = (ImageView)findViewById(R.id.imageView1);
test.setImageResource(??????????????);
//test.setImageResource((int)(System.currentTimeMillis()%10));
//array[i]++;
//double ran;
//ran = Math.random();
//System.out.println("ran : " + ran);
//System.out.println((int)(ran*6)+1);
}
});
}
}
버튼 클릭시, 이미지를 랜덤하게 출력하는방법중에.
math.Random 쓰라는 답변이 있더군요... 일딴 예제를 찾아서 한번 대입시켜봣는데...흠...몰겠군요;;;
test.setImageResource(??????????????); 요 부분에 어떻게 대입시켜야 할지;;;
아님 Random random = new Random(); 이방법도 있다던데...이건 뭐 예제를 봐도 이해가 안되더군요;;;
결론은 이미지 랜덤하게 출력하는 방법쩜요...
2011.08.08 23:28:29
위 방법으로하셔도 되고 Math.random을 쓰시려면
int index = Math.random() * 10;
int res = ran[index];
test.setImageResource(res);
하면될 것 같네요. ㅎ 폰으로 코딩할려니 힘드네ㅡㅡ
int index = Math.random() * 10;
int res = ran[index];
test.setImageResource(res);
하면될 것 같네요. ㅎ 폰으로 코딩할려니 힘드네ㅡㅡ
2011.08.09 09:48:38
package com.android.Test5;
import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView;
public class Test5 extends Activity {
int index = (int) (Math.random() * 10);
int res = ran[index];
public static final int ran[]= {
R.drawable.snc00581, R.drawable.snc00582, R.drawable.snc00583,
R.drawable.snc00584, R.drawable.snc00585, R.drawable.snc00586,
R.drawable.snc00587, R.drawable.snc00588, R.drawable.snc00590, R.drawable.snc00560
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
TextView t = (TextView)findViewById(R.id.text);
t.setText("클릭됨~");
ImageView test = (ImageView)findViewById(R.id.imageView1);
test.setImageResource(res);
}
});
}
}
이렇게 하니까 이미지가 배열에서 7번째 사진만 나오는데요;;;




Random random = new Random();
random.nextInt(10); 을 하면 0~9까지 중에 랜덤으로 나온답니다.