Dialogfragment using the +j interface callback

Source: Internet
Author: User

Although Android has Alterdialog and PROGRESSIONDIALOGL two kinds, but these two weeks are too limited, there is a compromise method is to redefine the layout, and the layout is set to dialog style, This allows you to customize the dialog style indirectly, but now Android provides a more basic class dialogfragment. The click event is then monitored using the interface callback method.

1.dialogfragment.class

@SuppressLint ("Newapi")
public class Diagfragment extends Dialogfragment implements Onclicklistener {
Private Context Mcontext;
Private TextView MTv1, MTv2, MTv3;


Public interface Ondeptcallback {
public void OnCallback (String st);
}


Private Ondeptcallback Onback;
Public Diagfragment () {
Remove the default dialog header
int style = Dialogfragment.style_no_title;
SetStyle (style, 0);
}
@Override
Public View Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {
View view = Inflater.inflate (R.layout.list_dialog, NULL);
Mcontext = Getactivity ();
Initview (view);
return view;
}


/**
* Initialize the control
*/
private void Initview (view view) {
MTv1 = (TextView) View.findviewbyid (r.id.tv_1);
MTv2 = (TextView) View.findviewbyid (r.id.tv_2);
MTv3 = (TextView) View.findviewbyid (r.id.tv_3);
Mtv1.setonclicklistener (this);
Mtv2.setonclicklistener (this);
Mtv3.setonclicklistener (this);
}

/**
* @return The Onback
*/
Public Ondeptcallback Getonback () {
return this.onback;
}


/**
* @param onback
* The Onback to set
*/
public void Setonback (Ondeptcallback onback) {
This.onback = Onback;
}


/**
* Set the Pop-up dialog style, this side is the center effect
*
*/
@Override
public void Onresume () {
Layoutparams params = Getdialog (). GetWindow (). GetAttributes ();
Displaymetrics mdisplaymetrics = new Displaymetrics ();
Getactivity (). Getwindowmanager (). Getdefaultdisplay (). Getmetrics (Mdisplaymetrics);
Params.width = MDISPLAYMETRICS.WIDTHPIXELS/2;
Params.height = layoutparams.wrap_content;
Getdialog (). GetWindow (). SetAttributes ((android.view.WindowManager.LayoutParams) params);
Super.onresume ();
}
@Override
public void OnClick (View v) {
Switch (V.getid ()) {
Case R.id.tv_1:
Onback.oncallback ("This is Text1");
Break
Case R.id.tv_2:
Onback.oncallback ("This is Text2");
Break
Case R.id.tv_3:
Onback.oncallback ("This is Text3");
Break
Default
Break
}
}
}

2.List_dialog.xml

Three TextView, the layout depends on the requirements

3.mainactivity.class

public class Mainactivity extends Activity {
Private Button mBtn;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Mbtn= (Button) Findviewbyid (R.ID.BT);
Mbtn.setonclicklistener (New Onclicklistener () {

@SuppressLint ("Newapi")
@Override
public void OnClick (View arg0) {
Final diagfragment diaf=new diagfragment ();
Diaf.setonback (New Ondeptcallback () {

@Override
public void OnCallback (String st) {
Toast.maketext (Mainactivity.this, St, Toast.length_long);
Diaf.dismiss ();
}
});
Diaf.show (MainActivity.this.getFragmentManager (), NULL);
}

});
}

At this point the entire project is over, and when you click on those textview in the dialogfragment, the activity is able to listen to the event based on the interface callback.

If not very clear can download the source to see:)

http://download.csdn.net/detail/u013651405/9032603

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Dialogfragment using the +j interface callback

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.