2011-11-14 27 views
1

我想獲得一個時間戳成一個文本視圖或任何視圖的活動,將提供時間,從一個按鈕打開活動的時間加上顯示輸入從兩個編輯文本到文本視圖的單獨活動。例如:從設備獲取時間戳以顯示在textview中?

活動Main

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 

public class MainActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     Button b = (Button) findViewById(R.id.button1); 
     b.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View v) { 

      } 
     }); 
    } 
} 

包含該XML佈局和視圖:

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

    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/hello" /> 

    <EditText 
     android:id="@+id/editText1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" > 

     <requestFocus /> 
    </EditText> 

    <EditText 
     android:id="@+id/editText2" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" /> 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Button" /> 

</LinearLayout> 

現在我想從兩個編輯文本的用戶輸入而打開此下一個活動使用按鈕上的onClick加上顯示點擊按鈕時的時間戳:

import android.app.Activity; 
import android.os.Bundle; 

public class Main2 extends Activity{ 

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


} 

哪個膨脹這個XML佈局:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 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/textView1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="TextView" android:textSize="20dp"/> 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="TextView" android:textSize="20dp"/> 

    <TextView 
     android:id="@+id/textView3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="TextView" /> 

</LinearLayout> 

所以,我要實現以下目標:

  editText1  ----------->   textView1 
     editText2  ----------->   textView2 
         Phone's time stamp  textview3 

和開放與按鈕的點擊MAIN2活動。因此,editText1是一個EditText,它將接受用戶輸入並將該值返回到textView1中,對editText2和textView2也是如此,但最重要的是我想要點擊按鈕的時間以及返回到textView3的時間。

現在我知道如何將一個edittext的值返回到另一個activity中的textview,但不是兩個並獲取時間戳。

LAS_VEGAS OK,我得到了兩個標識符的工作,但得到對這裏的時間戳的錯誤是我儘可能代碼:

這是我的第一個活動的onClick方法:

EditText edit = (EditText) findViewById(R.id.etTitle); 
      EditText editP = (EditText) findViewById(R.id.etPrice); 
      long currentTime = System.currentTimeMillis(); 

      Intent intent = new Intent(Post.this, PostSet.class); 
      intent.putExtra("com.main.espress.POSTSET",       `edit.getText().toString());` 
      intent.putExtra("com.main.espress.NEW",  editP.getText().toString()); 
      intent.putExtra("currentTime", currentTime); 
      startActivity(intent); 

這是我的第二個活動有:

TextView tv1 = (TextView) findViewById (R.id.tvTitleText); 
     TextView tv2 = (TextView) findViewById (R.id.tvPrice); 
     TextView tv3 = (TextView) findViewById (R.id.tvTimeStamp); 

     Intent intent = getIntent(); 
     String edit = intent.getStringExtra("com.main.express.POSTSET"); 
     String editP = intent.getStringExtra("com.main.espress.NEW"); 
     long currentTime = extras.getLong("currentTime"); 

     tv1.setText(edit); 
     tv2.setText(editP); 
     tv3.setText(currentTime); 
+0

檢查此示例:[Android - 獲取當前日期和時間](http://goo.gl/tHqrV) –

回答

0

可以使用Intent傳遞活動之間數據的多個值:

onClick()

EditText et1 = (EditText) findViewById(R.id.editText1); 
EditText et2 = (EditText) findViewById(R.id.editText2); 
long currentTime = System.currentTimeInMillis(); 

Intent i = new Intent(MainActivity.this, Main2.class); 
i.putExtra("editText1", et1.getText()); 
i.putExtra("editText2", et2.getText()); 
i.putExtra("currentTime", currentTime); 
startActivity(i); 

在Activitiy Main2

Bundle extras = getIntent().getExtras(); 
String text1 = extras.getString("editText1"); 
String text2 = extras.getString("editText2"); 
long currentTime = extras.getLong("currentTime"); 
+0

Ohhhhhhh好吧,看起來是正確的,比我想象的更簡單,酷我會試一試,讓你知道,我可以使時間戳顯示像他們的格式使用在這裏就像發佈13小時前或發佈10分鐘前的格式,而不是發佈2:30:23 ?? – mandam

+1

是的,你可以但那是另一個問題... – Caner

2

就可以得到當前的時間我N使用您的按鈕點擊()此行米利斯:

long currentTime=System.currentTimeInMillis(); //getting current time in millis 
//converting it into user readable format 
Calendar cal=Calendar.getInstance(); 
cal.setTimeInMillis(currentTime); 
String showTime=String.format("%1$tI:%1$tM:%1$tS %1$Tp",cal);//shows time in format 10:30:45 am