Tuesday, July 5, 2016

Bài tập thực hành tạo form đăng ky thành viên đơn giản

Code file activity_main.xml


Code file MainActivity.java

package com.example.macbookpro.dcsonform;

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

public class MainActivity extends AppCompatActivity {
    TextView txtHoTen, txtCMND,txtThongTin;
    RadioButton radTc, radCd, radDh;
    CheckBox chkDocSach, chkNgheNhac, chkXemPhim;
    String soThich;
    String boSung;
    Button btnGuiThongTin;
    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        addControls();
        addEvents();
    }

    private void addEvents() {
        btnGuiThongTin.setOnClickListener(new View.OnClickListener() {
            @Override            public void onClick(View v) {
                showInfo();
            }
        });
    }

    private void showInfo() {
        String chuoiLoi="";

        // Kiem tra ho ten khac rong va co it nhat 3 ky tu        String ten = txtHoTen.getText()+"";
        ten = ten.trim();
        if(ten.length() <3)
        {
            txtHoTen.requestFocus();
            chuoiLoi += "Ho ten phai khac trong va co 3 ky tu tro len\n";
        }

        // Kiem tra cmnd la so va co 9 ky tu        String cmnd = txtCMND.getText() +" ";
        cmnd = cmnd.trim();
        if(cmnd.length()!=9)
        {
            txtCMND.requestFocus();
            chuoiLoi += "CMND phai co 9 ky tu\n";
        }

        // Kiem tra bang cap: phai duoc check        RadioGroup radG = (RadioGroup) findViewById(R.id.radGroupBangCap);
        // Lay ra id cua RadioButton duoc chon trong RadioGroup        int id = radG.getCheckedRadioButtonId();
        if(id==-1)
        {
            chuoiLoi += "Phai chon bang cap\n";
        }
        // lay ra radioButton duoc chon tu id phia tren        RadioButton radBangCap = (RadioButton) findViewById(id);
        String bang = radBangCap.getText()+"";

        // lay ra so thich duoc chon dua vao cac checkBox da duoc check        String soThich ="";
        if(chkDocSach.isChecked())soThich+= chkDocSach.getText()+"\n";
        if(chkXemPhim.isChecked())soThich+= chkXemPhim.getText()+"\n";
        if(chkNgheNhac.isChecked())soThich+= chkNgheNhac.getText()+"\n";

        // Lay ra thong tin bo sung        boSung = txtThongTin.getText()+"";

        //Kiem tra neu hop le thi xuat ra AlertDialog thong tin Nhan vien
        if(chuoiLoi==""){
            //====> xuat thong tin lay duoc ra Alert            // Tao AlertDialog            AlertDialog.Builder bd = new AlertDialog.Builder(this);
            // Thiet lap tieu de cho Alert            bd.setTitle("Thong tin ca nhan");
            // Tao button cho Alert va set su kien click cho button nay            bd.setPositiveButton("Dong", new DialogInterface.OnClickListener() {
                @Override                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });

            // tao thong tin tom tat            String chuoiThongTin ="";
            chuoiThongTin += ten +"\n";
            chuoiThongTin += cmnd +"\n";
            chuoiThongTin += bang +"\n";
            chuoiThongTin += soThich +"\n";
            chuoiThongTin+= boSung;

            // xuat ra message cua AlertDialog
            bd.setMessage(chuoiThongTin);
            //Hien thi AleartDialog            bd.create().show();

        }else Toast.makeText(MainActivity.this,chuoiLoi,Toast.LENGTH_LONG).show();// xuat chuoi loi    }

    // anh xa cac control ngoai man hinh vao bien trong bo nho chinh    private void addControls() {
        txtHoTen = (TextView) findViewById(R.id.editTextHoTen);
        txtCMND = (TextView) findViewById(R.id.editTextCMND);
        txtThongTin = (TextView) findViewById(R.id.editTextTtBoSung);
        radTc = (RadioButton) findViewById(R.id.radTc);
        radCd = (RadioButton) findViewById(R.id.radCd);
        radDh = (RadioButton) findViewById(R.id.radDh);
        chkDocSach = (CheckBox) findViewById(R.id.checkBoxDocSach);
        chkNgheNhac = (CheckBox) findViewById(R.id.checkBoxNgheNhac);
        chkXemPhim = (CheckBox) findViewById(R.id.checkBoxXemPhim);
        btnGuiThongTin = (Button) findViewById(R.id.buttonGuiThongTin);
    }
}

