Android图片切换和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"
    android:gravity="center"
    tools:context="com.example.administrator.myapplication.ImgActivity">

    <ImageView
        android:id="@+id/IV"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@drawable/logo_01"
        />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        <Button
            android:id="@+id/bt1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="上一张"

            />
        <Button
            android:id="@+id/bt2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="下一张"

            />


    </LinearLayout>
    <Button
        android:id="@+id/bt3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="带文字的toals"
        />
    <Button
        android:id="@+id/bt4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="带文字改变位置的toals"/>
    <Button
        android:id="@+id/bt5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="带图片的toals"/>

</LinearLayout>

后台代码

package com.example.administrator.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.Toast;

public class ImgActivity extends AppCompatActivity implements View.OnClickListener{
    ImageView IV;
    Button bt1,bt2,bt3,bt4,bt5;
    //存储图片资源的数组
    private int[] photos = {R.drawable.logo_01, R.drawable.logo_02, R.drawable.logo_03
            , R.drawable.logo_04, R.drawable.logo_05, R.drawable.logo_06};
    //显示当前图片的索引
    private int photoIndex = 0;
    //图片索引最大值(图片数-1)
    private int maxIndex = 5;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_img);
        IV= (ImageView) findViewById(R.id.IV);
        bt1= (Button) findViewById(R.id.bt1);
        bt2= (Button) findViewById(R.id.bt2);
        bt3= (Button) findViewById(R.id.bt3);
        bt4= (Button) findViewById(R.id.bt4);
        bt5= (Button) findViewById(R.id.bt5);

        bt1.setOnClickListener(this);
        bt2.setOnClickListener(this);
        bt3.setOnClickListener(this);
        bt4.setOnClickListener(this);
        bt5.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bt1:
                //如果当前图片是第一张,则上一张图片为最后一张图片
                if (photoIndex == 0) {
                    photoIndex = maxIndex;
                } else {
                    //否则改为上一张图片索引
                    photoIndex = photoIndex - 1;
                }

                break;
            case R.id.bt2:
                //如果当前图片是最后一张,则下一张图片为第一张图片
                if (photoIndex == maxIndex) {
                    photoIndex = 0;
                } else {
                    //否则改为下一张图片索引
                    photoIndex = photoIndex + 1;
                }
                break;
            case R.id.bt3:
                Toast.makeText(this, "带文字的toast", Toast.LENGTH_SHORT).show();
                break;
            case R.id.bt4:
                Toast toast=Toast.makeText(this,"带文字该位置",Toast.LENGTH_LONG);
                toast.setGravity(Gravity.CENTER,0,0);
                toast.show();
                break;
            case R.id.bt5:
                Toast toast1=Toast.makeText(this,"带t图片",Toast.LENGTH_LONG);
                toast1.setGravity(Gravity.CENTER,0,0);
                ImageView ImageView=new ImageView(this);
                ImageView.setImageResource(R.drawable.logo_01);
                LinearLayout LinearLayout=(LinearLayout)toast1.getView();
                LinearLayout.addView(ImageView);
                toast1.show();
                break;
            default:
                break;
        }
        IV.setImageResource(photos[photoIndex]);
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值