Android Custom Pop-up dialog box instance

Source: Internet
Author: User

In the process of writing code, for pop-up boxes, pre-written layout files often do not meet our needs, then we need to define the style.

1, first create a new XML file, here to set the audio switch as an example

Myview.xml as follows:

<?xml version= "1.0" encoding= "Utf-8"?>

<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"

Android:layout_width= "Match_parent"

android:layout_height= "Match_parent" >

<textview

Android:id= "@+id/textview1"

Android:layout_width= "Wrap_content"

android:layout_height= "Wrap_content"

Android:layout_alignbottom= "@+id/togglebutton1"

Android:layout_alignparentleft= "true"

Android:layout_alignparenttop= "true"

android:text= "@string/voice"

Android:textsize= "20sp"/>

<togglebutton

Android:id= "@+id/togglebutton1"

Android:layout_width= "Wrap_content"

android:layout_height= "Wrap_content"

Android:layout_alignparenttop= "true"

android:layout_torightof= "@+id/textview1"

android:text= "ToggleButton"

android:textoff= "OFF"

android:texton= "on"/>

</RelativeLayout>

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/69/FB/wKiom1Urc-KwNxH9AABAqo8VXdQ489.jpg "title=" Qq20150413154351.png "alt=" Wkiom1urc-kwnxh9aabaqo8vxdq489.jpg "/>

2. Invoking a custom view in code

public void Setsound () {

Get a custom view

Layoutinflater layoutinflater = Layoutinflater.from (mainactivity.instance); Mainactivity.instance is defined in Mainactivity.java, public static mainactivity instance;

View MyLoginView = layoutinflater.inflate (R.layout.myview, NULL);

Mytogglebutton = (ToggleButton) Myloginview.findviewbyid (R.id.togglebutton1);

if (audio_on) {

Mytogglebutton.setchecked (TRUE);

}else{

Mytogglebutton.setchecked (FALSE);

}

Dialog Alertdialog = new Alertdialog.builder (mainactivity.instance)

. Settitle ("Settings")

. Setview (MyLoginView)

. SetIcon (Android. R.drawable.ic_dialog_info)

. Setpositivebutton ("OK", new Dialoginterface.onclicklistener () {

@Override

public void OnClick (Dialoginterface dialog, int which) {

TODO auto-generated Method Stub

if (mytogglebutton.ischecked ()) {

audio_on = true;

}else{

audio_on = false;

}

}

}).

Create ();

Alertdialog.show ();

}

This is the menu interface of the game:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6A/08/wKioL1Urdh3zbTm2AAKYNM4GZRU331.jpg "style=" float: none; "title=" Qq20150413154729.png "alt=" Wkiol1urdh3zbtm2aakynm4gzru331.jpg "/>

Click Settings to eject the Set Sound Switch dialog box, call the Setsound () method:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6A/0D/wKiom1UrdM-TOJk-AAF9hx2ng3c643.jpg "style=" float: none; "title=" Click Settings to eject the Settings Sound dialog box "alt=" Wkiom1urdm-tojk-aaf9hx2ng3c643.jpg "/>

3, relevant knowledge point supplement

It is still very useful to layoutinflater this class in real-world development, which acts like Findviewbyid (). The difference is that Layoutinflater is used to find the XML layout file under res/layout/and is instantiated, while Findviewbyid () is looking for specific widget controls (such as button, TextView, and so on) under the XML layout file.

Specific role:

1, for a non-loaded or want to dynamically load the interface, you need to use Layoutinflater.inflate () to load;

2, for an already loaded interface, you can use the Activiyt.findviewbyid () method to obtain the interface elements.



Android Custom Pop-up dialog box instance

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.