2014年12月1日 星期一

[Android Sample Code] Activities之間的切換與資料傳遞 (文字與圖片)

一個可與使用者進行互動的畫面程序在Android裡稱為Activity由於一個畫面代表一個Activity,所以一個App會有一至多個Activities若想將目前執行中的Activity暫停,然後啟動其它Activities則是得透過Intents來達成,更正確的來說每個Activity都是被某一Intent所啟動,另外Activities之間的資料傳遞也可以也可以靠Intent來達成。本範例實作兩個Activity,Fig. 1顯示MainActivity的介面,只有一個按鈕物件用來提示使用者進行操作,當按下按鈕後Activity會透過Intent傳遞文字與圖片給SecMainActivity,MainActivity的程式碼顯示於下,(待補後續講解)


MainActivity.java:
package com.example.activitiestest;

import java.io.ByteArrayOutputStream;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends ActionBarActivity {
 private Button button;
 private Intent intent ;
 public final static String EXTRA_STRING_MSG = "EXTRA_STRING_MSG";
 public final static String EXTRA_BITMAP_BYTE_STREAM = "EXTRA_BITMAP_BYTE_STREAM";
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViews();
        createIntent();
        setActionListener();
    }
    
    private void findViews(){
     button = (Button) findViewById(R.id.button);
    }
    
    private void createIntent(){
     Bitmap resImage;
     ByteArrayOutputStream outImageByteArrayOutputStream = new ByteArrayOutputStream(); 
     byte [] outImageByteArray;
     
     intent = new Intent(this,SecMainActivity.class);
        intent.putExtra(EXTRA_STRING_MSG, "Hello, another activity");
        resImage = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
        resImage.compress(Bitmap.CompressFormat.JPEG, 100, outImageByteArrayOutputStream);
        outImageByteArray = outImageByteArrayOutputStream.toByteArray();
        intent.putExtra(EXTRA_BITMAP_BYTE_STREAM, outImageByteArray);
    }
    
    private void setActionListener(){
      button.setOnClickListener(new OnClickListener() {  
          @Override  
          public void onClick(View v) {   
           startActivity(intent);
          }
         });
    }
}

Fig. 1. MainActivity Layout.
在Eclipse下建立SecMainActivity的步驟如Fig.2、3和4所示:
Fig. 2. 建立新的Activity (1)
 Fig. 3. 建立新的Activity (2)

 Fig. 4. 建立新的Activity (3)

SecMainActivity的介面圖示於Fig. 5,分別有一個TextView、ImageView和Button,其中TextView和ImageView用來顯示接收到的文字和圖片,Button則是用來返回先前的MainActivity,

SecMainActivity.java:
package com.example.activitiestest;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class SecMainActivity extends ActionBarActivity {
 private Button returnButton;
 private TextView receivedTextView;
 private ImageView receivedImageView;
 Intent intent;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_sec_main);
  findViews();
  handleReceivedInfoToUI();
  setActionListener();
 }
 
 private void findViews(){
  returnButton = (Button) findViewById(R.id.returnButton);
  receivedTextView = (TextView) findViewById(R.id.receivedTextView);
  receivedImageView = (ImageView) findViewById(R.id.receivedImageView);  
 }
 
 private void handleReceivedInfoToUI(){
  intent = getIntent();
  receivedTextView.setText(intent.getStringExtra(MainActivity.EXTRA_STRING_MSG));
  byte [] receivedImageByteArray = intent.getByteArrayExtra(MainActivity.EXTRA_BITMAP_BYTE_STREAM);
  Bitmap bmp = BitmapFactory.decodeByteArray(receivedImageByteArray, 0, receivedImageByteArray.length);
  receivedImageView.setImageBitmap(bmp);
 }
 
 private void setActionListener(){
  returnButton.setOnClickListener( new OnClickListener(){
   @Override
   public void onClick(View v){
    finish();
   }
  });
 }
}


Fig. 5. SecMainActivity Layout.
當在MainActivity按下Call Second Activity後的執行結果如下:
Fig. 6. 按下Call Second  Activity後的執行結果

之後按下Return to MainActivity後則會再返回Fig. 1。

延伸閱讀:

沒有留言:

張貼留言