-1

按钮返回计数和按钮激活,按下它会打开新的活动......
我正在为我的应用程序尝试新事物,这可能吗?(我需要显示插页式添加及其让按钮等待计数。)

方法A

  1. 用户单击按钮
  2. 按钮计数 3. 2. 1. 0
  3. 按钮激活!
    (然后单击按钮并打开下一步/新活动等。)

方法B

  1. 用户打开应用程序 / OnCreate 开始...
  2. 按钮计数 3. 2. 1. 0
  3. 按钮激活!

(...单击按钮并打开下一个/新活动等...)

哪个代码或方法可以做到这一点,请帮忙。谢谢 :)

4

1 回答 1

1

请尝试此代码,最后您只需将意图传递给下一个活动,而不是 toast,然后在第二个活动中粘贴相同的代码,它会更好地工作。

import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class Sample extends Activity {

// Declare a variable to hold count down timer's paused status
private boolean isPaused = false;
// Declare a variable to hold count down timer's paused status
private boolean isCanceled = false;

// Declare a variable to hold CountDownTimer remaining time
private long timeRemaining = 0;
CountDownTimer timer;
long millisInFuture = 4000; // 30 seconds
long countDownInterval = 1000; // 1 second
TextView tView;
Button btnStart;
Button btnPause;
Button btnResume;
Button btnCancel;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Get reference of the XML layout's widgets
    tView = (TextView) findViewById(R.id.tv);
    btnStart = (Button) findViewById(R.id.btn_start);

    // Set a Click Listener for start button
    btnStart.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            timercall();

            // Disable the start and pause button
            btnStart.setEnabled(false);

            // Initialize a new CountDownTimer instance

        }
    });

    // Set a Click Listener for pause button

    // Set a Click Listener for resume button

}

private void timercall() {
    // TODO Auto-generated method stub
    btnStart.setEnabled(false);
    timer = new CountDownTimer(millisInFuture, countDownInterval) {
        public void onTick(long millisUntilFinished) {
            // do something in every tick
            if (isPaused || isCanceled) {
                // If the user request to cancel or paused the
                // CountDownTimer we will cancel the current
                // instance
                cancel();
            } else {
                // Display the remaining seconds to app interface
                // 1 second = 1000 milliseconds
                tView.setText("" + millisUntilFinished / 1000);
                // Put count down timer remaining time in a variable
                timeRemaining = millisUntilFinished;
            }
        }

        public void onFinish() {
            // Do something when count down finished
            tView.setText("Activated");
            btnStart.setText("Active");
            btnStart.setEnabled(true);
            btnStart.setClickable(true);
            // Enable the start button
            btnStart.setEnabled(true);
            // Disable the pause, resume and cancel button
            btnStart.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Toast.makeText(getApplicationContext(), "next page",
                            Toast.LENGTH_SHORT).show();
                }
            });

        }
    }.start();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    // getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    // noinspection SimplifiableIfStatement
    /*
     * if (id == R.id.action_settings) { return true; }
     */

    return super.onOptionsItemSelected(item);
}
}

//activity_main.xml

<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:background="#ffffff"
android:padding="16dp"
tools:context=".MainActivity" >

<TextView
    android:id="@+id/tv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="CountDownTimer will display here..."
    android:textColor="#000000" />

<Button
    android:id="@+id/btn_start"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/tv"
    android:text="Start" />

</RelativeLayout>
于 2016-12-21T11:40:25.363 回答