简体   繁体   中英

Android next and previous buttons in Framelayout

I am trying to create a previous button to this framelayout so that it can have previous and next as well as home button. Can anyone please help me? I just need help with adding the previous button. Does anyone also know what would be the easiest way of adding numbers below the images, eg image 1 of 20 so that people know when they're at the last image.

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;

 public class MainActivity extends Activity {
    int count = 1;
    FrameLayout frame;
    ImageView imageview;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        frame = (FrameLayout) findViewById(R.id.framelayout);
    }
    public void btnClick(View view) {
        //---hide the current one---
        imageview = (ImageView) frame.findViewWithTag(String.valueOf(count));
        imageview.setVisibility(android.view.View.INVISIBLE);

    //---go to the next image---
        count++;
        if (count>3) count = 1;

    //---show the next image---
        imageview = (ImageView) frame.findViewWithTag(String.valueOf(count));
        imageview.setVisibility(android.view.View.VISIBLE);
        }
    }

Set an onClickListener on your buttons:

myButton.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
    // Change the images here
    }
});

Whenever you click your button the code inside onClick will be executed, so the only thing you do there is change the image. One listener for the next button and another for the previous.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM