마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

숫자 맞추기 질문 입니다.

0 추천
package com.example.project;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
 TextView text;
  Integer result;
  int count=2;
  String num;
  int toint;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
  
   Button btn1=(Button)findViewById(R.id.button1);
   Button btn2=(Button)findViewById(R.id.button2);
   Button btn3=(Button)findViewById(R.id.button3);
   Button btn4=(Button)findViewById(R.id.button4);
   Button btn5=(Button)findViewById(R.id.button5);
   Button btn6=(Button)findViewById(R.id.button6);
   Button btn7=(Button)findViewById(R.id.button7);
   Button btn8=(Button)findViewById(R.id.button8);
   Button btn9=(Button)findViewById(R.id.button9);
       
text=(TextView)findViewById(R.id.TextView1);

  num=text.getText().toString();
  toint= Integer.parseInt(num);
   View.OnClickListener a;
  

   a=new View.OnClickListener() {  //1 버튼
 public void onClick(View v) {
  if(toint==1){
  result=(int)(Math.random()*9)+1;
  text.setText("result = "+result);
  }
 }
};btn1.setOnClickListener(a);

a=new View.OnClickListener() {  //2버튼
 public void onClick(View v) {
  if(toint==2){
  result=(int)(Math.random()*9)+1;
  text.setText("result = "+result);
  }
 }
};btn2.setOnClickListener(a);

a=new View.OnClickListener() {  //3버튼
 public void onClick(View v) {
  if(toint==3){
  result=(int)(Math.random()*9)+1;
  text.setText("result = "+result);
  }
 }
};btn3.setOnClickListener(a);

a=new View.OnClickListener() {  //4버튼
 public void onClick(View v) {
  if(toint==4){
  result=(int)(Math.random()*9)+1;
  text.setText("result = "+result);
  }
 }
};btn4.setOnClickListener(a);

a=new View.OnClickListener() {  //5 버튼
 public void onClick(View v) {
  if(toint==5){
  result=(int)(Math.random()*9)+1;
  text.setText("result = "+result);
  }
 }
};btn5.setOnClickListener(a);

a=new View.OnClickListener() {  //6 버튼
 public void onClick(View v) {
  if(toint==6){
  result=(int)(Math.random()*9)+1;
  text.setText("result = "+result);
  }
 }
};btn6.setOnClickListener(a);

a=new View.OnClickListener() {  //7 버튼
 public void onClick(View v) {
  if(toint==7){
  result=(int)(Math.random()*9)+1;
  text.setText("result = "+result);
  }
 }
};btn7.setOnClickListener(a);

a=new View.OnClickListener() {  //8 버튼
 public void onClick(View v) {
  if(toint==8){
  result=(int)(Math.random()*9)+1;
  text.setText("result = "+result);
  }
 }
};btn8.setOnClickListener(a);

a=new View.OnClickListener() {  //9 버튼
 public void onClick(View v) {
  if(toint==9){
  result=(int)(Math.random()*9)+1;
  text.setText("result = "+result);
  }
 }
};btn9.setOnClickListener(a);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
   
}

우선 질문에 앞서 정말 이번에 이렇게 좋은 사이트를 알게되서 매우 기쁘고 활발히 활동하겠습니다.

본론으로 가장 처음에 num으로 문자를 String으로 가져온후에 toint로 문자를 숫자로 바꿔 가져왔습니다.

이후에 1번째 버튼을 누를 경우 toint와 같을 때에만 text의숫자를 랜덤으로set 해주었습니다.

각 버튼의 숫자에 맞을 경우에만 숫자를 랜덤으로 표현해주고 싶은데 실행할 경우

1번버튼에서 1이 아닐경우에도 랜덤으로 바뀌네요. 또 나머지 버튼은 이벤트도 발생하지 않는데

어떤점이 문제 인가요???

 

또 엉뚱한 질문일지 모르겠지만

for(int a=1;a<=9;a++){

Button btn[i]=(Button)findViewById(R.id.Btn[i]);

}

와같이 버튼선언해줄수는 없을까요??

깨끗하지 않은 글이지만 좋은 답변 해주시면 감사하겠습니다! ^^
얼상 (430 포인트) 님이 2013년 6월 6일 질문

2개의 답변

+1 추천
 
채택된 답변

안녕하세요

1이아닐때도 바뀐다는건 조건문이 문제가 있을테고 이벤트가 안먹는다는건 리스너가 잘못되었겠네요;;;

디버그나 로그를 찍어보세요...

 

이벤트가 많을때는 

public void onClick(View v)
{
	switch (v.getId())
	{
		case R.id.1:
				
		break;

		case R.id.2:
				
		break;

		case R.id.3:
				
		break;
	}
}

한눈에 들어와서 전 이렇게 씁니다

그리고 버튼 선언같은경우는 

view = new View[]
{ findViewById(R.id.1), findViewById(R.id.search_ch1), findViewById(R.id.ok),
findViewById(R.id.photo), findViewById(R.id.se123), findViewById(R.id.ppp) };

btn = new Button[view.length];
for (int i = 0; i < btn.length; i++)
{
	btn[i] = (Button) view[i];
}

요론식으로도 활용할수가 있겠네요.

  (11,920 포인트) 님이 2013년 6월 6일 답변
얼상님이 2013년 6월 6일 채택됨
정말 감사합니다 ^^ 많은 도움이 됬습니다 !!
0 추천
엉뚱한 질문이 아닙니다 버튼 역시 루프문으로 생성해서 사용 할 수 있습니다.

 

컴포넌트의 id를 리니어하게 작성을 하시면 R.java 파일에 일련된 int 값으로 생성이 됩니다.

이를 이용해서

for(counter 0 ~ max){

       Button btn[counter] = (Button)findViewById(R.id.btnid00 + counter);

}

이런식으로 가져와 사용 할 수 있습니다.

컴포넌트 뿐만 아니라 리소스도 이런식으로 쓸 수 있구요
Noizbuster (11,970 포인트) 님이 2013년 6월 6일 답변
아... 그렇군요 정말 감사합니다 ^^
...