[Android] 안드로이드 스튜디오 버튼으로 Toast(토스트) 예제

    토스트란 간단하게 말해 간단한 안내 메시지를 표시하는 걸 말한다.

     

    xml 파일

    <?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"
    android:weightSum="10"
    tools:context=".MainActivity"
    >
    
    
    <!-- Top Image Layout (주석처리)-->
    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:background="#000000"
    android:layout_weight="8"
    >
    
    <ImageView
    android:id="@+id/imageView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:src="@drawable/red_ball"
    />
    
    </LinearLayout>
    
    <!-- Bottom EditText Layout (주석처리)-->
    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="2"
    android:background="#FFFF00"
    android:weightSum="10"
    >
    
    <EditText
    android:id="@+id/editText"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="8"
    android:hint="Input Message"
    />
    
    <Button
    android:id="@+id/button"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="2"
    android:text="Push"
    android:textSize="22dp"
    android:textColor="#000000"
    android:gravity="center"
    />
    
    </LinearLayout>
    
    </LinearLayout>
    
    

    java파일

    package com.example.a91toast;
    
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.MotionEvent;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.ImageView;
    import android.widget.Toast;
    
    public class MainActivity extends AppCompatActivity { // 메인화면
    
    	private ImageView imageView;
    	private EditText editText;
    	private Button button;
    
    	@Override // 부모 메소드 재정의
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    
    		imageView = (ImageView) findViewById(R.id.imageView);
    		editText = (EditText) findViewById(R.id.editText);
    		button = (Button) findViewById(R.id.button);
    
    		button.setOnClickListener(new View.OnClickListener() {
    			@Override
    			public void onClick(View v) {
    				String msg = editText.getText().toString();
    				editText.setText("");
    				Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
    			}
    		});
    
    		imageView.setOnTouchListener(new View.OnTouchListener() {
    			@Override
    			public boolean onTouch(View v, MotionEvent event) {
    				Toast.makeText(MainActivity.this, "Hello", Toast.LENGTH_SHORT).show();
    				return false;
    			}
    		});
    	}
    }
    

     

    이미지 화면

    qwer이란 문자를 Input Message에 입력후 푸시 버튼을 누르면 qwer이란 문자가 알림으로 뜨게된다.

     

     

    검은 바탕을 누르게 되면 Hello라는 문자가 알림으로 뜨게된다.

     

    크게 어려운건 없는 것 같다.

    그냥 화면 클릭 했을때 알림 창이 뜬다.

    댓글

    Designed by JB FACTORY