Use static handler to PASS Parameters in the Custom dialog box

Source: Internet
Author: User

Directly paste the Code:

Jsdmaindialog. Java

Package com. JSD. Demo;

Import Android. App. activity;
Import Android. content. context;
Import Android. Graphics. color;
Import Android. OS. Bundle;
Import Android. OS. Handler;
Import Android. OS. message;
Import Android. View. view;
Import Android. View. View. onclicklistener;
Import Android. widget. Button;
Import Android. widget. textview;

/**
*
* @ Author jiangshide
*
*/
Public class jsdmaindialog extends activity {
 
Private context mcontext;
 
Private button Msub;
Private textview mresultvalue;
 
Public static handler = new handler ();
 
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Getviewbyid ();
}

Public void getviewbyid (){
Mcontext = this;
Msub = (button) This. findviewbyid (R. Id. sub );
Mresultvalue = (textview) This. findviewbyid (R. Id. resultvalue );
Mresultvalue. settext ("handler not processed ");
Msub. setonclicklistener (listener );
}

Private onclicklistener listener = new onclicklistener (){
@ Override
Public void onclick (view v ){
Switch (V. GETID ()){
Case R. Id. Sub:
Final string flag = "rqbackvalue ";
Dialogcustomer Dc = new dialogcustomer (mcontext, flag );
DC. Show ();
Handler = new handler (){
Public void handlemessage (Message MSG ){
String resultflag = msg. getdata (). getstring ("Flags ");
If (resultflag. equalsignorecase (FLAG )){
Mresultvalue. settext ("this is the data that is later displayed after handler processing ");
Mresultvalue. settextcolor (color. Cyan );
Mresultvalue. settextsize (30 );
}
};
};
Break;

Default:
Break;
}
}
};
}

 

Dialogcustomer. Java:

Package com. JSD. Demo;

Import Android. App. Dialog;
Import Android. content. context;
Import Android. OS. Bundle;
Import Android. OS. message;
Import Android. View. view;
Import Android. widget. Button;

/**
*
* @ Author jiangshide
*
*/
Public class dialogcustomer extends dialog {

Private context mcontext;
 
Private button OK;
String flag;
 
Public dialogcustomer (context c, string flag ){
Super (C );
This. mcontext = C;
This. Flag = flag;
}

@ Override
Protected void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Dialog );
Getviewbyid ();
}
 
Public void getviewbyid (){
OK = (button) This. findviewbyid (R. Id. dialog_ OK );
OK. setonclicklistener (listener );
}
 
Private Android. View. View. onclicklistener listener = new Android. View. View. onclicklistener (){
@ Override
Public void onclick (view v ){
Switch (V. GETID ()){
Case R. Id. dialog_ OK:
Message MSG = new message (); // get and set an information save point
MSG. What = 1;
MSG. getdata (). putstring ("Flags", flag );
Jsdmaindialog. handler. sendmessage (MSG); // put the data in the logoff queue
Dismiss ();
Break;
}
}
};
}

Dialog. 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: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "when a response is returned, the parameter is passed and the operation after judgment is executed"
/>
<Button
Android: Id = "@ + ID/dialog_ OK"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "click"
/>
</Linearlayout>

 

Address: http://blog.csdn.net/jiangshide/article/details/6434597

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.