Android Batch setup listener, listener pass parameters, override listener constructor

Source: Internet
Author: User

This problem troubled me for several days, in my curriculum application, each page has 15 TextView, each to set the listener, but this is not difficult, it is difficult to use anonymous inner class to implement the listening excuse, but also to set the listener inside! In short, textview the value back to TextView when the Edittext,edittext click is complete, this place requires two listeners.

My earliest implementation was (mon_tv[] is an TextView array, mon_et[] is a edittext array):

Mon_tv[1].setonlongclicklistener (New Onlongclicklistener () {
@Override
public boolean Onlongclick (View v) {

Mon_tv[1].setvisibility (View.gone);
Mon_et[1].setvisibility (view.visible);
Mon_et[1].settext (Mon_tv[1].gettext (). toString ());
Mon_et[1].setoneditoractionlistener (New Oneditoractionlistener () {

@Override
public boolean oneditoraction (TextView v, int ActionId, keyevent event) {
if (ActionId = = Editorinfo.ime_action_done) {
Mon_et[1].setvisibility (View.gone);
Mon_tv[1].setvisibility (view.visible);
Mon_tv[1].settext (Mon_et[1].gettext (). toString ());
}
return false;
}
});


return false;
}
});

This is easy to use, but to write 15 consecutive, if more is not more troublesome, not advisable, and then I want to write directly in a loop, as follows:

for (int i=0;i<15;i++) {

mon_tv[I].setonlongclicklistener (New Onlongclicklistener () {
@Override
public boolean Onlongclick (View v) {

mon_tv[ I].setvisibility (View.gone);
mon_et[ I].setvisibility (view.visible);
mon_et[ I].settext (mon_tv[I].gettext (). toString ());
mon_et[ I].setoneditoractionlistener (New Oneditoractionlistener () {

@Override
public boolean oneditoraction (TextView v, int ActionId, keyevent event) {
if (ActionId = = Editorinfo.ime_action_done) {
mon_et[ i].setvisibility (view.gone);
mon_tv[ i].setvisibility (view.visible);
mon_tv[ i].settext (Mon_et[1].gettext (). toString ());
}
return false;
}
});


return false;
}
});

}

Note This red place error, because anonymous inner class inside again anonymous internal class when asked I must be final type, that is immutable, but my I must change ah! So to think, not feasible, had to forget.


Today, on the machine class, ask the TA, tips to give me a very good idea, that is, listener transfer parameters, suddenly, to listener pass a parameter, and rewrite the constructor, the ultimate solution is as follows:

for (int i=0;i<15;i++) {

mon_tv[i].setonlongclicklistener (new Mymontvonlongclicklistener (i));

}

Monday, initialize the TV and prepare for ET
Private class Mymontvonlongclicklistener implements onlongclicklistener{
private int temp;
Public Mymontvonlongclicklistener (int i) {
TODO auto-generated Constructor stub
Temp=i;
}


@Override
public boolean Onlongclick (View v) {
System.out.println ("Call longclick:mon_tv_id=" +temp);
Mon_tv[temp].setvisibility (View.gone);
Mon_et[temp].setvisibility (view.visible);
Mon_et[temp].settext (Mon_tv[temp].gettext (). toString ());
Mon_et[temp].setoneditoractionlistener (new Mymononeditlistener (temp));
return false;
}

}
To ET initialization
Private class Mymononeditlistener implements oneditoractionlistener{
private int temp;
Public Mymononeditlistener (int i) {
Temp=i;
}
@Override
public boolean oneditoraction (TextView v, int ActionId, keyevent event) {
System.out.println ("Call editover:mon_tv_id=" +temp);
if (ActionId = = Editorinfo.ime_action_done) {
Mon_et[temp].setvisibility (View.gone);
Mon_tv[temp].setvisibility (view.visible);
int A=JUDGEZSG ("mon_tv[" +string.valueof (temp) + "]", Mon_et[temp].gettext (). toString ());
if (a!=4) {
Managesqlite (A, "mon_tv[" +string.valueof (temp) + "]", Mon_et[temp].gettext (). toString ());
}
Mon_tv[temp].settext (Mon_et[temp].gettext (). toString ());
}
return false;
}

}


Problem solving is so simple, I think for a few days, are ready to go directly to write 15 times, in fact, there are 15*5 all over so many. Before this thought, I did not think that the online access to the relevant information is relatively small, hoping to bring inspiration to others.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.