* R.java 리소스 저장 클래스
MainActivity.java
button1
button2
R.java(리소스의 주소값을 저장)
button1
button2
activity_main.xml
button1
button2
* R.java 를 보는 방법
Project Files로 변경
Show member 체크
app/build/generated/source/r/debug
/R.java 확인
* 환율계산
EditText :dollar
Button : button1
TextView : txtResult
* Container 와 Component
Container : 컴포넌트를 담을 수 있음
* Widget
view : 최상위 위젯
MainActivity.class
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;
}
startActivity(intent);
}
}
activity_main.xml
<?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" />
</RelativeLayout>
Exchange.class
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.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class Exchange extends AppCompatActivity {
//1.위젯 선언
EditText dollar;
Button button;
TextView txtResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.exchange);
//2.위젯 생성
//(형변환)findViewById(리소스 아이디)
dollar =(EditText)findViewById(R.id.dollar);
button=(Button)findViewById(R.id.button);
txtResult=(TextView)findViewById(R.id.txtResult);
//3.버튼 클릭 이벤트
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//EditText는 getText().toString() 을 해야 스트링으로 변환됨
// String.foramt("출력형식", 출력할값)
if(!dollar.getText().toString().equals("")){
// Toast.makeText(출력할 클래스.this, "메시지", 출력옵션).show()
Integer intDollar
=Integer.parseInt(dollar.getText().toString());
int money =intDollar * 1100;
txtResult.setText(intDollar+"달러="+String.format("%,d",money)+"원입니다.");
}else{
Toast.makeText(Exchange.this, "숫자를 입력하세요.", Toast.LENGTH_SHORT).show();
}
}
});
}
}
exchange.xml
<?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/exchange"
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.Exchange">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text=""
android:ems="10"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="44dp"
android:layout_marginStart="44dp"
android:id="@+id/dollar"
android:hint="달러를 입력하세요" />
<Button
android:text="환율계산"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/dollar"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="17dp"
android:layout_marginStart="17dp"
android:layout_marginTop="39dp"
android:id="@+id/button" />
<TextView
android:text=""
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button"
android:layout_alignLeft="@+id/button"
android:layout_alignStart="@+id/button"
android:layout_marginLeft="12dp"
android:layout_marginStart="12dp"
android:layout_marginTop="63dp"
android:id="@+id/txtResult" />
</RelativeLayout>
댓글 ( 4)
댓글 남기기