1 Application Name: Day10_yhg 예제 10-9 activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_weight="3" > android:id="@+id/iv1" android:src="@drawable/pic1" /> android:id="@+id/iv2" android:src="@drawable/pic2" /> android:id="@+id/iv3" android:src="@drawable/pic3" /> <LinearLayout android:layout_weight="3" > android:id="@+id/iv4" android:src="@drawable/pic4" /> android:id="@+id/iv5" android:src="@drawable/pic5" /> android:id="@+id/iv6" android:src="@drawable/pic6" />
2 Application Name: Day10_yhg <LinearLayout android:layout_weight="3" > android:id="@+id/iv7" android:src="@drawable/pic7" /> android:id="@+id/iv8" android:src="@drawable/pic8" /> android:id="@+id/iv9" android:src="@drawable/pic9" /> android:id="@+id/btnresult" android:text=" 투표종료 " />
3 Application Name: Day10_yhg 예제 10-10 result.xml <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="match_parent" android:gravity="center_vertical" android:stretchcolumns="0" > android:id="@+id/tv1" android:text=" 그림 1" android:id="@+id/rbar1" android:id="@+id/tv2" android:text=" 그림 2" android:id="@+id/rbar2" android:id="@+id/tv3" android:text=" 그림 3" android:id="@+id/rbar3"
4 Application Name: Day10_yhg android:id="@+id/tv4" android:text=" 그림 4" android:id="@+id/rbar4" android:id="@+id/tv5" android:text=" 그림 5" android:id="@+id/rbar5" android:id="@+id/tv6" android:text=" 그림 6" android:id="@+id/rbar6" android:id="@+id/tv7" android:text=" 그림 7" android:id="@+id/rbar7"
5 Application Name: Day10_yhg android:id="@+id/tv8" android:text=" 그림 8" android:id="@+id/rbar8" android:id="@+id/tv9" android:text=" 그림 9" android:id="@+id/rbar9" android:id="@+id/btnreturn" android:layout_span="2" android:text=" 돌아가기 " /> </TableLayout>
6 Application Name: Day10_yhg 예제 10-11,12,13 메인액티비티 (MainActivity.java) 코드 import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.widget.imageview; public class MainActivity extends AppCompatActivity { @Override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); settitle(" 각자학번이름 "); // 1. 그림을클릭할때마다투표수를저장할 9 개짜리배열을선언하고 0 으로초기화한다. final int votecount[] = new int[9]; for (int i = 0; i < 9; i++) votecount[i] = 0; // 2. 9 개의이미지버튼객체배열 ImageView image[] = new ImageView[9]; // 3. 9 개의이미지버튼 ID 배열 Integer imageid[] = { R.id.iv1, R.id.iv2, R.id.iv3, R.id.iv4, R.id.iv5, R.id.iv6, R.id.iv7, R.id.iv8, R.id.iv9 ; // 4. 9 개그림이름배열 final String imgname[] = { " 독서하는소녀 ", " 꽃장식모자소녀 ", " 부채를든소녀 ", " 이레느깡단베르양 ", " 잠자는소녀 ", " 테라스의두자매 ", " 피아노레슨 ", " 피아노앞의소녀들 ", " 해변에서 " ; for (int i = 0; i < imageid.length; i++) { final int index; // 주의! 꼭필요함.. index = i; image[index] = (ImageView) findviewbyid(imageid[index]); image[index].setonclicklistener(new View.OnClickListener() { // TODO Auto-generated method stub // 투표수증가. votecount[index]++; Toast.makeText(getApplicationContext(), imgname[index] + ": 총 " + votecount[index] + " 표 ", Toast.LENGTH_SHORT).show(); ); Button btnfinish = (Button) findviewbyid(r.id.btnresult); btnfinish.setonclicklistener(new View.OnClickListener() { Intent intent = new Intent(getApplicationContext(), ResultActivity.class); /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// intent.putextra("votecount", votecount); intent.putextra("imagename", imgname); ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// startactivity(intent); );
7 Application Name: Day10_yhg 예제 10-14 서브액티비티 (ResultActivity.java) 코드 import android.support.v7.app.appcompatactivity; import android.content.intent; import android.os.bundle; public class ResultActivity extends AppCompatActivity { @Override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.result); settitle(" 각자학번이름 "); // 앞화면에서보낸투표결과값을받는다. Intent intent = getintent(); int[] voteresult = intent.getintarrayextra("votecount"); String[] imagename = intent.getstringarrayextra("imagename"); // 9 개의 TextView, RatingBar 객체배열 TextView tv[] = new TextView[imageName.length]; RatingBar rbar[] = new RatingBar[imageName.length]; // 9 개의 TextView, RatingBar ID 배열 Integer tvid[] = { R.id.tv1, R.id.tv2, R.id.tv3, R.id.tv4, R.id.tv5, R.id.tv6, R.id.tv7, R.id.tv8, R.id.tv9 ; Integer rbarid[] = { R.id.rbar1, R.id.rbar2, R.id.rbar3, R.id.rbar4, R.id.rbar5, R.id.rbar6, R.id.rbar7, R.id.rbar8, R.id.rbar9 ; // TextView, RatingBar 개체찾기. for (int i = 0; i < voteresult.length; i++) { tv[i] = (TextView) findviewbyid(tvid[i]); rbar[i] = (RatingBar) findviewbyid(rbarid[i]); // 각 TextVeiw 및 RatingBar 에넘겨받은값을반영. for (int i = 0; i < voteresult.length; i++) { tv[i].settext(imagename[i]); rbar[i].setrating((float) voteresult[i]); Button btnreturn = (Button) findviewbyid(r.id.btnreturn); btnreturn.setonclicklistener(new View.OnClickListener() { finish(); );
8 Application Name: Day10_yhg <LinearLayout android:layout_weight="3" > android:id="@+id/iv7" android:src="@drawable/pic7" /> android:id="@+id/iv8" android:src="@drawable/pic8" /> android:id="@+id/iv9" android:src="@drawable/pic9" /> android:id="@+id/btnresult" android:text=" 투표종료 " />
9 Application Name: Day10_yhg <LinearLayout android:layout_weight="3" > android:id="@+id/iv4" android:src="@drawable/pic4" /> android:id="@+id/iv5" android:src="@drawable/pic5" /> android:id="@+id/iv6" android:src="@drawable/pic6" />
10 Application Name: Day10_yhg 예제 10-20 암시적인텐트예제 xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_height="match_parent" android:orientation="vertical" > android:id="@+id/btndial" android:text=" 전화걸기 " /> android:id="@+id/btnweb" android:text=" 홈페이지열기 " /> android:id="@+id/btngoogle" android:text=" 구글맵열기 " /> android:id="@+id/btnsearch" android:text=" 구글검색하기 " /> android:id="@+id/btnsms" android:text=" 문자보내기 " /> android:id="@+id/btnphoto" android:text=" 사진보기 " />
11 Application Name: Day10_yhg 예제 10-21 암시적인텐트예제 java 코드 import java.io.file; import android.manifest; import android.os.environment; import android.support.v4.app.activitycompat; import android.support.v7.app.appcompatactivity; import android.app.searchmanager; import android.content.intent; import android.net.uri; import android.os.bundle; import android.view.view; import android.widget.button; public class MainActivity extends AppCompatActivity { @Override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); settitle(" 학번이름 "); Button btndial = (Button) findviewbyid(r.id.btndial); Button btnweb = (Button) findviewbyid(r.id.btnweb); Button btngoogle = (Button) findviewbyid(r.id.btngoogle); Button btnsearch = (Button) findviewbyid(r.id.btnsearch); Button btnsms = (Button) findviewbyid(r.id.btnsms); Button btnphoto = (Button) findviewbyid(r.id.btnphoto); btndial.setonclicklistener(new View.OnClickListener() { Uri uri = Uri.parse("tel:010-1234-5678"); Intent intent = new Intent(Intent.ACTION_DIAL, uri); startactivity(intent); ); btnweb.setonclicklistener(new View.OnClickListener() { Uri uri = Uri.parse("http://www.hanb.co.kr"); Intent intent = new Intent(Intent.ACTION_VIEW, uri); startactivity(intent); );
12 Application Name: Day10_yhg btngoogle.setonclicklistener(new View.OnClickListener() { Uri uri = Uri.parse("http://maps.google.com/maps?q=" + 37.554264 + "," + 126.913598); Intent intent = new Intent(Intent.ACTION_VIEW, uri); startactivity(intent); ); btnsearch.setonclicklistener(new View.OnClickListener() { Intent intent = new Intent(Intent.ACTION_WEB_SEARCH); intent.putextra(searchmanager.query, " 안드로이드 "); startactivity(intent); ); btnsms.setonclicklistener(new View.OnClickListener() { Intent intent = new Intent(Intent.ACTION_SENDTO); intent.putextra("sms_body", " 안녕하세요 "); intent.setdata(uri.parse("smsto:" + Uri.encode("010-1234-4567"))); startactivity(intent); );