278
No
MainActivity
package com.example.choi.ex01;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
//Activitiy : 화면 처리 클래스
public class MainActivity extends AppCompatActivity {
//화면을 생성
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//버튼을 누르면 실행되는 method
//android:onclick="onClick"
// View v =new Button(...);
public void onClick(View v){
//new Intent(현재 클래스 .this, 다음 화면 클래스 .class)
Intent intent=null;
switch(v.getId()){
case R.id.button1:
intent =new Intent(this, ImgeViewDemo.class);
break;
case R.id.button2:
intent =new Intent(this, Exchange.class);
break;
case R.id.button3:
intent=new Intent(this, BmiActivity.class);
break;
case R.id.button4:
intent=new Intent(this, CheckboxDemo.class);
break;
case R.id.button5:
intent =new Intent(this, RadioDemo.class);
break;
case R.id.button6:
intent=new Intent(this, YutActivity.class);
break;
}
startActivity(intent);
}
@Override
protected void onStart() {
super.onStart();
}
@Override
protected void onResume() {
super.onResume();
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.choi.ex01.MainActivity">
<!--onClick ="버튼을 클릭했을 때 실행할 함수 이름-->
<Button
android:text="이미지뷰"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button1"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:onClick="onClick" />
<Button
android:text="환율계산"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="11dp"
android:onClick="onClick"
android:id="@+id/button2"
android:layout_below="@+id/button1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:text="비만도 측정"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="13dp"
android:id="@+id/button3"
android:onClick="onClick"
android:layout_below="@+id/button2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:text="체크박스"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="22dp"
android:id="@+id/button4"
android:layout_below="@+id/button3"
android:layout_centerHorizontal="true"
android:onClick="onClick"
/>
<Button
android:text="라디오버튼"
android:onClick="onClick"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/button4"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:id="@+id/button5" />
<Button
android:text="윷던지기"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/button5"
android:layout_centerHorizontal="true"
android:layout_marginTop="28dp"
android:id="@+id/button6"
android:onClick="onClick"
/>
</RelativeLayout>
YutActivity
package com.example.choi.ex01;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.Random;
public class YutActivity extends AppCompatActivity {
//변수 선언
//res/drawable 에 저장된 이미지 숫자아이디 배열
int[] imgYut ={R.drawable.b1, R.drawable.b3} ;//뒤는 0, 앞은 1
String[] strYut={"윷", "걸", "개", "도", "모"};
//윷 0, 걸 1, 개, 2 도:3 모 :4
//앞 4개면 도
Button button1;
ImageView img1, img2, img3, img4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.yut);
button1=(Button)findViewById(R.id.button1);
//버튼 클릭 이벤트 처리
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Random rand =new Random(); //랜덤 객체 생성
int n1=rand.nextInt(2);
int n2=rand.nextInt(2);
int n3=rand.nextInt(2);
int n4=rand.nextInt(2);
int sum=n1+n2+n3+n4;
img1=(ImageView)findViewById(R.id.img1);
img2=(ImageView)findViewById(R.id.img2);
img3=(ImageView)findViewById(R.id.img3);
img4=(ImageView)findViewById(R.id.img4);
TextView txtResult=(TextView)findViewById(R.id.txtResult);
//setImageResoruce(리소스의 id ) : 이미지뷰의 이미지 설정
// imgYut[0] R.drawable.b1;
// imgYut[0] R.drawable.b1;
img1.setImageResource(imgYut[n1]);
img2.setImageResource(imgYut[n2]);
img3.setImageResource(imgYut[n3]);
img4.setImageResource(imgYut[n4]);
txtResult.setText(strYut[sum]);
}
});
}
}
YUT.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/b3"
android:id="@+id/img1"
android:layout_weight="1" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/b3"
android:id="@+id/img2"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/b1"
android:id="@+id/img3"
android:layout_weight="1" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/b1"
android:textAlignment="center"
android:id="@+id/img4"
android:layout_weight="1" />
</LinearLayout>
<Button
android:text="윷던지기"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button1"
android:background="@android:drawable/button_onoff_indicator_on" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/txtResult"
android:textAlignment="center"
android:textSize="50sp"
android:textColor="@android:color/holo_blue_light"
/>
</LinearLayout>
댓글 ( 4)
댓글 남기기