2014-01-17 21 views
0

學習ADT,從C/HTML/CSS/JS背景的主要在Xcode和Linux(納米優選的)。我遇到了一些麻煩,希望能夠得到一些關於如何在這些非常簡單的第一步中解決問題的指導。轉換常見的Android倒數計時器例如接受用戶輸入

我與Wagied戴維斯的「Android食譜」的例子很常見的變化之一工作:

http://androidbite.blogspot.com/2012/11/android-count-down-timer-example.html

我的自我分配的任務就是以此爲基礎,簡單的蛋計時器(而不是固定的30秒)的應用程序(追溯到升級Froyo),其中用戶輸入他們希望在一個EditText從向下計數的時間

我作了如下改動的佈局:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 
<TextView 
    android:id="@+id/instruction" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:text="Enter countdown time:" 
    android:textSize="25sp" /> 

<TextView 
    android:id="@+id/timer" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:layout_centerVertical="true" 
    android:paddingRight="10dip" 
    android:textSize="50dp" /> 

<Button 
    android:id="@+id/button" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true" 
    android:padding="10dip" 
    android:text="Start" /> 
<--Here's the part I added, SO!--> 
<EditText 
    android:id="@+id/time" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/instruction" 
    android:layout_centerHorizontal="true" 
    android:ems="10" 
    android:imeOptions="actionGo" 
    android:inputType="number" /> 

</RelativeLayout> 

但是,主要的java活動的下一步沒有迴避。我從private final long改變startTime的定義public long,並已基本上一直用這樣的代碼塊:

//get countdown interval from user 
EditText myEdit = (EditText) findViewById(R.id.time); 
//convert edit text to string 
String numString = myEdit.getText().toString(); 
//convert string to integer 
myInt = Integer.parseInt(numString); 
//convert integer to long number of milliseconds 
long localStartTime = (Long.valueOf(myInt) * 1000); 

我試圖使它在活動文件中它自己的類並返回localStartTime 然後分配那startTime,從onCreate崩潰,onClick(它只從最初定義的值startTime倒數)我也試過它作爲onCreate直接代碼片段,所以最後一行看起來有點像: startTime = (Long.valueOf(myInt) * 1000); CA之前灌裝countDownTimer = new MyCountDownTimer(startTime, interval);但這又只是讓你輸入的文本,但不會從我想我已經分配給startTime變量倒計時。

我也試過在肯狼的答案在這裏的變化: Android java EditText to int 放置嘗試和副漁獲物在onCreate但同樣可以進入一個時間,而不從中倒計時當你點擊啓動按鈕。

如果您選擇已經閱讀這一切,謝謝你,你專注於兩件事情在回答這個問題之前我想請你。首先,就像一個經驗豐富的Java程序員看起來那麼簡單,你會如何處理這個問題?其次,關於Android Java的思考方式有哪些,我從C/JavaScript思維方式中忽略了哪些方式?

謝謝。

回答

0

,我發現因爲我與倒數計時器有另一個問題,你的問題。如果您只是想讓計時器使用用戶輸入,那麼下面的開始時間可能會有用。倒數計時器顯然只接受長時間作爲開始時間。

EditText timetocountdown = (EditText)findViewById(R.id.userinput); 
      String tmrstring = timetocountdown.getText().toString(); 

      Long A = Long.parseLong(tmrstring);   
      startTime = (A * 60000); 
      countDownTimer = new MyCountDownTimer(startTime, interval); 
      countDownTimer.start();   
      timerHasStarted = true;