Monday, November 9, 2015

Quản lý công việc


Class MainActivity: 
 
import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class MainActivity extends Activity implements View.OnClickListener, AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener{
    EditText edtCongViec, edtNoiDung;
    TextView NgayHT, GioHT;
    Button btnDate, btnTime, btnThem;
    ListView listCongViec;
    ArrayList<JobInWeek> arrCongViec = new ArrayList<JobInWeek>();
    ArrayAdapter<JobInWeek> adapter;
    String congviec, noidung;
    Date dateFinish, timeFinish;
    Calendar cal;
    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getControl();
        TimeDateDefault();
    }

    public void getControl(){
        edtCongViec = (EditText)findViewById(R.id.edtCongViec);
        edtNoiDung = (EditText)findViewById(R.id.edtNoiDung);
        NgayHT = (TextView)findViewById(R.id.tvDate);
        GioHT = (TextView)findViewById(R.id.tvTime);
        btnDate = (Button)findViewById(R.id.btnDate);
        btnTime = (Button)findViewById(R.id.btnTime);
        btnThem = (Button)findViewById(R.id.addCongViec);
        listCongViec = (ListView)findViewById(R.id.listCongViec);
        btnDate.setOnClickListener(this);
        btnTime.setOnClickListener(this);
        btnThem.setOnClickListener(this);
        adapter = new ArrayAdapter<JobInWeek>(MainActivity.this, android.R.layout.simple_list_item_1, arrCongViec);
        listCongViec.setAdapter(adapter);
        listCongViec.setOnItemClickListener(this);
        listCongViec.setOnItemLongClickListener(this);

    }

    public void TimeDateDefault(){
        cal = Calendar.getInstance();
        SimpleDateFormat sdfDate = new SimpleDateFormat("dd/MM/yyyy");
        NgayHT.setText(sdfDate.format(cal.getTime()));
        SimpleDateFormat sdfTime = new SimpleDateFormat("HH:mm");
        GioHT.setText(sdfTime.format(cal.getTime()));
        dateFinish = cal.getTime();
        timeFinish = cal.getTime();
    }
    // hàm Dialog set ngày hoàn thành    public void DialogDate(){
        cal = Calendar.getInstance();
        int nam = cal.get(Calendar.YEAR);
        int thang = cal.get(Calendar.MONTH);
        int ngay = cal.get(Calendar.DAY_OF_MONTH);
        DatePickerDialog picDate = new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() {
            @Override            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                NgayHT.setText(dayOfMonth+"/"+(monthOfYear+1)+"/"+year);
                cal.set(year, monthOfYear, dayOfMonth);
                dateFinish = cal.getTime();
            }
        }, nam, thang, ngay);
        picDate.show();

    }
    // hàm Dialog set giờ hoàn thành    public void DialogTime(){
        cal = Calendar.getInstance();
        int gio = cal.get(Calendar.HOUR_OF_DAY);
        int phut = cal.get(Calendar.MINUTE);
        TimePickerDialog picTime = new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() {
            @Override            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                GioHT.setText(hourOfDay+" : "+minute);
                cal.set(Calendar.HOUR_OF_DAY, hourOfDay);
                cal.set(Calendar.MINUTE, minute);
                timeFinish = cal.getTime();
            }
        }, gio, phut, false);

        picTime.show();

    }

    public void NoiDungvaCongviec(){
        congviec = edtCongViec.getText().toString();
        noidung = edtNoiDung.getText().toString();
        edtCongViec.setText("");
        edtNoiDung.setText("");
        edtCongViec.requestFocus();
    }

    @Override    public void onClick(View v) {
        if(v==btnDate){
            DialogDate();
        }

        if(v==btnTime){
            DialogTime();
        }

        if(v==btnThem){
            NoiDungvaCongviec();
            JobInWeek cv = new JobInWeek(congviec, dateFinish, noidung, timeFinish);
            arrCongViec.add(cv);
            adapter.notifyDataSetChanged();

        }
    }

    @Override    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Toast.makeText(MainActivity.this, arrCongViec.get(position).getNoiDung(), Toast.LENGTH_LONG).show();
    }

    @Override    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        final int i = position;
        AlertDialog.Builder a = new AlertDialog.Builder(MainActivity.this);
        a.setTitle("Câu hỏi");
        a.setMessage("Xóa công việc này?");
        a.setPositiveButton("Có", new DialogInterface.OnClickListener() {
            @Override            public void onClick(DialogInterface dialog, int which) {
                arrCongViec.remove(i);
                adapter.notifyDataSetChanged();
            }
        });
        a.setNegativeButton("Không", new DialogInterface.OnClickListener() {
            @Override            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });
        a.create().show();
        return false;
    }
}
 
 
Class JobInWeek :

