本帖最後由 tonyh 於 2017-6-24 17:59 編輯
- package com.example.student.myapplication;
- 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;
- private ImageView iv;
- 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+" 張");
- btn1= (Button) findViewById(R.id.button1);
- btn2= (Button) findViewById(R.id.button2);
- iv= (ImageView) findViewById(R.id.imageView);
- btn1.setOnClickListener(myListener);
- btn2.setOnClickListener(myListener);
- }
- private 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;
- iv.setImageResource(imgId[p]);
- setTitle("第 "+(p+1)+" / "+count+" 張");
- break;
- case R.id.button2:
- p++;
- if(p==count)
- p=0;
- iv.setImageResource(imgId[p]);
- setTitle("第 "+(p+1)+" / "+count+" 張");
- break;
- }
- }
- };
- }
複製代碼 |