Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 29

LẬP TRÌNH TRÊN THIẾT BỊ DI ĐỘNG

THỰC HÀNH LAB 3


Lab 3.1
Bài 1:
1/ Màn hình chạy ứng dụng
2/ Các file Java
a. File MainActivity.java
package edu.huflit.bt31;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


EditText edtUsername, edtPassword;
Button btnLogin, btnExit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edtUsername = findViewById(R.id.edtUsername);
edtPassword = findViewById(R.id.edtPassword);
btnLogin = findViewById(R.id.btnLogin);
btnExit = findViewById(R.id.btnExit);
btnExit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//
AlertDialog.Builder builder = new
AlertDialog.Builder(MainActivity.this);
builder.setTitle("Hỏi");
builder.setMessage("Đóng Ứng dụng nhé?");
builder.setCancelable(false);

//

builder.setNegativeButton("Hủy", new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface,
int i) {
dialogInterface.cancel();
}
});

builder.setPositiveButton("Đồng ý", new


DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface,
int i) {
finish();
}
});

//
builder.create().show();
}
});

btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String userName = edtUsername.getText().toString();
String passWord = edtPassword.getText().toString();

Intent intent = new Intent(MainActivity.this,


ChonChuDeActivity.class);

if (userName.contentEquals("admin") &&
passWord.contentEquals("admin@123"))
{
startActivity(intent);
return;
}
if (userName.contentEquals("admin") == false)
Toast.makeText(MainActivity.this, "Vui lòng kiểm tra
lại username", Toast.LENGTH_SHORT).show();
else if (passWord.contentEquals("admin@123") == false)
{
Toast.makeText(MainActivity.this, "Vui lòng kiểm tra
lại password", Toast.LENGTH_SHORT).show();
}
}
});
}
}

b. File ChonChuDeActivity.java
package edu.huflit.bt31;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

public class ChonChuDeActivity extends AppCompatActivity implements


View.OnClickListener{
Button btnExit2;
ImageButton imgBtn1, imgBtn2, imgBtn3, imgBtn4, imgBtn5;
TextView tv1, tv2, tv3, tv4, tv5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chon_chu_de);

//Find view
btnExit2 = findViewById(R.id.btnExit2);
imgBtn1 = findViewById(R.id.imgBtn1);
imgBtn2 = findViewById(R.id.imgBtn2);
imgBtn3 = findViewById(R.id.imgBtn3);
imgBtn4 = findViewById(R.id.imgBtn4);
imgBtn5 = findViewById(R.id.imgBtn5);
tv1 = findViewById(R.id.tv1);
tv2 = findViewById(R.id.tv2);
tv3 = findViewById(R.id.tv3);
tv4 = findViewById(R.id.tv4);
tv5 = findViewById(R.id.tv5);

imgBtn1.setImageResource(R.drawable.ic_launcher_background);
imgBtn2.setImageResource(R.drawable.ic_launcher_background);
imgBtn3.setImageResource(R.drawable.ic_launcher_background);
imgBtn4.setImageResource(R.drawable.ic_launcher_background);
imgBtn5.setImageResource(R.drawable.ic_launcher_background);
btnExit2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent starMain = new Intent(Intent.ACTION_MAIN);
starMain.addCategory(Intent.CATEGORY_HOME);
startActivity(starMain);
finish();
}
});

//Set event onClick for Image Button and Text View


imgBtn3.setOnClickListener(this);
tv3.setOnClickListener(this);

@Override
public void onClick(View view)
{
Intent intent = new Intent(ChonChuDeActivity.this,
ThemKhachHangActivity.class);
startActivity(intent);
}
}

c. File ThemKhachHangActivity.java
package edu.huflit.bt31;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class ThemKhachHangActivity extends AppCompatActivity {


EditText edtName, edtPhone, edtAddress;
Button btnSave, btnClear;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_them_khach_hang);

//Find view
edtName = findViewById(R.id.edtName);
edtPhone = findViewById(R.id.edtPhone);
edtAddress = findViewById(R.id.edtAddress);
btnSave = findViewById(R.id.btnSave);
btnClear = findViewById(R.id.btnClear);

