Toast 简单实例应用

界面代码:

<?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"
    tools:context=".MainActivity">

  <Button
      android:id="@+id/btn1"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:textSize="20sp"
      android:text="默认Toast"/>
    <Button
        android:id="@+id/btn2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:text="自定义Toast"/>

    <Button
        android:id="@+id/btn3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:text="带图片的Toast"/>



</LinearLayout>

JAVA代码:

package com.example.dell.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.Toast;

import org.w3c.dom.Text;

public class MainActivity extends AppCompatActivity {
    Button btn1,btn2,btn3;
    private Toast toast;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn1=(Button)findViewById(R.id.btn1);
        btn2=(Button)findViewById(R.id.btn2);
        btn3=(Button)findViewById(R.id.btn3);
        btn3.setOnClickListener(new Action());
        btn2.setOnClickListener(new Action());
        btn1.setOnClickListener(new Action());


    }
    class Action implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            switch(v.getId()){
                case R.id.btn1 :

                    Toast.makeText(MainActivity.this, "默认位置的Toast", Toast.LENGTH_SHORT).show();
                    return;
                case  R.id.btn2 :
                    toast=Toast.makeText(MainActivity.this,"自定义位置的Toast",Toast.LENGTH_LONG);
                    toast.setGravity(Gravity.CENTER,0,0);
                    toast.show();
                    return;
                case R.id.btn3:
                    toast=Toast.makeText(MainActivity.this,"带图标的Toast",Toast.LENGTH_LONG);
                    toast.setGravity(Gravity.CENTER,0,0);
                    LinearLayout toastview=(LinearLayout)toast.getView();//获取Toas的viw对象
                    ImageView image=new ImageView(MainActivity.this);//创建Imageview对象
                    image.setImageResource(R.mipmap.log);//设置Imagevie的背景图片
                    toastview.addView(image);将Imageview添加到view上
                    toast.setView(toastview);//将view显示到Toast上
                    toast.show();
                    return;
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_39046183/article/details/83479455
今日推荐