Bài 24: Intent Trong Android(P4)

Leave a Comment
- Tiến hành xem coding của MainActivity.java:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package tranduythanh.com;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
 
public class MainActivity extends Activity {
 EditText txta,txtb;
 Button btnketqua;
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 txta=(EditText) findViewById(R.id.txta);
 txtb=(EditText) findViewById(R.id.txtb);
 btnketqua=(Button) findViewById(R.id.btnketqua);
 btnketqua.setOnClickListener(new View.OnClickListener() {
 public void onClick(View arg0) {
 //Tạo Intent để mở ResultActivity
 Intent myIntent=new Intent(MainActivity.this, ResultActivity.class);
 //Khai báo Bundle
 Bundle bundle=new Bundle();
 int a=Integer.parseInt(txta.getText().toString());
 int b=Integer.parseInt(txtb.getText().toString());
 //đưa dữ liệu riêng lẻ vào Bundle
 bundle.putInt("soa", a);
 bundle.putInt("sob", b);
 //Đưa Bundle vào Intent
 myIntent.putExtra("MyPackage", bundle);
 //Mở Activity ResultActivity
 startActivity(myIntent);
 }
 });
 }
 public boolean onCreateOptionsMenu(Menu menu) {
 // Inflate the menu; this adds items to the action bar if it is present.
 getMenuInflater().inflate(R.menu.activity_main, menu);
 return true;
 }
}

- Tôi đã giải thích mã lệnh trong đoạn code ở trên rồi.
- Ở dòng 31, key nhận dạng Bundle là MyPackage, bên ResultActivity sẽ dựa vào key này để lấy Bundle ra.
- Sau khi có Bundle rồi thì dựa vào soasob ở dòng lệnh 28,29 để lấy đúng dữ liệu.
- Bây giờ ta qua giao diện ResultActivity:
24_intent_10- Bạn xem XML Resource của ResultActivity:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/LinearLayout1"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical"
 tools:context=".ResultActivity" >
 
<TextView
 android:id="@+id/textView1"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:gravity="center"
 android:text="Kết quả giải phương trình bậc 1" />
 
<TextView
 android:id="@+id/txtketqua"
 android:layout_width="match_parent"
 android:layout_height="50sp"
 android:gravity="center"
 android:background="#CCFFD9"
 />
 
<Button
 android:id="@+id/btnBack"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_gravity="center"
 android:text="Back" />
 
</LinearLayout>
- Tiếp tục xem xử lý coding của ResultActivity:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package tranduythanh.com;
 
import java.text.DecimalFormat;
 
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
 
public class ResultActivity extends Activity {
 TextView txtketqua;
 Button btnBack;
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_result);
 btnBack=(Button) findViewById(R.id.btnBack);
 txtketqua=(TextView) findViewById(R.id.txtketqua);
 //lấy intent gọi Activity này
 Intent callerIntent=getIntent();
 //có intent rồi thì lấy Bundle dựa vào MyPackage
 Bundle packageFromCaller=
 callerIntent.getBundleExtra("MyPackage");
 //Có Bundle rồi thì lấy các thông số dựa vào soa, sob
 int a=packageFromCaller.getInt("soa");
 int b=packageFromCaller.getInt("sob");
 //tiến hành xử lý
 giaipt(a, b);
 btnBack.setOnClickListener(new View.OnClickListener() {
 public void onClick(View v) {
 finish();
 }
 });
 }
 public void giaipt(int a,int b)
 {
 String kq="";
 if(a==0 && b==0)
 {
 kq="Vô số nghiệm";
 }
 else if(a==0 && b!=0)
 {
 kq="Vô nghiệm";
 }
 else
 {
 DecimalFormat dcf=new DecimalFormat("0.##");
 kq=dcf.format(-b*1.0/a);
 }
 txtketqua.setText(kq);
 }
 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
 // Inflate the menu; this adds items to the action bar if it is present.
 getMenuInflater().inflate(R.menu.activity_result, menu);
 return true;
 }
}
-Bạn xem dòng lệnh 22: Intent callerIntent=getIntent(); lệnh này cho phép lấy Intent start Activity này. Tức là lấy Intent mà ta khai báo bên MainActivity để start ResultActivity.
- Có được Intent này rồi thì mọi thứ bạn làm như  Tôi giải thích trong Coding ở trên.
- Bạn có thể tải coding mẫu ở đây:http://www.mediafire.com/?vb8ddcoos7dupv7

0 nhận xét:

Đăng nhận xét