//Open Activity
btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Create Intent
Intent intent = new Intent(ThemKhachHangActivity.this,
ThongTinKhachHangActivity.class);

//Create Bundle
Bundle bundle = new Bundle();

//Get data
bundle.putString("tenkh", edtName.getText().toString());
bundle.putString("sodt", edtPhone.getText().toString());
bundle.putString("diachi",
edtAddress.getText().toString());

//
intent.putExtra("MyData", bundle);

//
startActivity(intent);
}
});

//Clear data
btnClear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
edtName.setText("");
edtPhone.setText("");
edtAddress.setText("");
}
});
}
}

d. File ThongTinKhachHangActivity.java
package edu.huflit.bt31;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Button;

public class ThongTinKhachHangActivity extends AppCompatActivity {


TextView txtName, txtPhone, txtAddress;
Button btnReturn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_thong_tin_khach_hang);

//Find view
txtName = findViewById(R.id.txtName);
txtPhone = findViewById(R.id.txtPhone);
txtAddress = findViewById(R.id.txtAddress);
btnReturn = findViewById(R.id.btnReturn);
//Get Intent
Intent dataIntent = getIntent();

//Get Bundle
Bundle data = dataIntent.getBundleExtra("MyData");

txtName.setText(data.getString("tenkh"));
txtPhone.setText(data.getString("sodt"));
txtAddress.setText(data.getString("diachi"));

btnReturn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent returnActivity = new Intent(Intent.ACTION_VIEW);
returnActivity.addCategory(Intent.CATEGORY_HOME);
startActivity(returnActivity);
}
});
}
}

3/ Các file xml


a. File acitvity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/black"
tools:context=".MainActivity">

<TextView
android:id="@+id/tvUsername"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="200dp"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:textSize="20dp"
android:textColor="@color/white"
android:textStyle="bold"
android:gravity="left|center"
android:text="Tên đăng nhập:" />

<EditText
android:id="@+id/edtUsername"
android:layout_width="match_parent"
android:layout_height="25dp"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:ems="10"
android:inputType="text"
android:background="@color/white"
android:textColor="@color/black"/>
<TextView
android:id="@+id/tvPassword"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:textSize="20dp"
android:textColor="@color/white"
android:textStyle="bold"
android:gravity="left|center"
android:text="Mật khẩu:" />

<EditText
android:id="@+id/edtPassword"
android:layout_width="match_parent"
android:layout_height="25dp"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:ems="10"
android:inputType="textPassword"
android:background="@color/white"
android:textColor="@color/black"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="90dp"
android:layout_marginRight="90dp"
android:layout_marginTop="10dp"
android:orientation="horizontal">

<Button
android:id="@+id/btnLogin"
android:layout_width="wrap_content"
android:layout_height="70dp"
android:layout_weight="1"
android:textAllCaps="true"
android:text="Đăng nhập" />

<Button
android:id="@+id/btnExit"
android:layout_width="wrap_content"
android:layout_height="70dp"
android:layout_weight="1"
android:textAllCaps="true"
android:text="Thoát" />
</LinearLayout>

</LinearLayout>

b. File acivity_chon_chu_de.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="ChonChuDeActivity">

<TextView
android:id="@+id/tvChoice"
android:layout_width="match_parent"
android:layout_height="100dp"
android:paddingTop="30dp"
android:paddingLeft="70dp"
android:text="Chọn chủ đề"
android:textAllCaps="true"
android:textSize="30dp"
android:background="#1936D7"
android:textColor="@color/white"
android:textStyle="bold" />

<Button
android:id="@+id/btnExit2"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_alignRight="@+id/tvChoice"
android:layout_marginTop="20dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="20dp"
android:textAllCaps="false"
android:text="Thoát" />

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="15dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:layout_below="@+id/tvChoice">

<ImageButton
android:id="@+id/imgBtn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher_foreground" />

