2016-08-03 47 views
-1

Hier möchte ich eine Android-Downcount-App für das nächste Fußballspiel, das Datum aus dem Internet und zeigt die verbleibende Zeit, um das Spiel zu starten. Nach einigen Recherchen habe ich den folgenden Code, aber es zählt nur Datum, nämlich JJJJ-MM-TT, aber ich möchte es Datum und Uhrzeit zählen (JJJJ-MM-TT-Std: Min).Machen Sie einen Android-Countdown

enter image description here

MainActivity.java

import java.text.SimpleDateFormat; 
import java.util.Date; 

import android.annotation.SuppressLint; 
import android.app.Activity; 
import android.os.Bundle; 
import android.os.Handler; 
import android.view.View; 
import android.widget.LinearLayout; 
import android.widget.TextView; 
import android.widget.Toast; 

public class MainActivity extends Activity { 

    private TextView tvDay, tvHour, tvMinute, tvSecond, tvEvent; 
    private LinearLayout linearLayout1, linearLayout2; 
    private Handler handler; 
    private Runnable runnable; 

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

     initUI(); 
     countDownStart(); 
    } 

    @SuppressLint("SimpleDateFormat") 
    private void initUI() { 
     linearLayout1 = (LinearLayout) findViewById(R.id.ll1); 
     linearLayout2 = (LinearLayout) findViewById(R.id.ll2); 
     tvDay = (TextView) findViewById(R.id.txtTimerDay); 
     tvHour = (TextView) findViewById(R.id.txtTimerHour); 
     tvMinute = (TextView) findViewById(R.id.txtTimerMinute); 
     tvSecond = (TextView) findViewById(R.id.txtTimerSecond); 
     tvEvent = (TextView) findViewById(R.id.tvevent); 
    } 

    // //////////////COUNT DOWN START///////////////////////// 
    public void countDownStart() { 
     handler = new Handler(); 
     runnable = new Runnable() { 
      @Override 
      public void run() { 
       handler.postDelayed(this, 1000); 
       try { 
        SimpleDateFormat dateFormat = new SimpleDateFormat(
          "yyyy-MM-dd"); 
        // Here Set your Event Date 
        Date futureDate = dateFormat.parse("2016-12-30"); 
        Date currentDate = new Date(); 
        if (!currentDate.after(futureDate)) { 
         long diff = futureDate.getTime() 
           - currentDate.getTime(); 
         long days = diff/(24 * 60 * 60 * 1000); 
         diff -= days * (24 * 60 * 60 * 1000); 
         long hours = diff/(60 * 60 * 1000); 
         diff -= hours * (60 * 60 * 1000); 
         long minutes = diff/(60 * 1000); 
         diff -= minutes * (60 * 1000); 
         long seconds = diff/1000; 
         tvDay.setText("" + String.format("%02d", days)); 
         tvHour.setText("" + String.format("%02d", hours)); 
         tvMinute.setText("" + String.format("%02d", minutes)); 
         tvSecond.setText("" + String.format("%02d", seconds)); 
        } else { 
         linearLayout1.setVisibility(View.VISIBLE); 
         linearLayout2.setVisibility(View.GONE); 
         tvEvent.setText("Android Event Start"); 
         handler.removeCallbacks(runnable); 
         // handler.removeMessages(0); 
        } 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }; 
     handler.postDelayed(runnable, 0); 
    } 

    // //////////////COUNT DOWN END///////////////////////// 
} 

Activity_main.xml

<LinearLayout 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" > 

    <LinearLayout 
     android:id="@+id/ll1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="10dp" 
     android:background="@drawable/counter_back" 
     android:gravity="center" 
     android:orientation="horizontal" 
     android:visibility="gone" > 

     <TextView 
      android:id="@+id/tvevent" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:gravity="center_horizontal|center_vertical" 
      android:singleLine="true" 
      android:text="Android Event Start" 
      android:textColor="#fff" 
      android:textSize="24sp" 
      android:textStyle="bold" /> 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/ll2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="20dp" 
     android:layout_marginRight="20dp" 
     android:layout_marginTop="10dp" 
     android:background="@drawable/counter_back" 
     android:gravity="center" 
     android:orientation="horizontal" 
     android:visibility="visible" > 

     <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:background="@drawable/counter_back" 
      android:gravity="center" 
      android:orientation="vertical" > 

      <TextView 
       android:id="@+id/txtTimerDay" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="3" 
       android:gravity="center" 
       android:text="00" 
       android:textAppearance="?android:attr/textAppearanceLarge" 
       android:textColor="#fff" /> 

      <TextView 
       android:id="@+id/txt_TimerDay" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:gravity="center_horizontal" 
       android:text="Days" 
       android:textAppearance="?android:attr/textAppearanceSmall" 
       android:textColor="#fff" /> 
     </LinearLayout> 

     <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:background="@drawable/counter_back" 
      android:gravity="center" 
      android:orientation="vertical" > 

      <TextView 
       android:id="@+id/txtTimerHour" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="3" 
       android:gravity="center" 
       android:text="00" 
       android:textAppearance="?android:attr/textAppearanceLarge" 
       android:textColor="#fff" /> 

      <TextView 
       android:id="@+id/txt_TimerHour" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:gravity="center_horizontal" 
       android:text="Hour" 
       android:textAppearance="?android:attr/textAppearanceSmall" 
       android:textColor="#fff" /> 
     </LinearLayout> 

     <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:background="@drawable/counter_back" 
      android:gravity="center" 
      android:orientation="vertical" > 

      <TextView 
       android:id="@+id/txtTimerMinute" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="3" 
       android:gravity="center" 
       android:text="00" 
       android:textAppearance="?android:attr/textAppearanceLarge" 
       android:textColor="#fff" /> 

      <TextView 
       android:id="@+id/txt_TimerMinute" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:gravity="center_horizontal" 
       android:text="Minute" 
       android:textAppearance="?android:attr/textAppearanceSmall" 
       android:textColor="#fff" /> 
     </LinearLayout> 

     <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:background="@drawable/counter_back" 
      android:gravity="center" 
      android:orientation="vertical" > 

      <TextView 
       android:id="@+id/txtTimerSecond" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="3" 
       android:gravity="center" 
       android:text="00" 
       android:textAppearance="?android:attr/textAppearanceLarge" 
       android:textColor="#fff" /> 

      <TextView 
       android:id="@+id/txt_TimerSecond" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:gravity="center_horizontal" 
       android:text="Second" 
       android:textAppearance="?android:attr/textAppearanceSmall" 
       android:textColor="#fff" /> 
     </LinearLayout> 
    </LinearLayout> 

</LinearLayout> 
+0

Ich verstehe nicht wirklich, was exacty fragen Sie – josemigallas

+0

„es zählt nur Datum dh yyyy-MM-dd "aber du hast geschrieben" SimpleDateFormat dateFormat = new SimpleDateFormat ( "yyyy-MM-dd"); ', also verstehe ich nicht, was du sonst noch erwarten würdest? – njzk2

Antwort

0

Ihr Problem ist in diesem Code:

SimpleDateFormat dateFormat = new SimpleDateFormat(
         "yyyy-MM-dd"); 
       // Here Set your Event Date 
       Date futureDate = dateFormat.parse("2016-12-30"); 

Sie auf ein brauchen dd Stunden und Minuten auf die Formatierung, wie der Code unter ->

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm"); 

das gleiche mit dem futureDate Sie auch

+0

können Sie es bearbeiten, damit ich es verwenden konnte. habe nur ein paar Ideen .. –