简体   繁体   中英

Using Button Intents on Android

I want to make an app. I'm using Eclipse. I have four activities.

MainActivity = buttonStart

Activity1 = button1

Activity2 = button2

Activity3 = button3

for now, when calling button1, button2, button3 must be pressed.

I want to make something like this..

  1. If buttonStart pressed then directly it will execute button1 for 10 secs.

  2. When the button1's time is out (10 secs). Then it will jump to button2 automatically for 10 secs.

  3. When the button2's time is out (10 secs). Then it will jump to button3 automatically for 10 secs.

MainActivity

Button next = (Button) findViewById(R.id.buttonStart);
            next.setOnClickListener(new View.OnClickListener() {

                public void onClick(View view) {
                    Intent myIntent = new Intent(view.getContext(), classForButton1.class);
                    startActivityForResult(myIntent, 0);
                }
                });

ACTIVITY1

Button next = (Button) findViewById(R.id.button1);
            next.setOnClickListener(new View.OnClickListener() {

                public void onClick(View view) {
                    Intent myIntent = new Intent(view.getContext(), classForButton2.class);
                    startActivityForResult(myIntent, 0);
                }
                });

ACTIVITY2

Button next = (Button) findViewById(R.id.button2);
            next.setOnClickListener(new View.OnClickListener() {

                public void onClick(View view) {
                    Intent myIntent = new Intent(view.getContext(), classForButton2.class);
                    startActivityForResult(myIntent, 0);
                }
                });

ACTIVITY3

Button prev = (Button) findViewById(R.id.button3);
            prev.setOnClickListener(new View.OnClickListener() {

                public void onClick(View view) {
                    Intent intent = new Intent();
                    setResult(RESULT_OK, intent);
                    finish();

                }
                });

I think this can help you.

Button btn_1 = new Button(getApplicationContext());
final Button btn_2 = new Button(getApplicationContext());
final Button btn_3 = new Button(getApplicationContext());

btn_1.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // PERFORM YOUR WORK HERE
        Timer timer = new Timer();
        TimerTask task = new TimerTask() {

            @Override
            public void run() {
                btn_2.performClick();
            }
        };
        timer.schedule(task, 10000);
    }
});

btn_2.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // PERFORM YOUR WORK HERE
        Timer timer = new Timer();
        TimerTask task = new TimerTask() {

            @Override
            public void run() {
                btn_3.performClick();
            }
        };
        timer.schedule(task, 10000);
    }
});

}

为此,我认为您不需要一个按钮,只需在第一个活动中使用一个按钮,在10秒后使用timertask触发startActivity(A),然后在第二个活动的oncreate方法中使用timerTask触发startActivity(B),第三项活动也要这样做。

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