IT/Android

Android 프로그래밍

바바옄 2015. 6. 15. 16:46
반응형

activity_main.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<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"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
 
    <TextView
        android:text="@string/hello_android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
 
        <!--
             android:layout_width : View의 너비
             android:layout_height : View의 높이
             android:layout_width, layout_height은 3가지 상수를 제공
             - wrap_content : view의 내용만큼만 차지한다.
             - match_parent : 부모 view의 크기만큼 채운다.
             - fill_parent : match_parent와 동일하지만, 더 이상 사용되지 않는다.
                            하위 호환성을 위해 아직 지원하는 중
         -->
 
</RelativeLayout>
 
cs

 

dimens.xml

1
2
3
4
5
6
7
8
9
10
<resources>
    <!-- Default screen margins, per the Android Design guidelines. -->
    <dimen name="activity_horizontal_margin">16dp</dimen>
    <dimen name="activity_vertical_margin">16dp</dimen>
    
    <!-- 
        사용자 지정 크기는 "dp" 단위를 사용해 표현한다.
        안드로이드 단말의 크기가 모두 달라 표현의 문제가 존재해, 비율 단위로 맞춰줌,
    -->
</resources>
cs

 

Button을 클릭할 때마다 Textview 바꾸기

1. xml에서 onClick 사용

activity_main.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<LinearLayout
    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"
    android:orientation="vertical"
    tools:context=".MainActivity">
 
    <TextView
        android:id="@+id/tvHello"
        android:text="@string/hello_world"
        android:layout_gravity="center"
        android:textSize="40dp"
        android:paddingTop="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
 
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="클릭하세요!"
        android:id="@+id/btnClickMe"
        android:layout_gravity="center_horizontal"
        android:elegantTextHeight="false"
        android:textSize="20dp"
        android:onClick="changeText" />
 
</LinearLayout>
 
cs

MainActivity.java 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package com.ktds.myfirstapplication2;
 
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.TextView;
 
 
public class MainActivity extends ActionBarActivity {
 
    private TextView tvHello;
    private int count;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
 
        // 인플레이팅 : xml을 메모리로 로딩시키는 역할
        // setContentView가 먼저 실행되지 않으면 에러난다.
        setContentView(R.layout.activity_main);
 
        tvHello = (TextView) findViewById(R.id.tvHello);
        // tvHello에 해당하는 아이디를 가진 view를 가져온다.
        
        // error!!
        // setContentView(R.layout.activity_main);
 
 
    }
 
    public void changeText(View v){
 
        tvHello.setText("클릭 되었습니다."+(++count));
    }
}
 
cs

 

2. Listener를 템플릿 콜백으로 만들어서

activity_main.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<LinearLayout
    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"
    android:orientation="vertical"
    tools:context=".MainActivity">
 
    <TextView
        android:id="@+id/tvHello"
        android:text="@string/hello_world"
        android:layout_gravity="center"
        android:textSize="40dp"
        android:paddingTop="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
 
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="클릭하세요!"
        android:id="@+id/btnClickMe"
        android:layout_gravity="center_horizontal"
        android:elegantTextHeight="false"
        android:textSize="20dp"
        />
 
</LinearLayout>
 
cs

MainActivity.java 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.ktds.myfirstapplication2;
 
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
 
 
public class MainActivity extends ActionBarActivity {
 
    private TextView tvHello;
    private int count;
    private Button btnClickMe;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
 
        // 인플레이팅 : xml을 메모리로 로딩시키는 역할
        // setContentView가 먼저 실행되지 않으면 에러난다.
        setContentView(R.layout.activity_main);
 
        tvHello = (TextView) findViewById(R.id.tvHello);
        // tvHello에 해당하는 아이디를 가진 view를 가져온다.
 
        // error!!
        // setContentView(R.layout.activity_main);
 
        btnClickMe = (Button) findViewById(R.id.btnClickMe);
 
        // View라는 클래스 안에 있는 OnClickListener 인터페이스
        btnClickMe.setOnClickListener(new View.OnClickListener() {
 
            // v 는 클릭이 된 놈
            @Override
            public void onClick(View v) {
                tvHello.setText("클릭 되었습니다!!"+(++count));
            }
        });
    }
 
 /*   // button에 있던 변수명
    public void changeText(View v){
        tvHello.setText("클릭 되었습니다."+(++count));
    }*/
}
 
cs

 

 

 

          

 

EditText p.112~

activity_main.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<LinearLayout
    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"
    android:orientation="vertical"
    tools:context=".MainActivity"
    >
 
    <!--
        @+id/etName : R.id라는 클래스에 etName 변수를 만들어라
    -->
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/etName"
        android:hint="이름을 입력하세요" />
 
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="이름을 보여주세요!"
        android:textSize="20dp"
        android:id="@+id/btnClickMe"
        android:layout_gravity="center_horizontal" />
 
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="이름을 보여줄게요!"
        android:textSize="20dp"
        android:id="@+id/tvShowName"
        android:layout_gravity="center_horizontal" />
</LinearLayout>
 
cs

 

MainActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.ktds.myedittext;
 
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
 
 
public class MainActivity extends ActionBarActivity implements View.OnClickListener{
 
    private EditText etName;
    private Button btnClickMe;
    private TextView tvShowName;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        etName = (EditText) findViewById(R.id.etName);
        btnClickMe = (Button) findViewById(R.id.btnClickMe);
        tvShowName = (TextView) findViewById(R.id.tvShowName);
 
        btnClickMe.setOnClickListener(this);
    }
 
 
    @Override
    public void onClick(View v) {
        // EditText에서 입력된 문자를 가져온다.
        String name = etName.getText().toString();
 
        // TextView의 변수 name의 값을 보여준다.
        tvShowName.setText(name);
    }
}
 
cs

 

           

 

 

 

 

반응형

'IT > Android' 카테고리의 다른 글

Widget - check box  (0) 2015.06.16
실습  (0) 2015.06.16
Widget - RadioButton  (0) 2015.06.16
Android Project 만들기  (0) 2015.06.15
ANDROID STUDIO 설치 및 설정  (0) 2015.06.15