標籤:tco 代碼 edr color widget 屬性 idg 翻轉 rap
1)可以把映像的id存放數組中,利用setImageResource()或setImageDrawable()方法(放在數組中便於迴圈)
2)已經是第一張映像時,再點擊“上一頁”,應Toast提示:已經是第一張映像,並不再往前翻;同樣,已經是最後一張映像時,再點擊“下一頁”,應Toast提示:已經是最後一張映像,並不再往後翻。
給出原始碼
<?xml version="1.0" encoding="utf-8"?><FrameLayout 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="horizontal" tools:context="com.example.asus.a161304049_gary03.MainActivity"> <LinearLayout android:id="@+id/Layoutt" android:layout_width="match_parent" android:layout_height="215dp" android:orientation="vertical"> <ImageView android:id="@+id/imagexianshi" android:layout_width="match_parent" android:layout_height="180dp" app:srcCompat="@drawable/qzu1" /> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <Button android:id="@+id/Back" android:layout_width="100dp" android:layout_height="wrap_content" android:gravity="center" android:text="上一張" /> <Button android:id="@+id/Next" android:layout_width="100dp" android:layout_height="wrap_content" android:text="下一張" /> </LinearLayout> </LinearLayout></FrameLayout>
activity_main.xml
package com.example.asus.a161304049_gary03;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.ImageView;import android.widget.Button;import android.widget.Toast;public class MainActivity extends AppCompatActivity { //用一個數組來儲存圖片 int[] images = new int[]{ R.drawable.qzu1, R.drawable.qzu2, R.drawable.qzu3, R.drawable.qzu5 }; //作為圖片的下標 int countImg = 0; //前進和後退的按鈕 private Button B1,B2; //用ImageView來存放圖片 private ImageView Xianshi; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); B1 = (Button) findViewById(R.id.Back); B2 = (Button) findViewById(R.id.Next); Xianshi = (ImageView) findViewById(R.id.imagexianshi); Xianshi.setImageResource(images[0]); B1.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { //這裡就是一個簡單的邏輯啦 //如果選中的圖片是第一張,那麼就不能再選擇前面的一張。輸出Toast提示的訊息 if(countImg>0){ countImg--; //用setImageResource來顯示圖片 Xianshi.setImageResource(images[countImg]); }else{ Toast.makeText(MainActivity.this,"已經是第一張映像,並不再往前翻",Toast.LENGTH_SHORT).show(); } } }); B2.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { //如果選中的圖片是最後,那麼就不能再選擇後面的一張。輸出Toast提示的訊息 if(countImg<3){ countImg++; Xianshi.setImageResource(images[countImg]); }else{ Toast.makeText(MainActivity.this,"已經是最後一張映像,並不再往後翻",Toast.LENGTH_SHORT).show(); } } }); }}
MainActivity
ImageView控制項用來存放圖片展示出來,xml屬性不是本文重點,這裡就不再介紹。
第一步:定義成員變數
//用一個數組來儲存圖片 int[] images = new int[]{ R.drawable.qzu1, R.drawable.qzu2, R.drawable.qzu3, R.drawable.qzu5 }; //作為圖片的下標 int countImg = 0; //前進和後退的按鈕 private Button B1,B2; //用ImageView來存放圖片 private ImageView Xianshi;
第二步:實現按鈕"上一張"和"下一張"的事件響應機制
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); B1 = (Button) findViewById(R.id.Back); B2 = (Button) findViewById(R.id.Next); Xianshi = (ImageView) findViewById(R.id.imagexianshi); Xianshi.setImageResource(images[0]); B1.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { //這裡就是一個簡單的邏輯啦 //如果選中的圖片是第一張,那麼就不能再選擇前面的一張。輸出Toast提示的訊息 if(countImg>0){ countImg--; //用setImageResource來顯示圖片 Xianshi.setImageResource(images[countImg]); }else{ Toast.makeText(MainActivity.this,"已經是第一張映像,並不再往前翻",Toast.LENGTH_SHORT).show(); } } }); B2.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { //如果選中的圖片是最後,那麼就不能再選擇後面的一張。輸出Toast提示的訊息 if(countImg<3){ countImg++; Xianshi.setImageResource(images[countImg]); }else{ Toast.makeText(MainActivity.this,"已經是最後一張映像,並不再往後翻",Toast.LENGTH_SHORT).show(); } } }); }
傳送門:訊息模式Toast.make Text的幾種常見用法
Android_ImageView簡單實現圖片翻轉