huong_dan_lap_trinh_voi_android_11

Upload: nguyen-kien

Post on 08-Apr-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/7/2019 huong_dan_lap_trinh_voi_android_11

    1/5

    B5:Chnh sa file strings.xml trong res\value:M:

    Example 3Enter the work

    hereHourMinuteAdd work

    B6: Time to coding. i ti src\at.exam to mt class mi l CustomViewGroupvi ni dung sau:M:

    package at.exam;

    import android.content.Context;import android.view.LayoutInflater;import android.widget.CheckBox;import android.widget.LinearLayout;import android.widget.TextView;

    public class CustomViewGroup extends LinearLayout {public CheckBox cb;public TextView workContent;public TextView timeContent;public CustomViewGroup(Context context) {

    super(context);

  • 8/7/2019 huong_dan_lap_trinh_voi_android_11

    2/5

    //S dng LayoutInflater gn giao din tronglist.xml cho class ny

    LayoutInflater li = (LayoutInflater)this.getContext()

    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);li.inflate(R.layout.list, this, true);

    //Ly v cc View qua Idcb = (CheckBox) findViewById(R.id.check_work);workContent = (TextView)

    findViewById(R.id.work_content);timeContent = (TextView)

    findViewById(R.id.time_content);

    }}

    on code trn gip tanh ngha giao din ca custom ViewGroup mi da trnfile list.xml. Mi ngi cng c th to giao din bng code, ko cn s dng XMLnhng s phc tp hn v mnh cng ko gii thiu y.

    B7:To 1 class Work cng trong at.exam th hin cng vic:M:

    package at.exam;

    public class Work {private String workContent;private String timeContent;private boolean isChecked;

    public Work(String workContent, String timeContent){

    this.workContent = workContent;

    this.timeContent = timeContent;isChecked = false;

    }

    public String getContent() {return workContent;

    }

  • 8/7/2019 huong_dan_lap_trinh_voi_android_11

    3/5

    public String getTime() {

    return timeContent;}

    public void setChecked(boolean isChecked) {this.isChecked = isChecked;

    }

    public boolean isChecked() {return isChecked;

    }}

    Code rt n gin nn mnh s khng ch thch na.

    B8:Chng ta to xong custem ViewGroup, by gi chnh l lc s dng. To 1class mi tn l ListWorkApdapter trong at.exam:M:

    package at.exam;

    import java.util.ArrayList;import android.content.Context;

    import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.ArrayAdapter;import android.widget.CheckBox;import android.widget.CompoundButton;import android.widget.TextView;importandroid.widget.CompoundButton.OnCheckedChangeListener;

    public class ListWorkAdapter extendsArrayAdapter{

    ArrayList array;int resource;Context context;

  • 8/7/2019 huong_dan_lap_trinh_voi_android_11

    4/5

    public ListWorkAdapter(Context context, inttextViewResourceId,

    ArrayList objects) {super(context, textViewResourceId, objects);

    this.context = context;resource = textViewResourceId;array = objects;

    }

    //Phng thc xc nh View m Adapter hin th, y chnh l CustomViewGroup

    //Bt buc phi Override khi k tha tArrayAdapter

    @Override

    public View getView(int position, View convertView,ViewGroup parent) {

    View workView = convertView;

    if (workView == null) {workView = new

    CustomViewGroup(getContext());}

    //Ly v i tng Work hin tifinal Work work = array.get(position);

    if (work != null) {TextView workContent = ((CustomViewGroup)

    workView).workContent;TextView timeContent = ((CustomViewGroup)

    workView).timeContent;CheckBox checkWork = ((CustomViewGroup)

    workView).cb;

    //Set s kin khi nh du vo checkboxtrn list

    checkWork.setOnCheckedChangeListener(newOnCheckedChangeListener() {

    @Override

  • 8/7/2019 huong_dan_lap_trinh_voi_android_11

    5/5

    public voidonCheckedChanged(CompoundButton buttonView,

    boolean isChecked) {work.setChecked(isChecked);

    }});

    //Ly v ni dung cho TextView v CheckBoxda vo i tng Work hin ti

    workContent.setText(work.getContent());timeContent.setText(work.getTime());checkWork.setChecked(work.isChecked());

    }return workView;

    }}

    ListWorkAdapter s c s dng thay th cho ArrayAdapter c bind viListView. Thng thng ArrayAdapter ch cho hin th String bng TextView,nhng vi vic k tha v override phng thc getView, ta c th nh ngha lihin th cho cc thnh phn ca ListView.