main

package baitap.gameloft.com.picker; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.Locale; import android.os.Bundle; import android.app.Activity; import android.app.DatePickerDialog; import android.app.TimePickerDialog; import android.app.DatePickerDialog.OnDateSetListener; import android.app.TimePickerDialog.OnTimeSetListener; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.AdapterView.OnItemLongClickListener; 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; public class MainActivity extends Activity { TextView txtDate,txtTime; EditText editCv,editNd; Button btnDate,btnTime,btnAdd; ArrayList<JobInWeek>arrJob=new ArrayList<JobInWeek>(); ArrayAdapter<JobInWeek>adapter=null; ListView lvCv; Calendar cal; Date dateFinish; Date hourFinish; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getFormWidgets(); getDefaultInfor(); addEventFormWidgets(); } public void getFormWidgets() { txtDate=(TextView) findViewById(R.id.txt_ngay); txtTime=(TextView) findViewById(R.id.txt_gio); editCv=(EditText) findViewById(R.id.edt_cv); editNd=(EditText) findViewById(R.id.edt_nd); btnDate=(Button) findViewById(R.id.btn_date); btnTime=(Button) findViewById(R.id.btn_time); btnAdd=(Button) findViewById(R.id.btn_add); lvCv=(ListView) findViewById(R.id.lv); //Gán DataSource vào ArrayAdapter adapter=new ArrayAdapter<JobInWeek> (this, android.R.layout.simple_list_item_1, arrJob); lvCv.setAdapter(adapter); } public void getDefaultInfor() { cal=Calendar.getInstance(); SimpleDateFormat dft=null; dft=new SimpleDateFormat("dd/MM/yyyy",Locale.getDefault()); String strDate=dft.format(cal.getTime()); txtDate.setText(strDate); dft=new SimpleDateFormat("hh:mm a",Locale.getDefault()); String strTime=dft.format(cal.getTime()); txtTime.setText(strTime); dft=new SimpleDateFormat("HH:mm",Locale.getDefault()); txtTime.setTag(dft.format(cal.getTime())); editCv.requestFocus(); dateFinish=cal.getTime(); hourFinish=cal.getTime(); } public void addEventFormWidgets() { btnDate.setOnClickListener(new MyButtonEvent()); btnTime.setOnClickListener(new MyButtonEvent()); btnAdd.setOnClickListener(new MyButtonEvent()); lvCv.setOnItemClickListener(new MyListViewEvent()); lvCv.setOnItemLongClickListener(new MyListViewEvent()); } private class MyButtonEvent implements OnClickListener { @Override public void onClick(View v) { switch(v.getId()) { case R.id.btn_date: showDatePickerDialog(); break; case R.id.btn_time: showTimePickerDialog(); break; case R.id.btn_add: processAddJob(); break; } } } private class MyListViewEvent implements OnItemClickListener, OnItemLongClickListener { @Override public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { arrJob.remove(arg2); adapter.notifyDataSetChanged(); return false; } @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { Toast.makeText(MainActivity.this, arrJob.get(arg2).getDesciption(), Toast.LENGTH_LONG).show(); } } public void showDatePickerDialog() { OnDateSetListener callback=new OnDateSetListener() { public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { txtDate.setText( (dayOfMonth) +"/"+(monthOfYear+1)+"/"+year); cal.set(year, monthOfYear, dayOfMonth); dateFinish=cal.getTime(); } }; String s=txtDate.getText()+""; String strArrtmp[]=s.split("/"); int ngay=Integer.parseInt(strArrtmp[0]); int thang=Integer.parseInt(strArrtmp[1])-1; int nam=Integer.parseInt(strArrtmp[2]); DatePickerDialog pic=new DatePickerDialog( MainActivity.this, callback, nam, thang, ngay); pic.setTitle("Chọn ngày hoàn thành"); pic.show(); } public void showTimePickerDialog() { OnTimeSetListener callback=new OnTimeSetListener() { public void onTimeSet(TimePicker view, int hourOfDay, int minute) { String s=hourOfDay +":"+minute; int hourTam=hourOfDay; if(hourTam>12) hourTam=hourTam-12; txtTime.setText (hourTam +":"+minute +(hourOfDay>12?" PM":" AM")); txtTime.setTag(s); cal.set(Calendar.HOUR_OF_DAY, hourOfDay); cal.set(Calendar.MINUTE, minute); hourFinish=cal.getTime(); } }; String s=txtTime.getTag()+""; String strArr[]=s.split(":"); int gio=Integer.parseInt(strArr[0]); int phut=Integer.parseInt(strArr[1]); TimePickerDialog time=new TimePickerDialog( MainActivity.this, callback, gio, phut, true); time.setTitle("Chọn giờ hoàn thành"); time.show(); } public void processAddJob() { String title=editCv.getText()+""; String description=editNd.getText()+""; JobInWeek job=new JobInWeek(title, description, dateFinish, hourFinish); arrJob.add(job); adapter.notifyDataSetChanged(); editCv.setText(""); editNd.setText(""); editCv.requestFocus(); } }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.