일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- 제이쿼리연결
- SwiftUI 기반의 iOS 프로그래밍
- 한글입력 안됨
- max MySQL
- 기획자랑 사이좋게 지내고 싶다
- 자바스크립트
- python GUI 사용하기
- 배열 분해 할당
- 황반석 옮김
- 맥 mysql
- 생활코딩
- 맥 아파치
- 한글입력 오류
- jquery 사용하기
- 자바스크립트 객체 만들기
- 한글입력 잘림
- 자바스크립트 배열 할당
- 제이펍 출판
- 닐 스미스 지음
- 객체지향
- 핵심만 골라 배우는 SwiftUI 기반의 iOS 프로그래밍
- 블록 스코프
- max apache
- 한글입력 씹힘
- python tkinter 인터페이스
- 한글잘림
- 자바스크립트 객체
- 자바스크립트 class
- 객체
- 비주얼스튜디오 코드
- Today
- Total
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- 제이쿼리연결
- SwiftUI 기반의 iOS 프로그래밍
- 한글입력 안됨
- max MySQL
- 기획자랑 사이좋게 지내고 싶다
- 자바스크립트
- python GUI 사용하기
- 배열 분해 할당
- 황반석 옮김
- 맥 mysql
- 생활코딩
- 맥 아파치
- 한글입력 오류
- jquery 사용하기
- 자바스크립트 객체 만들기
- 한글입력 잘림
- 자바스크립트 배열 할당
- 제이펍 출판
- 닐 스미스 지음
- 객체지향
- 핵심만 골라 배우는 SwiftUI 기반의 iOS 프로그래밍
- 블록 스코프
- max apache
- 한글입력 씹힘
- python tkinter 인터페이스
- 한글잘림
- 자바스크립트 객체
- 자바스크립트 class
- 객체
- 비주얼스튜디오 코드
- Today
- Total
java,javascript,android,php,sql,공부용,메모용
안드로이드 Toast 토스트 / Android Studio / 안드로이드 앱만들기 공부 15 본문
Android Toast 토스트
앞으로는 공식 가이드 문서도 자세히 봐야겠다 ㅠ
책에서 설명하는 모든 내용의 코드가 작성되어있는 기분이다..
developer.android.com/guide/topics/ui/notifiers/toasts?hl=ko#java
토스트 메시지 개요 | Android 개발자 | Android Developers
토스트 메시지는 작은 팝업으로 작업에 관한 간단한 피드백을 제공합니다. 메시지에 필요한 공간만 차지하며 진행 중인 활동은 그대로 표시되고 상호작용도 유지됩니다. 토스트 메시지는 시간
developer.android.com
- 토스트는 간단한 메시지를 잠깐 보여주는 뷰다
- 대화상자와 함께 사용자에게 필요한 정보를 알려주는 대표 위젯
- 토스트는 포커스를 받지 않는다
- 디버깅 목적으로도 사용한다
- 디버깅은 Log 클래스를 이용하여 따로 Logcat 창에서 확인 할 수 있다.
- 앱이 화면에서 사라져도 메시지는 그대로 표시되기 떄문에 앱의 상태와 관계없이 보여줄 수 있다.
[code]
Toast.makeText(ConText context, String message, int duration).show();
Context 객체
- Context 클래스를 상속한 액티비티를 사용할 수 있다.
- 액티비티를 참조할 수 없는 경우 getApplicationContext() 메서드를 호출하면 Context 객체가 반환됩니다.
토스트 띄우기
- 보여주려는 메시지와 디스플레이 시간을 파라미터로 전달해 객체를 생성한 후 show() 메서드를 호출한다.
[code]
public void setGravity(int gravity, int xOffset, int yOffset)
public void setMargin(float horizontalMargin, float verticalMargin)
토스트는 위치나 모양을 바꿀 수 있다.
- setGravity() : 토스트의 뷰가 보이는 위치를 지정한다
int gravity 값은 Gravity.CENTER 와 같이 정렬 위치를 지정함
- setMargin() : 외부 여백을 지정하는 것 이 메서드를 이용해서 중앙이나 우측 하단에 배치가 가능하다.
Toast 위치 바꿔보기
1. SampleToast 프로젝트를 생성한다
2. activity_main.xml 파일 최상위 레이아웃을 리니어 레이어웃으로 변경
3. 리니어레이아웃을 추가하여 두개의 에디트와 하나의 버튼 배치
에디트텍스트에 inputType 속성을 numberSigned (양수로 된 숫자만 입력이 되도록함)
버튼에 onButton1Clicked 메서드가 호출되도록 onClick 속성을 설정
<?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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<EditText
android:id="@+id/editText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="X 위치"
android:inputType="numberSigned"
android:text="" />
<EditText
android:id="@+id/editText2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Y 위치"
android:inputType="numberSigned"
android:text="" />
<Button
android:id="@+id/button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="띄우기"
android:onClick="onButton1Clicked"/>
</LinearLayout>
</LinearLayout>
4. MainActivity.java 작성
package com.togapp.sampletoast;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText editText;
EditText editText2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = findViewById(R.id.editText);
editText2 = findViewById(R.id.editText2);
}
public void onButton1Clicked (View v) {
try {
Toast toastView = Toast.makeText(this, "위치가 바뀐 토스트 메시지입니다.", Toast.LENGTH_LONG);
// 두개의 editText를 xOffset과 yOffset으로 설정
// 숫자형의 문자를 받으면 값을 10진수의 integer 값으로 변경해줌 (Integer.parseInt로 정수만 입력이 되도록함)
int xOffset = Integer.parseInt(editText.getText().toString());
int yOffset = Integer.parseInt(editText2.getText().toString());
toastView.setGravity(Gravity.TOP|Gravity.TOP, xOffset, yOffset);
toastView.show();
} catch (NumberFormatException e) {
e.printStackTrace();
}
}
}
결과
MainActivity.java 에서
Gravity.CENTER로 설정하면 가운데를 기준으로 X와 Y의 값을 이용하여 조정하게 된다.
Toast 모양 바꿔보기
1. SampleToast 프로젝트의 activity_main.xml에 버튼을 추가한다. (모양 바꿔 띄우기)
2. android:onClick="onButton2Clicked" 추가
<?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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/editText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="X 위치"
android:inputType="numberSigned"
android:text=""
android:textSize="20sp"/>
<EditText
android:id="@+id/editText2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Y 위치"
android:inputType="numberSigned"
android:text=""
android:textSize="20sp"/>
<Button
android:id="@+id/button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onButton1Clicked"
android:text="띄우기"
android:textSize="20sp"/>
</LinearLayout>
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="모양 바꿔보여주기"
android:textSize="20sp"
android:onClick="onButton2Clicked"/>
</LinearLayout>
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="모양 바꿔보여주기"
android:textSize="20sp"
android:onClick="onButton2Clicked"/>
3. MainActivity.java
public void onButton2Clicked (View v) {
// 레이아웃 인플레이터 객체 참조
LayoutInflater inflater = getLayoutInflater();
// 토스트를 위한 레이아웃 인플레이션
View layout = inflater.inflate(
R.layout.toastborder,
(ViewGroup) findViewById(R.id.toast_layout_root));
TextView text = layout.findViewById(R.id.text);
// 토스트 객체 생성
Toast toast = new Toast(this);
text.setText("모양 바꾼 토스트");
toast.setGravity(Gravity.CENTER, 0, -100);
toast.setDuration(Toast.LENGTH_SHORT);
// 토스트가 보이는 뷰 설정
toast.setView(layout);
toast.show();
}
/res/layout/toastborder.xml 생성
toastborder를 만들고toast_layout_root를 설정해야한다.
그리고 layout은 toastvorder.xml의 toast_layout_root 안에 text를 만들어서 text도 불러온다.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:id="@+id/toast_layout_root"
android:padding="10dp">
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="20dp"
android:textSize="32sp"
android:background="@drawable/toast" />
</LinearLayout>
@drawable/toast ---> toast.xml 파일이 없으니 생성해준다
shape는 색상과 그라데이션을 포함한 도형 모양을 지정할 수 있는 드로어블이다
toast.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- shape 색상과 그라데이션을 포함한 도형 모양 지정 -->
<stroke
android:width="4dp"
android:color="#ffffff00"/>
<solid
android:color="#ff8273"/>
<padding
android:left="20dp"
android:top="20dp"
android:right="20dp"
android:bottom="20dp"
/>
<corners
android:radius="15dp" />
</shape>
결과
'모바일 > 안드로이드앱' 카테고리의 다른 글
안드로이드 알림 대화상자 보여주기 / Android Studio / 안드로이드 앱만들기 공부 17 (0) | 2021.02.26 |
---|---|
안드로이드 Snackbar 스낵바/ Android Studio / 안드로이드 앱만들기 공부 16 (0) | 2021.02.26 |
안드로이드 이벤트 처리 단말 방향 세로 가로 / Android Studio / 안드로이드 앱만들기 공부 14 (0) | 2021.02.26 |
안드로이드 이벤트 처리 TouchEvent, GestureDetector / Android Studio / 안드로이드 앱만들기 공부 13 (0) | 2021.02.24 |
안드로이드 드로어블 만들기/ Android Studio / 안드로이드 앱만들기 공부 12 (0) | 2021.02.24 |