안드로이드 프로그래밍
[안드로이드 프로그래밍] 2장 직접 풀어보기 2-3
Excellent Summer
2021. 7. 13. 03:37
[ 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
36
37
38
|
<?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"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/btn_web"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="네이트 홈페이지 열기"
android:textColor="@color/black" />
<Button
android:id="@+id/btn_call"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="911 응급전화 열기"
android:textColor="@color/black" />
<Button
android:id="@+id/btn_photo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="갤러리 열기"
android:textColor="@color/black" />
<Button
android:id="@+id/btn_exit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="끝내기"
android:textColor="@color/black" />
</LinearLayout>
|
cs |
[ MainActivity.java ]
- setContentview
res/layout 디렉토리에 있는 activity_main.xml를 활동 콘텐츠로 설정합니다. 리소스가 확장되어 활동에 모든 최상위 뷰가 추가됩니다.
- setBackgroundColor
뷰의 배경 색을 설정합니다.
- Intent
Intent는 메시징 객체로, 다른 앱 구성 요소로부터 작업을 요청하는 데 사용할 수 있습니다.
public class MainActivity extends AppCompatActivity {
Button btn_web, btn_call, btn_photo, btn_exit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// xml 파일에서 만튼 버튼 객체와 연결해준다.
btn_web = findViewById(R.id.btn_web);
btn_call = findViewById(R.id.btn_call);
btn_photo = findViewById(R.id.btn_photo);
btn_exit = findViewById(R.id.btn_exit);
// 버튼에 색상을 지정해준다.
btn_web.setBackgroundColor(Color.GRAY);
btn_call.setBackgroundColor(Color.GREEN);
btn_photo.setBackgroundColor(Color.RED);
btn_exit.setBackgroundColor(Color.YELLOW);
// 클릭시 네이트 홈페이지가 열린다
btn_web.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent mIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://m.nate.com"));
startActivity(mIntent);
}
});
// 911 입력된 상태로 전화 어플이 열린다.
btn_call.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent mIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("tel:/911"));
startActivity(mIntent);
}
});
// 갤러리가 열린다.
btn_photo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent mIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("content://media/internal/images/media"));
startActivity(mIntent);
}
});
// 앱을 종료한다.
btn_exit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
}
❗ 틀린 내용이나 오류가 있으면 덧글 남겨주시면 감사하겠습니다:) ❗