티스토리 뷰

버튼 클릭 이벤트가 발생하면 토스트 메시지가 나오게 됩니다.

https://youtu.be/s6KKPBWoxOk

 

- YouTube

 

www.youtube.com

토스트메시지가 안나오면 ADV매니저 가서 WIPE DATA를 눌러서 한번 정리하고 실행하면 토스트 메시지 나옵니다.

레이아웃 activity_main.xml

<?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:layout_marginStart="140dp"
        android:layout_marginTop="340dp"
        android:onClick="onButtonClicked"
        android:text="Button"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="140dp"
        android:layout_marginTop="76dp"
        android:onClick="onButtonClicked2"
        android:text="구글접속"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button" />
</androidx.constraintlayout.widget.ConstraintLayout>

 

자바 MainActivity

package com.jw.button_example;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    //버튼 클릭하면 토스트메시지 나오게 하기
    public void onButtonClicked(View v){
        Toast.makeText(this,"버튼을 눌렀어",Toast.LENGTH_SHORT).show();
    }

    //버튼 클릭하면 구글 연결
    public void onButtonClicked2(View view){
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://m.google.com"));
        startActivity(intent);
    }
}
댓글
최근에 달린 댓글
글 보관함
«   2024/04   »
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
Total
Today
Yesterday
    뽀로로친구에디
    최근에 올라온 글