프로그래밍/안드로이드

안드로이드 Toast 메시지에 내가 원하는 그림을 넣어보자

가카리 2012. 8. 13. 22:53
반응형

 

 

 

위는 file explore입니다.

 

mina.jpg는 그림파일인데 안드로이드에서는 png jpg를 쓰는 것을 추천합니다. 저는 아래 사진을 썼습니다.

 

 

 

토스트메시지는 화면에 잠시 나타나는 메시지인데 디버깅할 때나 잠시 사용자에게 메시지를 보여줄 때 유용합니다.

 

static Toast makeText(Context context, int resId, int duration)

static Toast makeText)Context context, CharSequence text, int duration)

 

첫번째인자는 액티비티를 전달하므로 액티비티.this로 넘겨주면 되고 두번째인자는 메시지인데 String타입도 가능합니다.

세번째인자는 메시지의 지속시간인데 LENGTH_SHORT LENGTH_LONG 값중 하나를 지정합니다.

 

그리고 마지막에 반드시

show()메소드를 호출해줘야합니다.

 

다음예제를 봅시다.

 

activity_main.xml

 

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

                    android:orientation="vertical"

                    android:layout_width="fill_parent"

                    android:layout_height="fill_parent"

                    >

   

    <Button

        android:id="@+id/shortmsg"

                     android:layout_width="fill_parent"

                     android:layout_height="wrap_content"

                     android:text="짧은 메시지"

           />

    <Button

        android:id="@+id/longmsg"

                     android:layout_width="fill_parent"

                     android:layout_height="wrap_content"

                     android:text="긴 메시지"

           />

    <Button

        android:id="@+id/count1"

                     android:layout_width="fill_parent"

                     android:layout_height="wrap_content"

                     android:text="카운트 연속 출력"

           />          

    <Button

        android:id="@+id/count2"

                     android:layout_width="fill_parent"

                     android:layout_height="wrap_content"

                     android:text="카운트 연속 출력2"

           />   

    <Button

        android:id="@+id/customview"

                     android:layout_width="fill_parent"

                     android:layout_height="wrap_content"

                     android:text="커스텀 뷰 표시"

           />

   

   

</LinearLayout>

 

toast.xml

 

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:id="@+id/toastlayout"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="vertical" >

  

   

    <!--  mina .jpg파일명을 나타냅니다. -->

     <TextView

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:layout_marginTop="10dp"

        android:text="민아 사랑"

        android:textSize="50dp"

           />

   <ImageView

        android:layout_width = "fill_parent"

        android:layout_height = "wrap_content"

        android:src="@drawable/mina"

        />

 

</LinearLayout>

 

 

 

MainActivity 클래스

 

package com.example.toasttest4;

 

 

 

import android.os.Bundle;

import android.app.Activity;

import android.view.View;

import android.widget.Button;

import android.widget.LinearLayout;

import android.widget.Toast;

 

public class MainActivity extends Activity {

    Toast mToast = null;

    int count;

    String str;

 

    public void onCreate(Bundle savedInstanceState) {

                     super.onCreate(savedInstanceState);

                     setContentView(R.layout.activity_main);

                    

                     findViewById(R.id.shortmsg).setOnClickListener(mClickListener);//리스너 등록 및 xml객체 가져옴

                     findViewById(R.id.longmsg).setOnClickListener(mClickListener);//리스너 등록 및 xml객체 가져옴

                     findViewById(R.id.count1).setOnClickListener(mClickListener);//리스너 등록 및 xml객체 가져옴

                     findViewById(R.id.count2).setOnClickListener(mClickListener);//리스너 등록 및 xml객체 가져옴

                     findViewById(R.id.customview).setOnClickListener(mClickListener);//리스너 등록 및 xml객체 가져옴

           }

 

           Button.OnClickListener mClickListener = new Button.OnClickListener() {

                    

                    

                     public void onClick(View v) {

                    

                                //xml 객체에 따라서 다른 메시지를 출력함.

                                switch (v.getId()) {

                                case R.id.shortmsg:

                                          Toast.makeText(MainActivity.this, "잠시 나타나는 메시지",

                                                                Toast.LENGTH_SHORT).show();

                                          break;

                                case R.id.longmsg:

                                          Toast.makeText(MainActivity.this, "조금 길게 나타나는 메시지",

                                                                Toast.LENGTH_LONG).show();

                                          break;

                                case R.id.count1:

                                          str = "현재 카운트 = " + count++;

                                          if (mToast != null) {

                                                     mToast.cancel();//cancel메소드를 호출하면 토스트는 사라지지만 이전메시지의 지속시간이 지나야 나타난다.

                                                     //이 지속시간을 없앨수가 없다.

                                          }

                                          mToast = Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT);

                                          mToast.show();

                                          break;

                                case R.id.count2:

                                          str = "현재 카운트 = " + count++;

                                          if (mToast == null) {

                                                     mToast = Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT);

                                          } else {

                                                     mToast.setText(str);//setText로 하면 즉시 값이 바뀐다.

                                          }

                                          mToast.show();

                                          break;

                                case R.id.customview:

                                         

                                          //여기서는 inflate.

                                          //inflate 간단히 말해서 xml 그대로 화면에 출력하라는 뜻

                                          //toast.xmlToast메시지로 출력함.

                             LinearLayout linear = (LinearLayout)View.inflate(MainActivity.this, R.layout.toast, null);

                             Toast t2 = new Toast(MainActivity.this);

                             t2.setView(linear);

                             t2.show();

                             break;

                                }

                     }

           };

}

 

출력화면입니다. 카운트 연속 출력과 카운트 연속 출력2를 눌러보세요.

 

카운트 연속 출력은 버튼을 빠르게 누르면 숫자가 잘 안바뀌는데 카운트 연속 출력2를 누르면 빠르게 눌러도 숫자가 잘 바뀌는 것을 알 수 있습니다.

 

 

 

 

 

 

반응형