Tải bản đầy đủ (.pptx) (28 trang)

Lập trình android từ A đến Z chương 12 intent

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (1.36 MB, 28 trang )







 !!""!#"
12-2

$"%%



&''(''"
)'&&&#intents"

*+,


-.


/
01
/

01
/
/
2
$"%%



2

)'(3(&'4
"
&&5"
&6'&"
-."

7
$"%%

7

&#6
'6&
"
' !"'
"
&&'#"
8
$"%%

8

9("
&''"
9'putXXXgetXXX0&1'&'!"
:#
Bundle myBundle = new Bundle();

myBundle.putDouble (“var1”, 3.1415);

Double v1 = myBundle.getDouble(“var1”);
&$;(0&$"6&"1<
&$;(01<
&$"0=$>6$21<
&$"?#0&$1<
&:@0&$6$$1<
A
$"%%

A
 
!" !
9?9
B01

?#*$;#,
C!@
D
$"%%

D
 
!" !
9?9
B01

?#*$;#,
C!@

&E;01<
&;&E"?#01<
$;&"0=$F1<
G
$"%%

G
 
!" !
9?9
B01
0$%1
?#*$;&'(,
C!@
&"C0F$F678A1<
&E"?#0&1<
@0&"#$%&'()*"&+
H
$"%%

H
!)*+!,,+--,.,,,-)
$I
$"%%

$I
/-01)2
&$('&'"94'&
&$"
$$

$"%%

$$
/-01)2
"+-J'&$0$"#1
KL#;,-.,/,01,23
KEE&#;,45!!!6!!,
;,,&M(;,7(,
&M;,7(,3
K9#N(
#;,-,
#CO;,88,
4;,9: :,
&M(;,7(,
&M;,(,!3
K?9#
;,#7;,
;,<=!#'> ,
&M(;,7(,
&M;,(,
9&;,;?@;$@;,!3
K?9#
;,$,
;,<=!#'>.8,
&M(;,7(,
&M;,(,
9&;,;,!3
K 
#;,A,
;,<=!;,

&M(;,(,
&M;,(,!3
K9#N(
4;,9: :,
#;,$,
#CO;,81,
;,<=!'>A ,
&M(;,7(,
&M;,(,!3
K!EE&P
3"9!+"Q6
("
$
$"%%

$
/-01)2
"+-J'&0"#1
KL#;,-.,/,01,23
KEE&#;,45!!!6!!,
;,,
&M(;,7(,
&M;,7(,
4;,9:111111,3
K9#N(
#;,8,
#CO;,88,
4;,9: :,
&M(;,7(,
&M;,(,!3

K?9#
#;,?,
;,<=!?,
&M(;,7(,
&M;,(,!3
K 
#;,?;6,
;,<=!;?,
&M(;,(,
&M;,(,!3
K!EE&P
$2
$"%%

$2
/-01)2
"+#-&$"R4 6'&"(''
&"
package cis493.matos.intents6;
// Activity1
// get input data from user, call Activity2, show result
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.EditText;
import android.widget.TextView;
public class Activity1 extends Activity {

EditText txtVal1;
EditText txtVal2;
TextView lblResult;
Button btnAdd;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
txtVal1 = (EditText)findViewById(R.id.EditText01);
txtVal2 = (EditText)findViewById(R.id.EditText02);
lblResult = (TextView) findViewById(R.id.TextView01);

btnAdd = (Button) findViewById(R.id.btnAdd);
btnAdd.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// get values from the UI
Double v1 = Double.parseDouble(txtVal1.getText().toString());
Double v2 = Double.parseDouble(txtVal2.getText().toString());
// create intent to call Activity2
Intent myIntentA1A2 = new Intent (Activity1.this,
Activity2.class);
// create a container to ship data
Bundle myData = new Bundle();
// add <key,value> data items to the container
myData.putDouble("val1", v1);
myData.putDouble("val2", v2);
// attach the container to the intent
myIntentA1A2.putExtras(myData);

// call Activity2, tell your local listener to wait response
startActivityForResult(myIntentA1A2, 101);
}//onClick
});
}//onCreate
///////////////////////////////////////////////////////////////////// local listener receiving callbacks from other activities
@Override
protected void onActivityResult(int requestCode,
int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try{
if ((requestCode == 101 ) && (resultCode == Activity.RESULT_OK)){
Bundle myResults = data.getExtras();
Double vresult = myResults.getDouble("vresult");
lblResult.setText("Sum is " + vresult);
}
}
catch (Exception e) {
lblResult.setText("Oops! - " + requestCode + " " + resultCode);
}
}//onActivityResult

}//Activity1
$7
$"%%

$7
/-01)2
"+&-&"'&$"?#' "'""
@ST"

package cis493.matos.intents6;
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.EditText;
public class Activity2 extends Activity
implements OnClickListener{
EditText dataReceived;
Button btnDone;
@Override
protected void onsuper.onCreate(savedInstanceState);
setContentView(R.layout.main2);
dataReceived = (EditText) findViewById(R.id.etDataReceived);
btnDone = (Button) findViewById(R.id.btnDone);
btnDone.setOnClickListener(this);
Create(Bundle savedInstanceState) {
// pick call made to Activity2 via Intent
Intent myLocalIntent = getIntent();
// look into the bundle sent to Activity2 for data items
Bundle myBundle = myLocalIntent.getExtras();
Double v1 = myBundle.getDouble("val1");
Double v2 = myBundle.getDouble("val2");
// operate on the input data
Double vResult = v1 + v2;
// for illustration purposes. show data received & result
dataReceived.setText("Data received is \n"
+ "val1= " + v1 + "\nval2= " + v2

+ "\n\nresult= " + vResult);
// add to the bundle the computed result
myBundle.putDouble("vresult", vResult);
// attach updated bumble to invoking intent
myLocalIntent.putExtras(myBundle);
// return sending an OK signal to calling activity
setResult(Activity.RESULT_OK, myLocalIntent);
}//onCreate
@Override
public void onClick(View v) {
// close current screen - terminate Activity2
finish();
}//onClick
}//Activity2
$8
$"%%

$8
/-01)2
"+'-U'"(K&P'=&=
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=" /> package="cis493.matos.intents6"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Activity1"
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=".Activity2">
</activity>

</application>
<uses-sdk android:minSdkVersion="4" />
</manifest>

$A
$"%%

$A
01+&$4&"'4'("
'>
B6
!!4Q&
!!'&
+427H2"<

+""&<
+""<
+""<
+"("N(<
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