<TextView
android:id="@+id/tv1"
android:layout_width="135dp"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:textColor="@color/black"
android:gravity="center"
android:textStyle="bold"
android:textSize="15dp"
android:text="Cá nhân"
android:layout_below="@id/imgBtn1"/>

<ImageButton
android:id="@+id/imgBtn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:src="@drawable/ic_launcher_foreground" />
<TextView
android:id="@+id/tv2"
android:layout_width="135dp"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:textColor="@color/black"
android:gravity="center"
android:textStyle="bold"
android:textSize="15dp"
android:text="Chi tiêu bán hàng"
android:layout_alignParentRight="true"
android:layout_below="@+id/imgBtn2"/>

<ImageButton
android:id="@+id/imgBtn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tv1"
android:src="@drawable/ic_launcher_foreground" />

<TextView
android:id="@+id/tv3"
android:layout_width="135dp"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:gravity="center"
android:textStyle="bold"
android:textSize="15dp"
android:text="Khách hàng"
android:layout_marginBottom="10dp"
android:layout_below="@+id/imgBtn3"/>

<ImageButton
android:id="@+id/imgBtn4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/tv2"
android:src="@drawable/ic_launcher_foreground" />

<TextView
android:id="@+id/tv4"
android:layout_width="135dp"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:gravity="center"
android:textStyle="bold"
android:textSize="15dp"
android:text="Danh sách tuyến"
android:layout_marginBottom="10dp"
android:layout_alignParentRight="true"
android:layout_below="@+id/imgBtn4"/>

<ImageButton
android:id="@+id/imgBtn5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/tv4"
android:src="@drawable/ic_launcher_foreground" />

<TextView
android:id="@+id/tv5"
android:layout_width="135dp"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:textColor="@color/black"
android:gravity="center"
android:textStyle="bold"
android:textSize="15dp"
android:text="Tra cứu sản phẩm"
android:layout_alignParentRight="true"
android:layout_below="@+id/imgBtn5"/>
</RelativeLayout>

</RelativeLayout>

c. File activity_them_khach_hang.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ThemKhachHangActivity">

<TextView
android:id="@+id/tvTitle"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="@color/black"
android:textColor="@color/white"
android:textAllCaps="true"
android:textStyle="bold"
android:textSize="20dp"
android:gravity="center"
android:text="Nhập thông tin khách hàng" />

<TextView
android:id="@+id/tvName"
android:layout_width="match_parent"
android:layout_height="40dp"
android:textSize="20dp"
android:gravity="left|bottom"
android:layout_marginTop="20dp"
android:textColor="@color/black"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:text="Nhập Họ và Tên:" />

<EditText
android:id="@+id/edtName"
android:layout_width="match_parent"
android:layout_height="50dp"
android:ems="10"
android:textColorHighlight="@color/teal_700"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:inputType="textPersonName" />

<TextView
android:id="@+id/tvPhone"
android:layout_width="match_parent"
android:layout_height="40dp"
android:textSize="20dp"
android:gravity="left|bottom"
android:textColor="@color/black"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:text="Nhập số điện thoại: " />

<EditText
android:id="@+id/edtPhone"
android:layout_width="match_parent"
android:layout_height="50dp"
android:ems="10"
android:textColorHighlight="@color/teal_700"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:inputType="phone" />

<TextView
android:id="@+id/tvAddress"
android:layout_width="match_parent"
android:layout_height="40dp"
android:textSize="20dp"
android:gravity="left|bottom"
android:textColor="@color/black"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:text="Nhập Địa chỉ: " />

<EditText
android:id="@+id/edtAddress"
android:layout_width="match_parent"
android:layout_height="50dp"
android:ems="10"
android:textColorHighlight="@color/teal_700"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:inputType="textPostalAddress" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_marginTop="10dp"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp">

<Button
android:id="@+id/btnSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Save" />
<Button
android:id="@+id/btnClear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Clear" />
</LinearLayout>

</LinearLayout>

d. File activity_thong_tin_khach_hang.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ThongTinKhachHangActivity">

