- package com.example.student.myapplication;
- import android.app.AlertDialog;
- import android.content.DialogInterface;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.ImageView;
- public class MainActivity extends AppCompatActivity {
- private int imgId[] = {R.drawable.img01, R.drawable.img02, R.drawable.img03, R.drawable.img04, R.drawable.img05, R.drawable.img06};
- private Button btn1, btn2, end;
- private ImageView imageView;
- private int p = 0, count = imgId.length;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- setTitle("第 " + (p + 1) + " / " + count + " 張");
- btn2 = (Button) findViewById(R.id.button2);
- btn1 = (Button) findViewById(R.id.button1);
- end = (Button) findViewById(R.id.end);
- imageView = (ImageView) findViewById(R.id.imageView);
- btn1.setOnClickListener(myListener);
- btn2.setOnClickListener(myListener);
- end.setOnClickListener(myListener);
- }
- public View.OnClickListener myListener = new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- switch (v.getId()) {
- case R.id.button1:
- p--;
- if (p < 0)
- p = count - 1;
- imageView.setImageResource(imgId[p]);
- setTitle("第 " + (p + 1) + " / " + count + " 張");
- break;
- case R.id.button2:
- p++;
- if (p == count)
- p = 0;
- imageView.setImageResource(imgId[p]);
- setTitle("第 " + (p + 1) + " / " + count + " 張");
- break;
- case R.id.end:
- AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
- builder.setTitle("確認視窗");
- builder.setIcon(R.mipmap.ic_launcher);
- builder.setMessage("即將離開應用程式!");
- builder.setPositiveButton("確認", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- finish();
- }
- });
- builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- }
- });
- builder.show();
- builder.setCancelable(false);
- break;
- }
- }
- };
- }
複製代碼- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
- android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
- android:background="#000000">
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="下一頁"
- android:id="@+id/button2"
- android:layout_alignParentBottom="true"
- android:layout_alignParentEnd="true"
- android:layout_marginBottom="172dp" />
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="上一頁"
- android:id="@+id/button1"
- android:layout_alignTop="@+id/button2"
- android:layout_alignParentStart="true" />
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/imageView"
- android:layout_alignParentTop="true"
- android:layout_alignEnd="@+id/button2"
- android:layout_alignParentStart="true"
- android:layout_above="@+id/button2" />
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="結束"
- android:id="@+id/end"
- android:layout_below="@+id/button1"
- android:layout_alignParentStart="true"
- android:layout_marginTop="46dp" />
- </RelativeLayout>
複製代碼 |