버튼 button1
텍스트뷰 txtResult
* 이벤트 처리 방법
1. 위젯 변수 선언
2. 위젯 객체 생성 ( R.java 에서 아이디 참조)
findViewById(리소스아이디)
3. 이벤트 처리
뷰.setOn 이벤트 Listener(이벤트 핸들러 구현 );
화면전화
스택처럼 쌓인다.
BmiResultActivity =>back => 스택에서 제거(pop)
BmiActivity
MainActivity
* 액티비티의 라이프 사이클
onCreate() 화면 생성 (스택에 push)
onResume() 화면 다시 그릴 때
onPause() 화면이 멈출 때
onDestroy() 화면을 제거(스택에서 pop)
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;
}
startActivity(intent);
}
@Override
protected void onStart() {
super.onStart();
}
@Override
protected void onResume() {
super.onResume();
}
}
activity_main
<?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_below="@+id/button1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="29dp"
android:onClick="onClick"
android:id="@+id/button2" />
<Button
android:text="비만도 측정"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="139dp"
android:id="@+id/button3"
android:layout_below="@+id/button2"
android:onClick="onClick"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
BmiActivity
package com.example.choi.ex01;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class BmiActivity extends AppCompatActivity {
//1.위젯 변수 선언
EditText editName, editAge, editHeight, editWeight;
Button button1;
TextView txtResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bmi);
//2. 위젯 생성 (R.java 에서 아이디 참조)
editName=(EditText)findViewById(R.id.editName);
editAge=(EditText)findViewById(R.id.editAge);
editHeight=(EditText)findViewById(R.id.editHight);
editWeight=(EditText)findViewById(R.id.editWeight);
button1=(Button)findViewById(R.id.button1);
txtResult=(TextView)findViewById(R.id.txtResult);
//3.이벤트 처리
//뷰.이벤트리스너(이벤트핸들러)
button1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
//화면전화
//new Intent(현재화면.this, 다음화면.class);
Intent intent=
new Intent(BmiActivity.this, BmiResultActivity.class);
//BMI =몸무게(kg) /(신장*신장)
double kg =Double.parseDouble(editWeight.getText().toString());
double height=Double.parseDouble(editHeight.getText().toString());
double bmi=kg / ((height/100)* (height/100));
//18.5저체중, 18.5~23정상, 23~25 과체중
// 25 ~30비만, 30이상 고도비만
String result ="";
if(bmi < 18.5){
result="저체중";
}else if (bmi >=18.5 && bmi <23){
result="정상";
}else if( bmi >=23 && bmi <25){
result="과체중";
}else if(bmi >=25 && bmi < 30){
result ="비만";
}else if(bmi >=30){
result="고도비만";
}
//intent.putExtra("변수명", 값)
// intent.putExtra("name", editName.getText().toString());
// intent.putExtra("age", editAge.getText().toString());
// intent.putExtra("bmi",bmi);
// intent.putExtra("result", result);
BmiDTO dto =new BmiDTO();
dto.setAge(Integer.parseInt(editAge.getText().toString()));
dto.setName(editName.getText().toString());
dto.setBmi(bmi);
dto.setResult(result);
intent.putExtra("dto", dto);
startActivity(intent);
//txtResult.setText("bmi 지수 : " +bmi + " : " +result.toString());
}
});
}
@Override
protected void onResume() {
super.onResume();
editHeight.setText("");
editWeight.setText("");
editName.setText("");
editAge.setText("");
}
}
bmi
<?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/bmi"
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.BmiActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text=""
android:hint="이름을 입력하세요"
android:ems="10"
android:layout_marginTop="19dp"
android:id="@+id/editName"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:layout_below="@+id/editName"
android:layout_centerHorizontal="true"
android:layout_marginTop="33dp"
android:id="@+id/editAge"
android:hint="나이를 입력하세요" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:layout_below="@+id/editAge"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:id="@+id/editHight"
android:hint="키를 입력하세요" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:layout_marginTop="28dp"
android:id="@+id/editWeight"
android:hint="체중을 입력하세요"
android:layout_below="@+id/editHight"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:text="확인"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/editWeight"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:id="@+id/button1" />
<TextView
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/button1"
android:layout_centerHorizontal="true"
android:layout_marginTop="38dp"
android:id="@+id/txtResult" />
</RelativeLayout>
BmiResultActivity
package com.example.choi.ex01;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class BmiResultActivity extends AppCompatActivity {
//!위젯 변수 선언
TextView txtResult;
Button btnBack;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bmi_result);
//2.위젯 연결
txtResult=(TextView)findViewById(R.id.txtResult);
btnBack=(Button)findViewById(R.id.btnBack);
//3. intent.putExtra 로 넘어온 값 조회
//이전 화면에서 넘어온 인텐트 정보를 가져옴
Intent intent =getIntent();
//intent.getExtra("변수명") servelt request.getParameter 유사
//숫자 자료형인 경우 기본값이 필요함
/*
String name =intent.getStringExtra("name");
String age =intent.getStringExtra("age");
double bmi =intent.getDoubleExtra("bmi", 0);
String result=intent.getStringExtra("result");
txtResult.setText("name :" + name +"\n" +
"age : "+ age +"\n"+
"bmi :" +bmi+"\n"+
"result:"+result
);
*/
//객체 직렬화된 바이트배열을 읽어서 dto로 변환
BmiDTO dto=(BmiDTO)intent.getSerializableExtra("dto");
String name=dto.getName();
int age=dto.getAge();
double bmi=dto.getBmi();
String result =dto.getResult();
txtResult.setText("name :" + name +"\n" +
"age : "+ age +"\n"+
"bmi :" +bmi+"\n"+
"result:"+result
);
//4.버튼 클릭 이벤트;
btnBack.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
finish();//현재 화면을 종료
}
});
}
}
bmi_result
<?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/bmi_result"
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.BmiResultActivity">
<Button
android:text="뒤로가기"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/btnBack" />
<TextView
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/btnBack"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="75dp"
android:id="@+id/txtResult" />
</RelativeLayout>
BmiDTO
package com.example.choi.ex01;
import java.io.Serializable;
/**
* Created by choi on 2017-02-08.
*/
//intent 를 이용해서 객체를 넘길 경우 Seriablizable 을 구현해야 함
//객체 직렬화: 객체에 포함된 변수들의 자료형을 무시하고 바이트배열로
public class BmiDTO implements Serializable {
private String name;
private int age;
private double bmi;
private String result;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getBmi() {
return bmi;
}
public void setBmi(double bmi) {
this.bmi = bmi;
}
public String getResult() {
return result;
}
public void setResult(String result) {
this.result = result;
}
@Override
public String toString() {
return "BmiDTO{" +
"name='" + name + '\'' +
", age=" + age +
", bmi=" + bmi +
", result='" + result + '\'' +
'}';
}
}
댓글 ( 4)
댓글 남기기