<TextView
android:id="@+id/tvTitle2"
android:layout_width="match_parent"
android:layout_height="60dp"
android:textColor="@color/white"
android:background="@color/black"
android:textAllCaps="true"
android:textStyle="bold"
android:textSize="20dp"
android:gravity="center"
android:text="Thông tin khách hàng" />

<TableLayout
android:layout_width="match_parent"
android:layout_height="150dp"
android:gravity="center"
android:layout_margin="10dp">

<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView
android:id="@+id/tvShowname"
android:layout_width="100dp"
android:layout_height="40dp"
android:textSize="15dp"
android:gravity="center"
android:text="Họ và tên: " />

<TextView
android:id="@+id/txtName"
android:layout_width="250dp"
android:layout_height="match_parent"
android:gravity="center"
android:textSize="15dp" />

</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/tvShowphone"
android:layout_width="100dp"
android:layout_height="40dp"
android:textSize="15dp"
android:gravity="center"
android:text="Số điện thoại: " />

<TextView
android:id="@+id/txtPhone"
android:layout_width="250dp"
android:layout_height="match_parent"
android:gravity="center"
android:textSize="15dp" />
</TableRow>

<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView
android:id="@+id/tvShowaddress"
android:layout_width="100dp"
android:layout_height="40dp"
android:textSize="15dp"
android:gravity="center"
android:text="Địa chỉ: " />

<TextView
android:id="@+id/txtAddress"
android:layout_width="250dp"
android:layout_height="match_parent"
android:gravity="center"
android:textSize="15dp" />
</TableRow>

</TableLayout>

<Button
android:id="@+id/btnReturn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Quay về" />

</LinearLayout>

Bài 2:
1/ Màn hình chạy ứng dụng
2/ MainActivity.java
package edu.huflit.bt32;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {


Button btnOpen;
EditText edtInput;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//Find view
btnOpen = findViewById(R.id.btnOpen);
edtInput = findViewById(R.id.edtInput);

//Open website
btnOpen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(Intent.ACTION_VIEW,
Uri.parse(edtInput.getText().toString()));
startActivity(i);
}
});

//Call 01234 56789


btnOpen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent callPhone = new Intent(Intent.ACTION_DIAL);
callPhone.setData(Uri.parse("tel:01234-56789"));
startActivity(callPhone);
}
});

//Open Map: HUFLIT


btnOpen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String url = "https://goo.gl/maps/6nd5EH5ZPXyq9yob7";
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse(url));
startActivity(intent);
}
});
}
}

3/ activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >

<EditText
android:id="@+id/edtInput"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text"
android:textColorHighlight="@color/teal_200"/>

<Button
android:id="@+id/btnOpen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAllCaps="true"
android:text="Open Web" />
</LinearLayout>

Lab 3.2
Bài 1:
1/ Màn hình chạy ứng dụng
2/ MainActivity.java
package edu.huflit.bitpimageviewalertdialogcontrol;

