2016-03-13 42 views
0

我真的新到Android編程(昨天開始)和IM目前的工作,應該給我點擊一個按鈕,第二次點擊按鈕第一時間之間的時間。時間戳不能正常工作,改變上的應用程式活動

它,當我留在活動工作正常。但是如果我在時間「運行」時更改活動,然後重新輸入主要活動,它會給我一個奇怪的時間戳。

時間戳工作原理:

我有開始/停止 一個按鈕上點擊啓動呼叫的方法從哪裏獲得以毫秒爲單位的當前系統時間並將其保存到一個變量。 單擊停止時,它會執行相同的操作並減去endTime-startTime。多數民衆贊成我是如何得到總時間。 (正常工作)

但改變活動時(我有一個按鈕,它變成一個活動,我可以添加一個客戶),並重新進入主要和停止計時器,它增加了TOTALTIME起來的東西我不能涉及到..目前我的停留時間是在45分鐘。

也許我做錯事上救了我的價值觀?

我只是張貼我的代碼。也許有人可以幫助我,給我一個提示。謝謝並對我的英語不好!

類 「Timerecording」

package com.example.cmsolutions.zeiterfassung; 

import android.content.Intent; 
import android.content.SharedPreferences; 
import android.os.SystemClock; 
import android.preference.PreferenceManager; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.Spinner; 
import android.widget.TextView; 
import android.view.View; 
import java.util.ArrayList; 
import java.util.Date; 
import java.util.LinkedList; 


public class ZeitErfassen extends AppCompatActivity { 

public static LinkedList<Kunde> kunden = new LinkedList(); 
boolean running = false; 
long startTime,endTime,totalTime; 
public Date date = new Date(); 

private SharedPreferences app_preferences; 
private SharedPreferences.Editor editor; 
private static final int PREFERENCE_MODE_PRIVAT=0; 
private TextView displayTime; 
public Button startEndButton; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_zeit_erfassen); 
    //Einstellungen laden 
    app_preferences = getPreferences(PREFERENCE_MODE_PRIVAT); 
    displayTime = (TextView)findViewById(R.id.zeit_bei_Kunde); 
    startTime= app_preferences.getLong("startTime", 0); 
    endTime = app_preferences.getLong("endTime", 0); 
    running = app_preferences.getBoolean("running", false); 
    totalTime = app_preferences.getLong("totalTime", 0); 
    displayTime.setText((CharSequence) app_preferences.getString("zeitAnzeige", "Zeit bei Kunde")); 
    startEndButton = (Button)findViewById(R.id.start_Timer); 
    startEndButton.setText((CharSequence)app_preferences.getString("timerButton","Start Timer")); 
    editor = app_preferences.edit(); 
    editor.commit(); 

    createDropDown(); 

} 


public void startTimer(View view) { 



    if(running == false) { 
     startTime = getTime(); 
     displayTime.setText("Zeitstoppung läuft"); 
     editor.putString("zeitAnzeige",(String)displayTime.getText()); 
     running = true; 
     editor.putBoolean("running",true); 
     editor.putLong("startTimer", startTime); 
     startEndButton.setText("End Timer"); 
     editor.putString("timerButton", (String)startEndButton.getText()); 
     editor.commit(); 

    } else { 
     endTime = getTime(); 
     editor.putLong("endTime",endTime); 
     totalTime = endTime - startTime; 
     editor.putLong("totalTime",totalTime); 
     int hours = (int) ((totalTime/(1000*60*60)) % 24); 
     int minutes = (int) ((totalTime/(1000*60)) % 60); 
     int seconds = (int) (totalTime/1000) % 60; 
     displayTime.setText(String.valueOf(hours)+ ":"+String.valueOf(minutes)+":"+ String.valueOf(seconds)); 
     startEndButton.setText("Start Timer"); 
     editor.putString("timerButton",(String)startEndButton.getText()); 
     editor.commit(); 
     running = false; 
    } 
} 

public void neuerKunde(View view) { 
    Intent intent = new Intent(this, AddKunde.class); 
    startActivity(intent); 
} 

public long getTime() { 
    long millis = System.currentTimeMillis(); 
    return millis; 
} 


public void createDropDown() { 

    if(kunden.size() > 0) { 
     Spinner spinner = (Spinner) findViewById(R.id.chooseCustomer); 
     ArrayList<String> names = new ArrayList<>(); 
     for(Kunde k:kunden) { 
      names.add(k.getName()); 
     } 
     // Create an ArrayAdapter using the string array and a default spinner layout 
     ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, names); 

     // Specify the layout to use when the list of choices appears 
     adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
     // Apply the adapter to the spinner 
     spinner.setAdapter(adapter); 
    } 
} 

}

類 「AddCusomter」

package com.example.cmsolutions.zeiterfassung; 

import android.content.Intent; 
import android.os.Bundle; 
import android.support.design.widget.FloatingActionButton; 
import android.support.design.widget.Snackbar; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.View; 
import android.widget.EditText; 

import java.util.LinkedList; 

public class AddKunde extends AppCompatActivity { 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_add_kunde2); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
    fab.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
        .setAction("Action", null).show(); 
     } 
    }); 
    getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
} 



public void addKunde(View view) throws Exception { 
    try { 
     EditText strings = (EditText) findViewById(R.id.customerName); 
     String name = strings.getText().toString(); 
     strings = (EditText) findViewById(R.id.addressField); 
     String address = strings.getText().toString(); 
     Kunde customer = new Kunde(name,address); 
     ZeitErfassen.kunden.add(customer); 
    } catch (Exception e) { 
     throw new Exception("Fehler in addKunde!"); 
    } 
    startActivity(new Intent(this,ZeitErfassen.class)); 
} 

}

我只是意識到,也許其因爲在方法addKunde的端部( )我再次啓動MainActivity?

PS:我覺得我也可以提高我的編碼風格。如果您有關於更好編碼的技巧(針對其他課程的方法,....),那麼im也很有用!謝謝!

回答

0

而不是開始活動,你可以完成活動。

//startActivity(new Intent(this,ZeitErfassen.class)); 
finish(); 

檢查共享pref是XML是使用ddms存儲值。

開放共享的首選項,只有當你需要

//OnCreate 

    app_preferences = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE); 
    Editor editor = pref.edit(); 

public void startTimer(View view) { 
    SharedPreferences.Editor editor = = app_preferences.edit(); 
    if(running == false) { 
    startTime = getTime(); displayTime.setText("Zeitstoppung läuft"); 
    editor.putString("zeitAnzeige",(String)displayTime.getText()); 
    running = true; 
    editor.putBoolean("running",true); editor.putLong("startTimer", startTime); 
    startEndButton.setText("End Timer"); 
    editor.putString("timerButton", (String)startEndButton.getText()); 
    editor.commit(); 
    } else { 
    endTime = getTime(); 
    editor.putLong("endTime",endTime); totalTime = endTime - startTime; 
    editor.putLong("totalTime",totalTime); 
    int hours = (int) ((totalTime/(1000*60*60)) % 24); int minutes = (int) ((totalTime/(1000*60)) % 60); 
    int seconds = (int) (totalTime/1000) % 60; 
    displayTime.setText(String.valueOf(hours)+ ":"+String.valueOf(minutes)+":"+ String.valueOf(seconds)); 
    startEndButton.setText("Start Timer"); editor.putString("timerButton",(String)startEndButton.getText()); 
    editor.commit(); 
    running = false; 
    } 
} 
+0

我通過手機發布的代碼不正確對齊 –

+0

感謝您的答覆!完成()幫了很多! –

+0

如果你滿意請接受這個答案和馬克它會幫助我提高自己 –