안드로이드 개발 질문/답변
(글 수 45,052)
안녕하세요!
요새 안드로이드를 배우고있는 초짜입니다.
코드를 짜다가 궁금한점이 생겨서 질문을 하게 되었습니다.
제가 텍스트뷰1에 text1.setText("100")
텍스트뷰2에는 text2.setText("200")
텍스트뷰3에는 text3.setText("300")
텍스트뷰4에는 text4.setText("400")
을 넣고 텍스트뷰 모두안에 잇는 값을 정수화해서더한후 다시 스트링화 하여 text5에 넣으려면
String str1=text1.getText().toString();
int a1=Integer.valueOf(str1);
String str2=text2.getText().toString();
int a2=Integer.valueOf(str2);
String str3=text3.getText().toString();
int a3=Integer.valueOf(str3);
String str4=text4.getText().toString();
int a4=Integer.valueOf(str4);
int a6=a1+a2+a3+a4;
String str5=String.valueOf(a6);
int a1=Integer.valueOf(str1);
String str2=text2.getText().toString();
int a2=Integer.valueOf(str2);
String str3=text3.getText().toString();
int a3=Integer.valueOf(str3);
String str4=text4.getText().toString();
int a4=Integer.valueOf(str4);
int a6=a1+a2+a3+a4;
String str5=String.valueOf(a6);
text5.setText(str5)
이러면 되는거 같은데
텍스트뷰가 몇개 있는지 모를때 어떻게 코드를 짜 넣어야 되는지 아무리 머리를 싸매도 잘 모르겠습니다 ㅜㅠㅜㅠ
고수분들 좀 도와주세요 ㅜㅠ
Arraylist 를 써서 for문을 돌리라고 하셔서 그렇게 해봤는데
제가 무지한지라 구글링 해보고 했는데도 코드가 짜지지가 않아서 ㅠㅜㅠ
저에게 구원의 손길을 내밀어 주십시옹 ㅜㅠ
TextView tvList[] = new TextView[tvNum] ;
tvList[0] = (TextView)findview( R.id.tv1 ) ;
tvList[1] = (TextView)findview( R.id.tv2 ) ;
tvList[2] = (TextView)findview( R.id.tv3) ;
tvList[3] = (TextView)findview( R.id.tv4 ) ;
for( ; ; )
{
... 텍스트 구해서 파싱해서 더해서 다시 넣는코드
}
/////////////////////
"패키지명 : 타입 / 리소스명", null, null
getIdentifier( "com.test:drawable/drawimage" + i, null, null ) ;
"리소스명", "타입", "패키지명"
getIdentifier( "drawimage" + i, "drawable", "com.test" ) ;
리소스 id값이 반드시 순서대로라고 정해진 법은 없기 때문에 위와같이 처리해야 한다.
Resources rc = getResources( ) ;
m_imgView.setImageDrawable( rc.getDrawable( rc.getIdentifier( "drawimage" + m_num , "drawable", "com.test" ) ) ) ;
//그런고로 텍스트뷰 얻는 작업은 아래와 같이 노가다를 피할 수 있습니다.
TextView tvList[] = new TextView[tvNum] ;
for( ; ; )
{
tvList[i] = rc.getIdentifier( "tv" + i , "id", "com.test" )
}
for( ; ; )
{
... 텍스트 구해서 파싱해서 더해서 다시 넣는코드
}
끗?