Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 按下按钮并从3到0计数_Java_Android_Android Button_Counting_Click Counting - Fatal编程技术网

Java 按下按钮并从3到0计数

Java 按下按钮并从3到0计数,java,android,android-button,counting,click-counting,Java,Android,Android Button,Counting,Click Counting,按钮后退计数并激活按钮,按下按钮将打开新活动… 我正在尝试为我的应用程序添加新内容,那么这可能吗?(我需要显示填隙添加及其用于使按钮等待计数。) 方法A 用户单击一个按钮 按钮计数3。2.1.0 按钮激活 (然后单击按钮并打开下一步/新活动等) 方法B 用户打开应用程序/OnCreate启动 按钮计数3。2.1.0 按钮激活 (…单击按钮并打开下一步/新活动等) 哪种代码或方法可以做到这一点,请帮助。谢谢:)请尝试此代码,在结束时,您只需将意图传递给下一个活动,并在第二个活动中粘贴此代码,效果会

按钮后退计数并激活按钮,按下按钮将打开新活动…
我正在尝试为我的应用程序添加新内容,那么这可能吗?(我需要显示填隙添加及其用于使按钮等待计数。)

方法A

  • 用户单击一个按钮
  • 按钮计数3。2.1.0
  • 按钮激活
    (然后单击按钮并打开下一步/新活动等)
  • 方法B

  • 用户打开应用程序/OnCreate启动
  • 按钮计数3。2.1.0
  • 按钮激活 (…单击按钮并打开下一步/新活动等)


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

    请尝试此代码,在结束时,您只需将意图传递给下一个活动,并在第二个活动中粘贴此代码,效果会更好

    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>
    
    
    
    您是否试图设置按钮上显示的超时,以强制用户查看间隙?非常感谢您HsRaja:)我会尝试它工作得很好真的谢谢