Mobile/Android

#02 ( 시작하기 )

choi121xx 2013. 1. 28. 23:01

간단한 예제 > Edittext에 값을 입력하고, 버튼을 클릭하면 하단에 보여주기.

 

- 버튼 이벤트 주기

- Toast

- layout/activity_main.xml

- layout에 추가한 resource에 대한 참조값 확인 : R.java

 

확인할 소스파일위치> 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

(1) activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <EditText
        android:id="@+id/edtName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:ems="10" />

    <Button
        android:id="@+id/btn1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/edtName"
        android:text="Button" />

</RelativeLayout>

; EditText와  Button을 추가함.

; Graphical layout에서 추가해도 되나 직접 속성을 추가함. (속성을 이해하기 위해)

; 추가하면 Graphical layout 결과는 아래와 같다.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

(2) R.java

; 고치거나 추가할 것은 없다. 추가된 정보를 확인해 본다.

; 위에서 추가한 EditText나 Button의 참조대상 값이 보인다.

/* AUTO-GENERATED FILE.  DO NOT MODIFY.
 *
 * This class was automatically generated by the
 * aapt tool from the resource data it found.  It
 * should not be modified by hand.
 */

package com.example.test01;

public final class R {
    public static final class attr {
    }
    public static final class drawable {
        public static final int ic_launcher=0x7f020000;
    }
    public static final class id {
        public static final int btn1=0x7f070001;
        public static final int edtName=0x7f070000;
        public static final int menu_settings=0x7f070002;
    }
    public static final class layout {
        public static final int activity_main=0x7f030000;
    }
    public static final class menu {
        public static final int activity_main=0x7f060000;
    }
    public static final class string {
        public static final int app_name=0x7f040000;
        public static final int hello_world=0x7f040001;
        public static final int menu_settings=0x7f040002;
    }
    public static final class style {
        /**
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
   

            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.
       

        Base application theme for API 11+. This theme completely replaces
        AppBaseTheme from res/values/styles.xml on API 11+ devices.
   
 API 11 theme customizations can go here.

        Base application theme for API 14+. This theme completely replaces
        AppBaseTheme from BOTH res/values/styles.xml and
        res/values-v11/styles.xml on API 14+ devices.
   
 API 14 theme customizations can go here.
         */
        public static final int AppBaseTheme=0x7f050000;
        /**  Application theme.
 All customizations that are NOT specific to a particular API-level can go here.
         */
        public static final int AppTheme=0x7f050001;
    }
}

 

(3) MainActivity.java

; OnClickListener 리스너 구현- onClick추상메소드 override하기.

; R.java에서 확인한 object에 대한 참조값 얻어오기.

   findViewById메소드를 이용해서 버튼,에디트텍스트의 참조값을 얻어온다. 해당 object타입으로 형변환해야 한다.

; 버튼에 이벤트 리스너를 등록한다.

; out.println을 사용할수 없어서 Toast라는 객체를 사용해 확인해 본다. 하단에 메시지박스같은 것이 Toast실행결과이다.

; 에뮬레이터 실행시킨 후 프로젝트 선택 --> run as --> 에뮬레이터로 넘어간다.

 

package com.example.test01;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener{
    Button btn1;
    EditText edt1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
        //버튼에 대한 참조값 얻어오기.

        btn1 = (Button)findViewById(R.id.btn1);

 

        //에디트텍스트에 대한 참조값 얻어오기.
        edt1 = (EditText)findViewById(R.id.edtName);
       
        //버튼에 이벤트리스너 등록하기.

        btn1.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        // 에디트텍스트에서 입력된 값 얻어오기.
        String msg = edt1.getText().toString();
       
        Toast.makeText(this, "input msg:" + msg, Toast.LENGTH_LONG).show();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

 

 

test 일력 후 Button 클릭하면 하단에 input msg:test (toast) 가 출력된다.

이제부터 시작이다.