package com.example.letrongtam.dialogdatetime;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/** * Created by Le Trong Tam on 09-Nov-15. */public class JobInWeek {
    private String tenCongViec;
    private String noiDung;
    private Date DateFinish;
    private Date TimeFinish;

    public JobInWeek(String tenCongViec, Date dateFinish, String noiDung, Date timeFinish) {
        this.tenCongViec = tenCongViec;
        DateFinish = dateFinish;
        this.noiDung = noiDung;
        TimeFinish = timeFinish;
    }
    public JobInWeek(){
        super();
    }

    public String getTenCongViec() {
        return tenCongViec;
    }

    public void setTenCongViec(String tenCongViec) {
        this.tenCongViec = tenCongViec;
    }

    public Date getDateFinish() {
        return DateFinish;
    }

    public void setDateFinish(Date dateFinish) {
        DateFinish = dateFinish;
    }

    public String getNoiDung() {
        return noiDung;
    }

    public void setNoiDung(String noiDung) {
        this.noiDung = noiDung;
    }

    public Date getTimeFinish() {
        return TimeFinish;
    }

    public void setTimeFinish(Date timeFinish) {
        TimeFinish = timeFinish;
    }

    public String getDateFormat(Date d)
    {
        SimpleDateFormat dft=new SimpleDateFormat("dd/MM/yyyy", Locale.getDefault());
        return dft.format(d);
    }

    public String getHourFormat(Date d)
    {
        SimpleDateFormat dft=new                SimpleDateFormat("HH:mm", Locale.getDefault());
        return dft.format(d);
    }
    @Override    public String toString() {
        return getDateFormat(DateFinish)+"  "+ getHourFormat(TimeFinish)+"  "+tenCongViec;
    }
}


Main XML:  
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">
    <TextView        android:id="@+id/tvQuanLyCV"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:gravity="center"        android:text="Quản lý công việc hàng tuần"        android:textColor="#fff"        android:background="#b71aff"/>
   <TableLayout       android:id="@+id/tablelayout"       android:layout_width="match_parent"       android:layout_height="wrap_content"       android:layout_marginTop="10dp"       android:layout_marginLeft="5dp"       android:layout_marginRight="5dp"       android:stretchColumns="1">
       <TableRow>
           <TextView               android:id="@+id/tvCongViec"               android:layout_width="wrap_content"               android:layout_height="wrap_content"               android:text="Công việc:"/>
           <EditText               android:id="@+id/edtCongViec"               android:layout_width="wrap_content"               android:layout_height="wrap_content"               android:layout_span="2"/>
       </TableRow>
       <TableRow>
           <TextView               android:id="@+id/tvNoiDung"               android:layout_width="wrap_content"               android:layout_height="wrap_content"               android:text="Nội dung:"               />
           <EditText               android:id="@+id/edtNoiDung"               android:layout_width="wrap_content"               android:layout_height="wrap_content"               android:layout_span="2"/>
       </TableRow>
       <TableRow>
           <TextView               android:id="@+id/tvNgayHienThi"               android:layout_width="wrap_content"               android:layout_height="wrap_content"               android:text="Ngày hoàn thành"/>
           <TextView               android:id="@+id/tvDate"               android:layout_width="wrap_content"               android:layout_height="wrap_content"               android:gravity="center"/>
           <Button               android:id="@+id/btnDate"               android:layout_width="wrap_content"               android:layout_height="wrap_content"               android:text="Date"               android:textAllCaps="false"/>
       </TableRow>
       <TableRow>
           <TextView               android:id="@+id/tvGioHoanThanh"               android:layout_width="wrap_content"               android:layout_height="wrap_content"               android:text="Giờ hoàn thành:"/>
           <TextView               android:id="@+id/tvTime"               android:layout_width="wrap_content"               android:layout_height="wrap_content"               android:gravity="center"/>
           <Button               android:id="@+id/btnTime"               android:layout_width="wrap_content"               android:layout_height="wrap_content"               android:text="Time"               android:textAllCaps="false"/>
       </TableRow>
   </TableLayout>
    <TextView        android:layout_width="match_parent"        android:layout_height="2dp"        android:background="#32c92727"/>
    <Button        android:id="@+id/addCongViec"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Thêm"        android:layout_gravity="center"        android:layout_margin="5dp"/>
    <TextView        android:id="@+id/tvDsCongViec"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="Danh sách công việc"        android:gravity="center"        android:background="#b71aff"        android:textColor="#fff"/>
    <ListView        android:id="@+id/listCongViec"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginRight="5dp"        android:layout_marginLeft="5dp"></ListView>
</LinearLayout>

No comments:

Post a Comment