Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- tic-tac-toe
- android studio
- dart
- 양방향 연결리스트
- programmers
- 안드로이드 스튜디오
- 안드로이드
- 개발
- 알고리즘
- leetcode
- 게임
- 플러터
- c언어
- c언어 프로젝트
- Baekjoon
- 기초
- 단방향 연결리스트
- 코딩테스트
- 코딩
- 백준
- 연결리스트
- android studio tutorial
- 안드로이드 튜토리얼
- C
- level1
- IT
- 풀이
- 프로젝트
- Flutter
- 틱택토
Archives
- Today
- Total
얼렁뚱땅 개발 블로그
[안드로이드 스튜디오] 버튼과 토스트 메시지 사용하기 본문
반응형
1. activity_main.xml에 버튼(button)을 추가
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.515" />
</androidx.constraintlayout.widget.ConstraintLayout>
2. MainActivity 클래스에서 버튼(button)을 참조
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button)findViewById(R.id.button);
}
}
findViewById(int id)를 이용하여 지정한 id 속성 값을 사용하여 참조를 합니다.
3. 버튼(button) 클릭에 대한 이벤트 처리
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
- setOnClickListener(OnClickListener listener) : 해당 요소에 이벤트 리소너를 설정해주는 메서드 입니다.
- OnClickListener() : View 클래스 내에 있는 인터페이스입니다. 이벤트가 발생 되었을 때 호출하는 메서드가 아래와 같이 있습니다.
메서드 | 설명 |
onClick(View view) | 사용자가 항목을 터치하면 호출됩니다. |
onLongClick(View view) | 사용자가 항목을 길게 터치하면 호출됩니다. |
onFocusChange(View v) | 사용자가 다른 항목으로 포커스를 하면 호출됩니다. |
onKey(View v) | 사용자가 기기에 있는 키를 누르거나 손을 뗴면 호출됩니다. |
onTouch(View v) | 사용자가 터치 이벤트로서의 자격을 만족하는 작업을 수행하는 경우에 호출되며, 여기에는 누르기, 손 떼기와 화면에서 이루어지는 모든 움직임 동작(항목의 경계 내에서)이 포함됩니다. |
onCreateContextMenu(View v) | 메뉴가 구축되는 중일 때 호출됩니다. |
- OnClick(View view) : 사용자가 항목을 터치하면 호출되는 메서드입니다.
4. 토스트 메시지 추가
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "on Click Button", Toast.LENGTH_SHORT).show();
}
OnClick(View view) 안에 토스트 메시지를 출력하기 위해 Toast 클래스 안에 있는 makeText(Context context, CharSequence text, int duration)를 작성하여 줍니다.
- Context context : getApplicationContext()를 이용하여 현재 프로세스의 context를 넘겨줍니다.
- CharSequence text : 사용자에게 보여줄 문자열을 넘겨줍니다.
- int duration : 메시지를 띄우는 시간을 지정하여 줍니다.
인자값 | 설명 |
Toast.LENGTH_SHORT | 짤게 토스트 메시지를 출력합니다. |
Toast.LENGTH_LONG | 길게 토스트 메시지를 출력합니다. |
그 다음 show()를 통해 토스트 메시지를 출력하여 줍니다.
반응형
'전공 > 안드로이드' 카테고리의 다른 글
[안드로이드 스튜디오 ] 프로젝트 구조 및 Hello world 출력하기 (0) | 2020.05.01 |
---|---|
[안드로이드 스튜디오] 안드로이드 스튜디오 시작하기 (0) | 2020.05.01 |
Comments