import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.CheckBox;
import android.widget.RadioGroup;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.Button;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {


EditText edtName;
CheckBox chkMU, chkBM, chkBar;
RadioGroup rgMauYt;
RadioButton rbXanh, rbDo, rbTim, rbVang;
Spinner spinnerQQ;
Button btnAdd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//Find view
edtName = findViewById(R.id.edtName);

chkMU = findViewById(R.id.chkMU);
chkBM = findViewById(R.id.chkBM);
chkBar = findViewById(R.id.chkBar);

rgMauYt = findViewById(R.id.rgMauYT);
rbXanh = findViewById(R.id.rbXanh);
rbDo = findViewById(R.id.rbDo);
rbTim = findViewById(R.id.rbTim);
rbVang = findViewById(R.id.rbVang);

spinnerQQ = findViewById(R.id.spinnerQQ);

btnAdd = findViewById(R.id.btnAdd);

//Create ArrayList
ArrayList<String> dsQQ = new ArrayList<String>();

//Add data
dsQQ.add("Khánh Hòa");
dsQQ.add("Hồ Chí Minh");
dsQQ.add("Long An");
dsQQ.add("Quảng Ngãi");
dsQQ.add("Quảng Bình");

//Create ArrayAdapter
ArrayAdapter adapter = new ArrayAdapter<>(this,
android.R.layout.simple_spinner_item, dsQQ);

//Set adapter
spinnerQQ.setAdapter(adapter);

//Create event onClick


btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Check name
String name = (edtName.getText() + "").trim();
if (name.length() < 3)
{
edtName.requestFocus();
edtName.selectAll();
Toast.makeText(MainActivity.this, "Họ tên phải >= 3 kí
tự", Toast.LENGTH_SHORT).show();
return;
}

//Check favorite color


int id = rgMauYt.getCheckedRadioButtonId();
if ( id == -1)
{
Toast.makeText(MainActivity.this, "Phải chọn màu",
Toast.LENGTH_SHORT).show();
return;

//
RadioButton radioButton = findViewById(id);
String colorChoice = radioButton.getText() + "";

String frClub = "";


if(chkMU.isChecked())
frClub += "\t" + chkMU.getText() + "\n";
if(chkBM.isChecked())
frClub += "\t" + chkBM.getText() + "\n";
if(chkBar.isChecked())
frClub += "\t" + chkBar.getText() + "\n";

//Create AlertDialog
AlertDialog.Builder builder = new
AlertDialog.Builder(MainActivity.this);
builder.setTitle("Thông tin cá nhân");

String msg = name;


msg += "\n-------------------------------\n";
msg += "Quê quán: " + spinnerQQ.getSelectedItem();
msg += "\n-------------------------------\n";
msg += "CLB yêu thích: \n";
msg += frClub;
msg += "\n-------------------------------\n";
msg += "Màu sắc chủ đạo: ";
msg += colorChoice;

builder.setMessage(msg);

builder.setPositiveButton("Đóng", new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface,
int i) {
dialogInterface.cancel();
}
});
builder.create().show();
}
});
}
}

3/ activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/rgMau"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="15dp"
android:orientation="vertical"
tools:context=".MainActivity">

<TextView
android:id="@+id/tvTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25dp"
android:textColor="@color/black"
android:text="Thông tin Cá nhân" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">

<TextView
android:id="@+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:textColor="@color/black"
android:text="Họ tên" />

<EditText
android:id="@+id/edtName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
android:textColorHighlight="#D72967"/>
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:orientation="vertical">

<TextView
android:id="@+id/tvClub"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="20dp"
android:textColor="@color/black"
android:gravity="left|center"
android:text="CLB yêu thích" />
<CheckBox
android:id="@+id/chkMU"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Manchester United"
android:textColorHighlight="#D72967"/>

<CheckBox
android:id="@+id/chkBM"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Bayern Munich"
android:textColorHighlight="#D72967"/>

<CheckBox
android:id="@+id/chkBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Barcelona"
android:textColorHighlight="#D72967"/>

<TextView
android:id="@+id/tvColor"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="@color/black"
android:textSize="20dp"
android:text="Màu yêu thích" />

</LinearLayout>

<RadioGroup
android:id="@+id/rgMauYT"
android:layout_width="match_parent"
android:layout_height="30dp"
android:orientation="horizontal">

<RadioButton
android:id="@+id/rbXanh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Xanh"
android:textColorHighlight="#D72967"/>

<RadioButton
android:id="@+id/rbDo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Đỏ"
android:textColorHighlight="#D72967"/>

<RadioButton
android:id="@+id/rbTim"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tím"
android:textColorHighlight="#D72967"/>
<RadioButton
android:id="@+id/rbVang"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Vàng"
android:textColorHighlight="#D72967"/>
</RadioGroup>

<TableLayout
android:layout_width="match_parent"
android:layout_height="28dp">

<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/tvCity"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textColor="@color/black"
android:textSize="20dp"
android:text="Quê quán" />

<Spinner
android:id="@+id/spinnerQQ"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:spinnerMode="dropdown"/>
</TableRow>

</TableLayout>

<Button
android:id="@+id/btnAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textAllCaps="true"
android:textColor="@color/black"
android:text="Xuất thông tin"/>
</LinearLayout>

You might also like