BACK-END/Android

[Android] 안드로이드의 Alert창, Toast 사용방법

단비_danbee 2020. 11. 9. 16:19

developer.android.com/reference/android/widget/Toast

A toast is a view containing a quick little message for the user.
toast 는 사용자를 위한 작은 메시지 알림이다.

 

 

 

 

makeText 함수의 매개인자는 Context context (맥락), CharSequence text (텍스트), int duration (몇초) 총 3개

 

 

 

 

Toast.LENGTH_SHORT Toast.LENGTH_LONG
2초 4초

 

 

 

 

| MainActivity.java

Toast.makeText(MainActivity.this, "3번째 그림", Toast.LENGTH_SHORT).show();

package com.example.aro;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
    TextView tv;
    ImageView iv;
    Button bt1, bt2, bt3;
    int my[]={R.drawable.apple, R.drawable.banana, R.drawable.melon};
    int cnt = 0;

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

        bt1 = this.findViewById(R.id.button1);
        iv = this.findViewById(R.id.myImg);

        bt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                iv.setImageResource(my[cnt]);
                cnt++;
                if(cnt==3) {cnt=0;
                    Toast.makeText(MainActivity.this, "3번째 그림", Toast.LENGTH_SHORT).show();}
            }
        });
    }//onCreate end
}//class end

 

 

| 출력결과