![Phân tích tư tưởng của nhân dân qua đoạn thơ: Những người vợ nhớ chồng… Những cuộc đời đã hóa sông núi ta trong Đất nước của Nguyễn Khoa Điềm](https://timtailieu.net/upload/document/136415/phan-tich-tu-tuong-cua-nhan-dan-qua-doan-tho-039-039-nhung-nguoi-vo-nho-chong-nhung-cuoc-doi-da-hoa-song-nui-ta-039-039-trong-dat-nuoc-cua-nguyen-khoa-136415.jpg)
Giáo trình hình thành quy trình phân tích nguyên lý lập trình cơ bản với Androi p4
Số trang: 10
Loại file: pdf
Dung lượng: 695.21 KB
Lượt xem: 13
Lượt tải: 0
Xem trước 2 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
Tham khảo tài liệu giáo trình hình thành quy trình phân tích nguyên lý lập trình cơ bản với androi p4, công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả
Nội dung trích xuất từ tài liệu:
Giáo trình hình thành quy trình phân tích nguyên lý lập trình cơ bản với Androi p4B3: Tạo giao diện cho Activity2 -> Chuột phải vào folder reslayout -> New ->Android XML File ->Gõ tên là activity2_layout.xmlMã: Layout của Activity2 tương tự như Activity1, nhưng Button bây giờ là để gọiBroadCast Receiver. Ngoài ra mình dùng EditText để hiển thị value nhận được (donó có cái đường bao ngoài đẹp hơn TextView ^_^) nên không cho phép nhập giátrị vào EditText nàyMã:android:enabled=falseB4:Sửa lại nội dung của Activity1.java như sau:Mã:package at.exam;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 Activity1 extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity1_layout); final EditText editValue = (EditText)findViewById(R.id.value_edit); final Button sendButton = (Button)findViewById(R.id.send_button); sendButton.setOnClickListener(newOnClickListener() { public void onClick(View v) { String valueString =editValue.getText().toString(); long value; if (valueString != null) { value =Long.parseLong(valueString); } else { value = 0; } //Tạo 1 đối tượng Bundle để gửiđi cùng Intent Bundle sendBundle = new Bundle(); sendBundle.putLong(value,value); //Tạo Intent để khởi chạyActivity2 và gắn sendBundble vào Intent Intent i = newIntent(Activity1.this, Activity2.class); i.putExtras(sendBundle); startActivity(i); //Giải phóng Activity1 khỏiActivity Stack vì ta sẽ ko quay lại nó nữa finish(); } }); }}B5: Tạo mới 1 Class Activity2.java trong package at.exam -> chỉnh sửa nội dung:Mã:package at.exam;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 { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity2_layout); final EditText receiveValueEdit = (EditText)findViewById(R.id.value_receive); final Button callReceiverButton = (Button)findViewById(R.id.call_button); //Lấy về Bundle được gửi kèm Intent rồi lấy ragiá trị Bundle receiveBundle =this.getIntent().getExtras(); final long receiveValue =receiveBundle.getLong(value);receiveValueEdit.setText(String.valueOf(receiveValue)); callReceiverButton.setOnClickListener(newOnClickListener() { public void onClick(View v) { //Khởi tạo 1 Intent để gửi tớiBroadCast Receiver //Gắn giá trị vào Intent, lần nàyko cần Bundle nữa Intent i = newIntent(Activity2.this, Receiver.class); i.putExtra(new value,receiveValue - 10); sendBroadcast(i); } }); }}B6: Tạo BroadCast Receiver để nhận Intent mà Activity2 gửi tới -> Tạo 1 fileReceiver.java trong at.exam -> Nội dung:Mã:package at.exam;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.widget.Toast;public class Receiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intentintent) { long value = intent.getLongExtra(newvalue, -10) + 10; Toast toast = Toast.makeText(context,Broadcast Receiver catch an Intent + + The value is stored inthe Intent is + String.valueOf(value),Toast.LENGTH_LONG); toast.show(); }}Code không hề khó hiểu, và mình cũng đã add comment. Chỉ cần lưu ý ở đây làToast là lớp để hiển thị một thông báo đơn giản trong 1 khoảng thời gian cố định,và ko thể thay đổi thời gian này T_T (why???) chỉ có thể chọn giữaLENGTH_SHORT với LENGTH_LONGB7: Bổ sung thêm thông tin về component mới vào AndroidManifest.xml:Mã: ...
Nội dung trích xuất từ tài liệu:
Giáo trình hình thành quy trình phân tích nguyên lý lập trình cơ bản với Androi p4B3: Tạo giao diện cho Activity2 -> Chuột phải vào folder reslayout -> New ->Android XML File ->Gõ tên là activity2_layout.xmlMã: Layout của Activity2 tương tự như Activity1, nhưng Button bây giờ là để gọiBroadCast Receiver. Ngoài ra mình dùng EditText để hiển thị value nhận được (donó có cái đường bao ngoài đẹp hơn TextView ^_^) nên không cho phép nhập giátrị vào EditText nàyMã:android:enabled=falseB4:Sửa lại nội dung của Activity1.java như sau:Mã:package at.exam;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 Activity1 extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity1_layout); final EditText editValue = (EditText)findViewById(R.id.value_edit); final Button sendButton = (Button)findViewById(R.id.send_button); sendButton.setOnClickListener(newOnClickListener() { public void onClick(View v) { String valueString =editValue.getText().toString(); long value; if (valueString != null) { value =Long.parseLong(valueString); } else { value = 0; } //Tạo 1 đối tượng Bundle để gửiđi cùng Intent Bundle sendBundle = new Bundle(); sendBundle.putLong(value,value); //Tạo Intent để khởi chạyActivity2 và gắn sendBundble vào Intent Intent i = newIntent(Activity1.this, Activity2.class); i.putExtras(sendBundle); startActivity(i); //Giải phóng Activity1 khỏiActivity Stack vì ta sẽ ko quay lại nó nữa finish(); } }); }}B5: Tạo mới 1 Class Activity2.java trong package at.exam -> chỉnh sửa nội dung:Mã:package at.exam;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 { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity2_layout); final EditText receiveValueEdit = (EditText)findViewById(R.id.value_receive); final Button callReceiverButton = (Button)findViewById(R.id.call_button); //Lấy về Bundle được gửi kèm Intent rồi lấy ragiá trị Bundle receiveBundle =this.getIntent().getExtras(); final long receiveValue =receiveBundle.getLong(value);receiveValueEdit.setText(String.valueOf(receiveValue)); callReceiverButton.setOnClickListener(newOnClickListener() { public void onClick(View v) { //Khởi tạo 1 Intent để gửi tớiBroadCast Receiver //Gắn giá trị vào Intent, lần nàyko cần Bundle nữa Intent i = newIntent(Activity2.this, Receiver.class); i.putExtra(new value,receiveValue - 10); sendBroadcast(i); } }); }}B6: Tạo BroadCast Receiver để nhận Intent mà Activity2 gửi tới -> Tạo 1 fileReceiver.java trong at.exam -> Nội dung:Mã:package at.exam;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.widget.Toast;public class Receiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intentintent) { long value = intent.getLongExtra(newvalue, -10) + 10; Toast toast = Toast.makeText(context,Broadcast Receiver catch an Intent + + The value is stored inthe Intent is + String.valueOf(value),Toast.LENGTH_LONG); toast.show(); }}Code không hề khó hiểu, và mình cũng đã add comment. Chỉ cần lưu ý ở đây làToast là lớp để hiển thị một thông báo đơn giản trong 1 khoảng thời gian cố định,và ko thể thay đổi thời gian này T_T (why???) chỉ có thể chọn giữaLENGTH_SHORT với LENGTH_LONGB7: Bổ sung thêm thông tin về component mới vào AndroidManifest.xml:Mã: ...
Tìm kiếm theo từ khóa liên quan:
giáo trình lập trình thủ thuật quản trị mạng kỹ năng lập trình phương pháp lập trình mẹo quản lập trìnhTài liệu liên quan:
-
Giáo trình Lập trình logic trong prolog: Phần 1
114 trang 205 0 0 -
Giáo trình Lập trình C căn bản
135 trang 176 0 0 -
Giáo trình Lập trình C căn bản: Phần 1
64 trang 170 0 0 -
Thiết kế mạch logic bằng Verilog - HDL
45 trang 170 0 0 -
Hướng dẫn lập trình với Android part 4
5 trang 156 0 0 -
14 trang 137 0 0
-
142 trang 130 0 0
-
Giáo trình lập trình hướng đối tượng - Lê Thị Mỹ Hạnh ĐH Đà Nẵng
165 trang 122 0 0 -
Bài giảng Phương pháp lập trình: Chương 9 - GV. Từ Thị Xuân Hiền
36 trang 115 0 0 -
information technology outsourcing transactions process strategies and contracts 2nd ed phần 3
65 trang 113 0 0