Code file activity_main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    android:orientation="vertical"    tools:context="com.example.macbookpro.dcsonform.MainActivity"    android:weightSum="1">

    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:textAppearance="?android:attr/textAppearanceLarge"        android:text="Thong tin ca nhan"        android:textColor="#FF0000"        android:background="#0d1eda"        android:id="@+id/textView"        android:layout_gravity="center_horizontal"        android:gravity="center" />

    <TableLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_gravity="center_horizontal">

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

            <TextView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:textAppearance="?android:attr/textAppearanceLarge"                android:text="Ho ten: "                android:id="@+id/textViewHoTen" />

            <EditText                android:layout_width="250dp"                android:layout_height="wrap_content"                android:id="@+id/editTextHoTen"                android:layout_column="11" />
        </TableRow>
        <TableRow            android:layout_width="match_parent"            android:layout_height="match_parent">

            <TextView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:textAppearance="?android:attr/textAppearanceLarge"                android:text="CMND: "                android:id="@+id/textViewCMND" />

            <EditText                android:layout_width="250dp"                android:layout_height="wrap_content"                android:id="@+id/editTextCMND"                android:layout_column="11" />
        </TableRow>

    </TableLayout>

    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:textAppearance="?android:attr/textAppearanceLarge"        android:text="Bang cap"        android:id="@+id/textViewBangCap"        android:textColor="#FF0000"        android:background="#0d1eda"        android:gravity="center"        android:layout_gravity="center_horizontal" />

    <RadioGroup        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:orientation="horizontal"        android:id="@+id/radGroupBangCap"        android:layout_gravity="center_horizontal">

        <RadioButton            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="Trung cap"            android:id="@+id/radTc"            android:checked="false" />

        <RadioButton            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="Cao dang"            android:id="@+id/radCd"            android:checked="false" />

        <RadioButton            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="Dai hoc"            android:id="@+id/radDh"            android:checked="true" />
    </RadioGroup>

    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:textAppearance="?android:attr/textAppearanceLarge"        android:text="So thich"        android:id="@+id/textView2"        android:textColor="#FF0000"        android:background="#0d1eda"        android:gravity="center"        android:layout_gravity="center_horizontal" />

    <LinearLayout        android:orientation="horizontal"        android:layout_width="match_parent"        android:layout_height="wrap_content">

        <CheckBox            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="Doc sach"            android:id="@+id/checkBoxDocSach"            android:checked="false" />

        <CheckBox            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="Nghe nhac"            android:id="@+id/checkBoxNgheNhac"            android:checked="false" />

        <CheckBox            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="Xem phim"            android:id="@+id/checkBoxXemPhim"            android:checked="false" />
    </LinearLayout>

    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:textAppearance="?android:attr/textAppearanceLarge"        android:text="Thong tin bo sung"        android:id="@+id/textViewTtBoSung"        android:textColor="#FF0000"        android:background="#0d1eda"        android:gravity="center"        android:layout_gravity="center_horizontal" />

    <EditText        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:id="@+id/editTextTtBoSung"        android:layout_gravity="center_horizontal"        android:layout_weight="0.23" />

    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Gui thong tin"        android:id="@+id/buttonGuiThongTin"        android:layout_gravity="center_horizontal" />

</LinearLayout>

No comments:

Post a Comment