2012年7月16日 星期一

Android:要求被呼叫的Activity回傳資料

記得要先到manifest中註冊新頁面


package com.intentpage;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class IntentPageActivity extends Activity {
    /** Called when the activity is first created. */
EditText edi1,edi2;
TextView show;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        edi1=(EditText)findViewById(R.id.editText1);
        edi2=(EditText)findViewById(R.id.editText2);
        show=(TextView)findViewById(R.id.textView2);
    }
    //第一個按鈕到page2
    public void butonclick(View v){
    int one=0,two=0;
    try{
    one=Integer.parseInt(edi1.getText().toString());
    two=Integer.parseInt(edi2.getText().toString());
    }catch(Exception e){
   
    }
   
    Intent intent=new Intent(IntentPageActivity.this,Page2.class);
    intent.putExtra("onekey", one);
    intent.putExtra("twokey", two);
   
   
    //要求對方一定要傳回值,requestCode為100
    startActivityForResult(intent,100);
    }
   //第二個按鈕,將1000數字傳到page3

    public void onclick(View v){
     Intent page3Intent=new Intent(this,page3.class);
     Bundle bundle=new Bundle();
     bundle.putInt("key", 1000);
     page3Intent.putExtras(bundle);
      //要求對方一定要傳回值,requestCode為103
     startActivityForResult(page3Intent,103);
    }

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
               //使用requestCode來判斷傳回的物件是從哪個Activity所傳回的
switch(requestCode){
case 100:
if(resultCode==RESULT_OK){
Bundle bundle=data.getExtras();
int result=bundle.getInt("replykey");
show.setText("等於"+result);
break;
}

case 103:
if(resultCode==RESULT_OK){
Bundle bundle=data.getExtras();
int resultpage3=bundle.getInt("page3key");
show.setText("等於"+resultpage3);
break;
}

}

}
 
}

package com.intentpage;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class Page2 extends Activity {
TextView tex;
Button but;
int result;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.page2);
tex=(TextView)findViewById(R.id.textView);
but=(Button)findViewById(R.id.bittonpage2);
//由於從上個Activity並沒有使用Bundle物件來傳遞資料,因此不用getExtras( )來取得
                Bundle物件
               Intent obj=this.getIntent();


int one=0,two=0,mod=0;

        //Intent.getXXXExtra("key",default value)
one=obj.getIntExtra("onekey", 0);
two=obj.getIntExtra("twokey", 0);

int one1=one;int two1=two;
while(two!=0){
mod=one%two;
one=two;
two=mod;
}
result=one;
tex.setText(String.format("%d與%d之最大公因數為%d", one1,two1,one));
but.setOnClickListener(new OnClickListener(){

public void onClick(View arg0) {
// TODO Auto-generated method stub

//已經知道是要傳回哪個頁面,所以就不用標示來源目的了
Intent replyIntent=new Intent();
Bundle bundle=new Bundle();
bundle.putInt("replykey", result);
replyIntent.putExtras(bundle);
                                //回傳帶有資料的Intent物件以及ResultCode,ResultCode常見為RESULT_OK
                                   或是RESULT_CANCEL
setResult(RESULT_OK,replyIntent);
Page2.this.finish();
}

});
}


}

package com.intentpage;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import android.view.View;

public class page3 extends Activity {
int page3;
int x;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.page3);
Bundle bundle=this.getIntent().getExtras();
x=bundle.getInt("key");
((TextView)findViewById(R.id.textViewpage3)).setText("傳來值為"+x);
}

public void onclick(View v) {
Intent page3Intent=new Intent();
Bundle bundle=new Bundle();
page3=x+1000;
bundle.putInt("page3key",page3 );
page3Intent.putExtras(bundle);
setResult(RESULT_OK,page3Intent);
page3.this.finish();

}
}


沒有留言:

張貼留言