Android - findViewById,xml에 String 적용

2020. 9. 20. 20:38Android

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">
 
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btn"
        android:text="@string/btn_name"/>
 
</LinearLayout>
//------------------------string.xmnl-----------------------
<resources>
    <string name="app_name">And0901_Ch02_BaseApp</string>
    <string name="btn_name">Button 입니다</string>
</resources>
 
//------------------------java code--------------------------
package com.example.and0901_ch02_baseapp;
 
import androidx.appcompat.app.AppCompatActivity;
 
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
 
public class MainActivity extends AppCompatActivity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        /*
        activity_main.xml 에서 생성한 위젯들의 ID를
        자바코드에서 가져와야만 해당 위젯에 접근 가능함
        별도로 자바코드에서 다시 개게 생성을 수행할 필요는 없음
        findViewById() 메서드를 호출하여 해당 위젯의 ID를 지정해야한다.
        이때, 파라미터로 전달할 ID를 R.Id.XXX형식으로 지정
        (XXX은 xml 파일에서 id 속성에 지정된 문자열)
        ID를 지정하여 가져온 위젯은 해당 클래스 타입 변수에 대입하여 참조변수를 통해 접근
         */
        //Button btn=new Button() 과 같이 객체생성 위치 주소값을 참조변수에 대입과 동일한 작업
        Button btn=findViewById(R.id.btn);
        // btn 이라는 이름의 id를 찾아서 Button 타입변수에 해당 객체 주소 대입
 
        // btn 변수를 사용하여 버튼객체에 접근 및 작업 수행 가능
        btn.setText("버튼입니다.");
        btn.setTextSize(30);
 
        //버튼 위젯을 클릭 시 동작 처리를 위한 리스너 연결
        //클릭 이벤트에 대한 리스너 :  OnClickListener 인터페이스
        //리스너 연결을 위해서는 setXXXListener() 메서드를 호출하여 리스너 객체 전달
 
//        btn.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                Toast.makeText(MainActivity.this, "버튼이 클릭됨", Toast.LENGTH_SHORT).show();
//            }
//        });
 
        btn.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                Toast.makeText(MainActivity.this"꾹눌림", Toast.LENGTH_SHORT).show();
                return false;
            }
        });
    }
}
cs

text를 파일 안에서 곧바로 String을 입력해도 되지만 안드로이드에서 외부에 xml 파일을 생성하여서 간접적으로 객체를 선언한 후 그것을 끌어다 써도 사용이 가능하다. 

LongClickListener는 위젯을 길게 눌렀을 경우 작동하게 하는 Listener이다.

'Android' 카테고리의 다른 글

Android - Intent  (0) 2020.09.20
Android - ClickListener  (0) 2020.09.20
Android - Rating Bar,Seek Bar  (0) 2020.09.20
Android - Basic  (0) 2020.09.20