BÀI 1
MAIN [Link]
package [Link].baitap273;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
EditText editTextName;
Button btnSend;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
editTextName = findViewById([Link]);
btnSend = findViewById([Link]);
[Link](new [Link]() {
@Override
public void onClick(View v) {
String name = [Link]().toString();
Intent intent = new Intent([Link],
[Link]);
[Link]("user_name", name);
startActivity(intent);
}
});
}
}
ACTIVITY_MAIN.XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<EditText
android:id="@+id/editTextName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Nhập tên của bạn" />
<Button
android:id="@+id/btnSend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Gửi tên" />
</LinearLayout>
[Link]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:id="@+id/textViewGreeting"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Xin chào, "
android:textSize="18sp"
android:textStyle="bold"/>
<Button
android:id="@+id/btnReturn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Quay lại"
android:layout_marginTop="16dp"/>
</LinearLayout>
[Link]
package [Link].baitap273;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class secondactivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link]); // Sử dụng layout
[Link]
TextView textView = findViewById([Link]);
Button btnReturn = findViewById([Link]);
// Nhận dữ liệu từ Intent
String name = getIntent().getStringExtra("user_name");
if (name != null) {
[Link]("Xin chào, " + name);
}
// Quay lại MainActivity
[Link](new [Link]() {
@Override
public void onClick(View v) {
finish();
}
});
}
}
BÀI 2
Activity_main.XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ĐĂNG NHẬP"
android:textSize="24sp"
android:textStyle="bold"
android:gravity="center"
android:layout_gravity="center_horizontal"
android:paddingBottom="20dp"/>
<EditText
android:id="@+id/edtUsername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Tài khoản" />
<EditText
android:id="@+id/edtPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Mật khẩu"
android:inputType="textPassword" />
<Button
android:id="@+id/btnLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Đăng nhập"
android:layout_marginTop="10dp"/>
<TextView
android:id="@+id/tvResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textColor="#A90505"
android:gravity="center"
android:layout_marginTop="10dp"/>
</LinearLayout>
[Link]
package [Link].bai1thuchanh03;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
EditText edtUsername = findViewById([Link]);
EditText edtPassword = findViewById([Link]);
Button btnLogin = findViewById([Link]);
TextView tvResult = findViewById([Link]);
[Link](new [Link]() {
@Override
public void onClick(View v) {
String username = [Link]().toString().trim();
String password = [Link]().toString().trim();
if ([Link]("FIT") && [Link]("123")) {
[Link]("Đăng nhập THÀNH CÔNG!");
[Link](getResources().getColor([Link].holo_green_dark)
);
} else {
[Link]("Đăng nhập thất bại!");
[Link](getResources().getColor([Link].holo_red_dark));
}
}
});
}
}
BÀI 3
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Nhập số A =" />
<EditText
android:id="@+id/edtA"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Nhập số B =" />
<EditText
android:id="@+id/edtB"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number" />
<Button
android:id="@+id/btnTinh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Kết quả = "
android:textSize="18sp" />
<TextView
android:id="@+id/tvKetQua"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textStyle="bold" />
</LinearLayout>
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
EditText edtA, edtB;
Button btnTinh;
TextView tvKetQua;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
// Ánh xạ các thành phần giao diện
edtA = findViewById([Link]);
edtB = findViewById([Link]);
btnTinh = findViewById([Link]);
tvKetQua = findViewById([Link]);
// Xử lý khi nhấn nút
[Link](new [Link]() {
@Override
public void onClick(View v) {
// Lấy giá trị từ EditText và chuyển sang số nguyên
int soA = [Link]([Link]().toString());
int soB = [Link]([Link]().toString());
// Tính tổng
int tong = soA + soB;
// Hiển thị kết quả
[Link]([Link](tong));
}
});
}
}