2015-10-12 61 views
0

我想開發一個應用程序,從而有一個開始按鈕的活動。按下開始按鈕後,它會引導我開始一個新的活動,定時器開始運行。只有當按鈕和textview在同一頁面上時,我才能使其運行。不過,我想在另一個活動上有一個活動和textview的按鈕。導航CountDownTimer

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" />  
+0

試試我的波紋管代碼,如果它工作正常,然後將我的答案標記爲接受並投票UP –

回答

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文件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>