package com.intentpage;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class IntentPageActivity extends Activity {
/** Called when the activity is first created. */
EditText edi1,edi2;
@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);
}
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){
}
//建立Bundle物件,並讓Bundle攜帶資料
Bundle bundle=new Bundle();
//putXXX("key",values)
bundle.putInt("onekey", one);
bundle.putInt("twokey", two);
//建立Intent物件,第一個參數為此活動頁面,第二個參數為目標活動頁面
Intent intent=new Intent(IntentPageActivity.this,Page2.class);
//使用putExtras()方法讓Intent攜帶Bundle物件一起到目標活動頁面
intent.putExtras(bundle);
startActivity(intent);
}
}
---------------------------------------------------------------------------------------------------------
在manifest中必須建立此頁面
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".IntentPageActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Page2"></activity>
</application>
------------------------------------------------------------------------------------------------------------
package com.intentpage;
import android.app.Activity;
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;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//要將page2的XML layout與結合
setContentView(R.layout.page2);
tex=(TextView)findViewById(R.id.textView);
but=(Button)findViewById(R.id.bittonpage2);
/*當透過Intent傳到新的Activity後,只要使用Activity.getIntent()就可以得到傳過來
的 Intent物件,接著再使用gerExtras()就可以得到Intent物件所附帶的Bundle物件*/
Bundle bundle=this.getIntent().getExtras();
int one=0,two=0,mod=0;
//取回Bundle中攜帶的資料,以Key值來取回
one=bundle.getInt("onekey");
two=bundle.getInt("twokey");
int one1=one;int two1=two;
while(two!=0){
mod=one%two;
one=two;
two=mod;
}
tex.setText(String.format("%d與%d之最大公因數為%d", one1,two1,one));
but.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
// TODO Auto-generated method stub
Page2.this.finish();
}
});
}
}
沒有留言:
張貼留言