使用CountDownTimer进行导航

时间:2015-10-12 07:34:37

标签: android countdowntimer

我正在尝试开发一个应用程序,其中有一个带有开始按钮的活动。按下开始按钮后,它将引导我进入计时器开始运行的新活动。当按钮和textview位于同一页面时,我只能使它运行。但是,我希望在一个活动上有按钮,在另一个活动上有textview。

Java文件:

import android.content.DialogInterface;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.view.View.OnClickListener;

public class MainActivity extends AppCompatActivity  {

Button buttonStart;
TextView textCounter;

MyCountDownTimer myCountDownTimer;

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

    buttonStart = (Button)findViewById(R.id.start);
    textCounter = (TextView)findViewById(R.id.counter);

    buttonStart.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            myCountDownTimer = new MyCountDownTimer(30000, 1000, textCounter);
            myCountDownTimer.start();
        }
    });

}

public class MyCountDownTimer extends CountDownTimer {

    private TextView textCounter;
    public MyCountDownTimer(long millisInFuture, long countDownInterval, TextView textCounter) {
        super(millisInFuture, countDownInterval);
        this.textCounter = textCounter;
    }

    @Override
    public void onTick(long millisUntilFinished){
        this.textCounter.setText(millisUntilFinished / 1000l + " seconds remaining ");

    }

    @Override
    public void onFinish() {
        this.textCounter.setText("Finished");

    }

}



@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);
}
}

Main_Activity:

<Button
    android:id="@+id/start"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/Button"
    android:clickable="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

Main_Activity2:

<TextView
    android:id="@+id/counter"
    android:layout_width="300dp"
    android:layout_height="300dp"
    android:textStyle="bold"
    android:textSize="50sp"
    android:gravity="center"
    android:layout_marginTop="50dp"
    android:clickable="false"
    android:enabled="false" />    

1 个答案:

答案 0 :(得分:0)

试用此代码: 布局1的代码:activity_main.xml,仅带有开始按钮

   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

    <Button
        android:id="@+id/start"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:clickable="true"
        android:text="Start" />

</LinearLayout>

布局2的代码:count_down_activity.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/counter"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_marginLeft="50dp"
        android:layout_marginTop="50dp"
        android:gravity="center"
        android:textSize="50sp"
        android:textStyle="bold" />

</LinearLayout>

Java文件:MainActivity.java

    import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

    private Context mContext;
       @Override
       protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
              mContext = this;
              Button startBrowser = (Button) findViewById(R.id.start);
              startBrowser.setOnClickListener(new View.OnClickListener() {
                 public void onClick(View view) {

                    startActivity(new Intent(mContext, CountDownActivity.class));
                 }
              });
       }

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

}

Java File 2:CountDownActivity.java

    import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.Menu;
import android.widget.TextView;

public class CountDownActivity extends Activity {

    TextView textCounter;
    MyCountDownTimer myCountDownTimer;

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

              textCounter = (TextView)findViewById(R.id.counter);

              myCountDownTimer =  new MyCountDownTimer(30000, 1000, textCounter);
              myCountDownTimer.start();

       }
       public class MyCountDownTimer extends CountDownTimer {

            private TextView textCounter;
            public MyCountDownTimer(long millisInFuture, long countDownInterval, TextView textCounter) {
                super(millisInFuture, countDownInterval);
                this.textCounter = textCounter;
            }

            @Override
            public void onTick(long millisUntilFinished) {
                this.textCounter.setText(millisUntilFinished / 1000l + " seconds remaining : ");

            }

            @Override
            public void onFinish() {
                this.textCounter.setText("Finished");

            }

        }

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

}

的AndroidManifest.xml

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.androidsample"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.androidsample.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.androidsample.CountDownActivity"
            android:screenOrientation="portrait" >
        </activity>

    </application>

</manifest>