溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

實現(xiàn)Button控制ImageView顯示下一張照片

發(fā)布時間:2020-08-10 12:35:16 來源:網(wǎng)絡(luò) 閱讀:2070 作者:吃素只吃肉 欄目:移動開發(fā)

話不多說,直接上碼:xml文件,簡單的線性布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center">

    </LinearLayout>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button3"
        android:text="下一張"
        android:layout_gravity="right" />

    <ImageView
        android:id="@+id/p_w_picpath2"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:src="@drawable/cat"
        android:scaleType="fitCenter"
    />
</LinearLayout>

效果圖實現(xiàn)Button控制ImageView顯示下一張照片




package com.example.administrator.myapplication;

import android.app.Activity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;

/**
 * Created by Administrator on 2015/6/6.
 */
public class Textp_w_picpathActivity extends Activity{
    //定義一個訪問照片的數(shù)組
    int[] p_w_picpaths=new int[]{
            R.drawable.cat,
            R.drawable.dog,
            R.drawable.happen,
            R.drawable.hashiqi,
            R.drawable.what,
            R.drawable.name,
            };
//定義默認(rèn)顯示的照片
    int currentImg=2;
       protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//隱藏TITLE
        setContentView(R.layout.p_w_picpathview);
        final ImageView p_w_picpath2=(ImageView)findViewById(R.id.p_w_picpath2);
        final Button next=(Button)findViewById(R.id.button3);
              //定義查看下一張圖片的監(jiān)聽器
        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //控制Image View顯示下一張照片
                p_w_picpath2.setImageResource(
                        p_w_picpaths[++currentImg % p_w_picpaths.length]
                );
            }
        });


}}


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI