- TextView와 Button 추가 - Button클릭시 TextView에 난수 표시 |
1 2 3 4 | < textview android:id = "@+id/textView1" android:text = "@string/hello_world" > </ textview > < button android:id = "@+id/button1" android:text = "Button" android:onclick = "onClick" > </ button > <!-- android:onclick="onClick" 버튼 클릭시 실행되는 함수 --> |
1 2 3 4 5 6 7 8 9 | protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); txtView =(TextView)findViewById(R.id.textView1); btn_start = (Button)findViewById(R.id.button1); rndNum = new Random(); //난수 사용을 위한 객체 생성 } |
1 2 3 4 5 6 | public void onClick(View view) { int imsi = rndNum.nextInt( 10 ) + 1 ; //범위 1부터 10까지의 난수 생성 txtView.setText( "random data: " + imsi); } |