Android-Toast的使用方法详解

  1. 献上官方文档<1>:https://developer.android.google.cn/reference/android/widget/Toast
  2. 献上官方文档<2>:https://developer.android.google.cn/guide/topics/ui/notifiers/toasts#java

建议先看完官方的文档,再看本篇博客,本篇博客不讲解实际的用法,只展示相应的使用效果 

  •  最普通的用法,默认位置,只显示Text

//主Activity代码
public class Main_activity extends AppCompatActivity implements View.OnClickListener {

    private Button btn1;
    private Button btn2;
    private Button btn3;
    private Button btn4;
    private Context context;

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

        initView();

    }

    private void initView(){
        //初始化Button
        btn1=findViewById(R.id.btn1);
        btn2=findViewById(R.id.btn2);
        btn3=findViewById(R.id.btn3);
        btn4=findViewById(R.id.btn4);

        context=getApplicationContext();

        //设置监听
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        btn4.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn1:
                Toast.makeText(context, "btn1", Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn2:
                Toast.makeText(context, "btn2", Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn3:
                Toast.makeText(context, "btn3", Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn4:
                Toast.makeText(context, "btn4", Toast.LENGTH_SHORT).show();
                break;
        }
    }

}

 

  •  效果展示:

Android-Toast的使用方法详解_第1张图片

  • 高级使用,自定义显示位置,自定义显示内容 

public class Main_activity extends AppCompatActivity implements View.OnClickListener {

    private Button btn1;
    private Button btn2;
    private Button btn3;
    private Button btn4;
    private Context context;
    private Toast toast;

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

        initView();

    }

    private void initView(){
        //初始化Button
        btn1=findViewById(R.id.btn1);
        btn2=findViewById(R.id.btn2);
        btn3=findViewById(R.id.btn3);
        btn4=findViewById(R.id.btn4);

        context=getApplicationContext();

        LayoutInflater layoutInflater = getLayoutInflater();
        View view = layoutInflater.inflate(R.layout.toast_view, (ViewGroup) findViewById(R.id.toast_content));
        toast = new Toast(context);
        toast.setGravity(Gravity.TOP,0,800);
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.setView(view);

        //设置监听
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        btn4.setOnClickListener(this);

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn1:
                toast.show();
                break;
            case R.id.btn2:
                break;
            case R.id.btn3:
                Toast.makeText(context, "btn3", Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn4:
                Toast.makeText(context, "btn4", Toast.LENGTH_SHORT).show();
                break;
        }
    }

}

 




    

    


 

 效果展示: 

Android-Toast的使用方法详解_第2张图片

ToastDemo代码:https://download.csdn.net/download/qq_41113081/10920849

你可能感兴趣的:(Android)