Android self-defined dialog box to implement QQ exit interface

Source: Internet
Author: User
<span id="Label3"></p>Effect<p><p>First look at the qq, click on the menu button to exit the dialog box will appear.</p></p><p><p></p></p><p><p>As can be seen, the dialog box has a rounded corner, as well as the title, tip information, two Button,button color is white, button Click after the background will turn gray, Normal dialog box background color is white. and in addition to clicking the Cancel button and the return key, Tap other areas of the screen the dialog box will not be in the Hour. so now let's implement this dialog box. the effect of our implementation, for example, is to see</p></p><p><p></p></p>Realize<p><p>First write our background, the background is white by default, and there is a rounded corner, using shape to implement</p></p><pre class="prettyprint"><code class=" hljs xml"><code class=" hljs xml"><span class="hljs-pi"><?</span></code><p><code class=" hljs xml"></code></p></code><p><code class=" hljs xml">XML version= "1.0" encoding= "utf-8"?><span class="hljs-tag"><span class="hljs-tag"><<span class="hljs-title">shape</span> <span class="hljs-attribute">xmlns:android</span>=<span class="hljs-value">"http://schemas.android.com/apk/res/android"</span><span class="hljs-attribute">android: Shape</span>=<span class="hljs-value">"rectangle"</span> > </span></span> <span class="hljs-tag"><span class="hljs-tag"><<span class="hljs-title">corners</span><span class="hljs-attribute">Android:bottomleftradius</span>=<span class="hljs-value">"15px"</span><span class="hljs-attribute">Android:bottomrightradius</span>= <span class="hljs-value">"15px"</span> <span class="hljs-attribute">Android:topleftradius</span>=<span class="hljs-value">"15px"</span><span class="hljs-attribute">Android:toprightradius</span>=<span class="hljs-value">"15px"</span> /> </span></span> <span class="hljs-tag"><span class="hljs-tag"><<span class="hljs-title">solid</span> <span class="hljs-attribute">android:color</span>=<span class="hljs-value">"@color/dialog_background"</span>/></span></span><span class="hljs-tag"><span class="hljs-tag"></<span class="hljs-title">shape</span>></span></span></code></p></pre><p><p>And then write our layout file, The layout is mainly two textview, two button,textview mainly used to display the title, and cue information, the title is centered display, the section can use gravity and layout_gravity together to achieve, button is the bottom button to see, two button width of 1:1, this time can be implemented with layout_weight, and then the button has a 1-pixel high gray cutting line, button between the 1-pixel Gray cut line, This can be used to set the width and height of the view, and then set the background color can be, and button also has the effect of clicking, this section one uses selector Implementation.</p></p><pre class="prettyprint"><code class=" hljs xml"><span class="hljs-tag"><span class="hljs-tag"><<span class="hljs-title">linearlayout</span> <span class="hljs-attribute">xmlns:android</span>=<span class="hljs-value">"http://schemas.android.com/apk/res/android"</span> <span class="hljs-attribute"> Xmlns:tools</span>=<span class="hljs-value">"http://schemas.android.com/tools"</span><span class="hljs-attribute">android:layout_width</span>=<span class="hljs-value">"320dp"</span> <span class="hljs-attribute"> Android:layout_height</span>=<span class="hljs-value">"match_parent"</span><span class="hljs-attribute">android:layout_gravity</span>=<span class="hljs-value">"center"</span> <span class="hljs-attribute"> Android:orientation</span>= "<span class="hljs-value">vertical"</span><span class="hljs-attribute">tools:context</span>=<span class="hljs-value">". Mainactivity "</span>> </span></span> <span class="hljs-tag"><<span class="hljs-title"><span class="hljs-title">TextView</span></span> <span class="hljs-attribute"><span class="hljs-attribute">Android:id</span></span>=<span class="hljs-value"><span class="hljs-value">"@+id/title"</span></span> <span class="hljs-attribute"><span class="hljs-attribute">Android:layout_width</span></span>=<span class="hljs-value"><span class="hljs-value">"wrap_content"</span></span> <span class="hljs-attribute"><span class="hljs-attribute">Android:layout_height</span></span>=<span class="hljs-value"><span class="hljs-value">"50dip"</span></span> <span class="hljs-attribute"><span class="hljs-attribute">android:layout_gravity</span></span>=<span class="hljs-value"><span class="hljs-value">"center"</span></span> <span class="hljs-attribute"><span class="hljs-attribute">android:gravity</span></span>=<span class="hljs-value"><span class="hljs-value">"center"</span></span> <span class="hljs-attribute"><span class="hljs-attribute">Android:text</span></span>=<span class="hljs-value"><span class="hljs-value">"exit"</span></span> <span class="hljs-attribute"><span class="hljs-attribute">Android:textcolor</span></span>=<span class="hljs-value"><span class="hljs-value">"@color/dialog_text"</span></span> <span class="hljs-attribute"><span class="hljs-attribute">android:textsize</span></span>=<span class="hljs-value"><span class="hljs-value">"18sp"</span></span> <span class="hljs-attribute"><span class="hljs-attribute">Android:textstyle</span></span>=<span class="hljs-value"><span class="hljs-value">"bold"</span></span>/></span> <span class="hljs-tag"><<span class="hljs-title"><span class="hljs-title">TextView</span></span> <span class="hljs-attribute"><span class="hljs-attribute">Android:id</span></span>=<span class="hljs-value"><span class="hljs-value">"@+id/content"</span></span> <span class="hljs-attribute"><span class="hljs-attribute">Android:layout_width</span></span>=<span class="hljs-value"><span class="hljs-value">"wrap_content"</span></span> <span class="hljs-attribute"><span class="hljs-attribute">Android:layout_height</span></span>=<span class="hljs-value"><span class="hljs-value">"60sp"</span></span> <span class="hljs-attribute"><span class="hljs-attribute">Android:paddingleft</span></span>=<span class="hljs-value"><span class="hljs-value">"10dip"</span></span> <span class="hljs-attribute"><span class="hljs-attribute">Android:paddingright</span></span>=<span class="hljs-value"><span class="hljs-value">"10dip"</span></span> <span class="hljs-attribute"><span class="hljs-attribute">android:gravity</span></span>=<span class="hljs-value"><span class="hljs-value">"center_vertical"</span></span> <span class="hljs-attribute"><span class="hljs-attribute">Android:text</span></span>=<span class="hljs-value"><span class="hljs-value">"are you sure you want to exit?"</span></span> <span class="hljs-attribute"><span class="hljs-attribute">Android:textcolor</span></span>=<span class="hljs-value"><span class="hljs-value">"@color/dialog_text"</span></span> <span class="hljs-attribute"><span class="hljs-attribute">android:textsize</span></span>=<span class="hljs-value"><span class="hljs-value">"16sp"</span></span>/></span> <span class="hljs-tag"><span class="hljs-tag"><<span class="hljs-title">View</span><span class="hljs-attribute">android:layout_width</span>= "<span class="hljs-value">match_parent"</span><span class="hljs-attribute">android:layout_height</span>=<span class="hljs-value">" 1px "</span><span class="hljs-attribute">android:background</span>=<span class="hljs-value">" @color/dialog_divider "</span> /> </span></span> <span class="hljs-tag"><span class="hljs-tag"><<span class="hljs-title">linearlayout</span><span class="hljs-attribute">android:layout_width</span>=<span class="hljs-value">"match_parent"</span><span class="hljs-attribute">android:layout_ Height</span>=<span class="hljs-value">"40dp"</span><span class="hljs-attribute">android:layout_alignparentbottom</span>=<span class="hljs-value">"true"</span>> </span></span> <span class="hljs-tag"><span class="hljs-tag"><<span class="hljs-title">button </span> <span class="hljs-attribute">android:id</span>=<span class="hljs-value"> "@+id/dialog_cancel" </span> <span class="hljs-attribute">android:layout_width </span> =<span class="hljs-value"> "wrap_content" </span> <span class="hljs-attribute">android:layout_height </span> =<span class="hljs-value"> "wrap_content" </span> <span class="hljs-attribute">android:layout_weight </span> =<span class="hljs-value"> "1" </span> <span class="hljs-attribute">android:text </span> =<span class="hljs-value"> "cancel" </span> <span class="hljs-attribute">android:textcolor </span> =<span class="hljs-value"> "@color/dialog_btn" </span> <span class="hljs-attribute">android:background </span> =<span class="hljs-value"> "@drawable/dialog_left_btn_ Selector "</span>/> </span></span> <span class="hljs-tag"><span class="hljs-tag"><<span class="hljs-title">view </span> <span class="hljs-attribute">android:layout_wid th </span> =<span class="hljs-value"> "1px" </span> <span class="hljs-attribute">android:layout_height </span> =<span class="hljs-value"> "match_parent" </span> <span class="hljs-attribute">android:background </span> =<span class="h Ljs-value ">" @color/dialog_divider "</span>/> </span></span> <span class="hljs-tag"><span class="hljs-tag"><<span class="hljs-title">button</span><span class="hljs-attribute">android:id</span>= "<span class="hljs-value">@+id/dialog_confirm"</span><span class="hljs-attribute">android:layout_width</span>=<span class="hljs-value">" Wrap_content "</span><span class="hljs-attribute">android:layout_height</span>=<span class="hljs-value">" Wrap_content "</span><span class="hljs-attribute">android:layout_weight</span>=<span class="hljs-value">" 1 " </span> <span class="hljs-attribute">Android:text</span>=<span class="hljs-value">"ok"</span><span class="hljs-attribute">android:textcolor</span>=<span class="hljs-value">"@color/dialog_btn"</span> <span class="hljs-attribute"> Android:background</span>=<span class="hljs-value">"@drawable/dialog_right_btn_selector"</span> /> </span></span> <span class="hljs-tag"><span class="hljs-tag"></<span class="hljs-title">linearlayout</span>></span></span><span class="hljs-tag"><span class="hljs-tag"></<span class="hljs-title">linearlayout</span>></span></span></code></pre><p><p>We also write selector file, divided into two, one is the effect of the button on the left, one is the effect of the button on the right, why use two, because the two buttons have a corner is rounded, and the position of the fillet is different, one is the lower left corner, one is the lower right corner, So we need two of these files, here we take the left for example, the right side of the implementation of the same effect.<br>First write the background shape under normal state</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs xml"><span class="hljs-pi"><?xml version="1.0" encoding="UTF-8"?</span></code><p><code class=" hljs xml"></code></p><p><code class=" hljs xml">><span class="hljs-tag"><<span class="hljs-title">shape</span> <span class="hljs-attribute">xmlns:android</span>=<span class="hljs-value">"http://schemas.android.com/apk/res/android"</span> <span class="hljs-attribute">android:shape</span>=<span class="hljs-value">"rectangle"</span>></span> <span class="hljs-tag"><<span class="hljs-title">solid</span> <span class="hljs-attribute">android:color</span>=<span class="hljs-value">"#FFFFFF"</span>/></span> <span class="hljs-tag"><<span class="hljs-title">corners</span> <span class="hljs-attribute">android:bottomLeftRadius</span>=<span class="hljs-value">"15px"</span>/></span><span class="hljs-tag"></<span class="hljs-title">shape</span>></span></code></p></pre></pre><p><p>Then write the selected state or the background shape that gets the focus</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs xml"><span class="hljs-pi"><?<p></p><p>xml version="1.0" encoding="UTF-8"?</p></span></code><p><code class=" hljs xml">><span class="hljs-tag"><<span class="hljs-title">shape</span> <span class="hljs-attribute">xmlns:android</span>=<span class="hljs-value">"http://schemas.android.com/apk/res/android"</span> <span class="hljs-attribute">android:shape</span>=<span class="hljs-value">"rectangle"</span>></span> <span class="hljs-tag"><<span class="hljs-title">solid</span> <span class="hljs-attribute">android:color</span>=<span class="hljs-value">"#EAEAEA"</span>/></span> <span class="hljs-tag"><<span class="hljs-title">corners</span> <span class="hljs-attribute">android:bottomLeftRadius</span>=<span class="hljs-value">"15px"</span>/></span><span class="hljs-tag"></<span class="hljs-title">shape</span>></span></code></p></pre></pre><p><p>Finally write selector</p></p><pre class="prettyprint"><code class=" hljs xml"><span class="hljs-pi"><span class="hljs-pi"><?xml version= "1.0" encoding= "utf-8"?></span></span><span class="hljs-tag"><span class="hljs-tag"><<span class="hljs-title">selector</span> <span class="hljs-attribute">xmlns:android</span>=<span class="hljs-value">"http://schemas.android.com/apk/res/android"</span>></span></span> <span class="hljs-tag"><span class="hljs-tag"><<span class="hljs-title">item</span> <span class="hljs-attribute">android:state_pressed</span>=<span class="hljs-value">"true"</span> <span class="hljs-attribute">android:drawable</span>=<span class="hljs-value">"@drawable/ dialog_left_btn_pressed "</span>/></span></span> <span class="hljs-tag"><span class="hljs-tag"><<span class="hljs-title">item</span> <span class="hljs-attribute">android:state_focused</span>=<span class="hljs-value">"true"</span> <span class="hljs-attribute">android:drawable</span>=<span class="hljs-value">"@ drawable/dialog_left_btn_pressed "</span>/></span></span> <span class="hljs-tag"><span class="hljs-tag"><<span class="hljs-title">item</span> <span class="hljs-attribute">android:drawable</span>=<span class="hljs-value">"@drawable/dialog_left_btn"</span>/></span></span><span class="hljs-tag"><span class="hljs-tag"></<span class="hljs-title">selector</span>></span></span></code></pre><p><p>For the background of the button on the right, just change the corresponding left to just right, and there is no code.</p></p><p><p>In addition, we have to write a default style, we want to remove the default dialog annoying title, completely use our own layout, and some other settings</p></p><pre class="prettyprint"><code class=" hljs applescript"><style<span class="hljs-property"><span class="hljs-property">name</span></span>=<span class="hljs-string"><span class="hljs-string">"exitdialog"</span></span>Parent=<span class="hljs-string"><span class="hljs-string">"@android: style/theme.dialog"</span></span>> <<span class="hljs-property"><span class="hljs-property">Item</span></span> <span class="hljs-property"><span class="hljs-property">name</span></span>=<span class="hljs-string"><span class="hljs-string">"android:windowframe"</span></span>> @null </<span class="hljs-property"><span class="hljs-property">Item</span></span>> <<span class="hljs-property"><span class="hljs-property">Item</span></span> <span class="hljs-property"><span class="hljs-property">name</span></span>=<span class="hljs-string"><span class="hljs-string">"android:windowisfloating"</span></span>><span class="hljs-constant"><span class="hljs-constant">true</span></span></<span class="hljs-property"><span class="hljs-property">Item</span></span>> <<span class="hljs-property"><span class="hljs-property">Item</span></span> <span class="hljs-property"><span class="hljs-property">name</span></span>=<span class="hljs-string"><span class="hljs-string">"android:windowistranslucent"</span></span>><span class="hljs-constant"><span class="hljs-constant">false</span></span></<span class="hljs-property"><span class="hljs-property">Item</span></span>> <<span class="hljs-property"><span class="hljs-property">Item</span></span> <span class="hljs-property"><span class="hljs-property">name</span></span>=<span class="hljs-string"><span class="hljs-string">"android:windownotitle"</span></span>><span class="hljs-constant"><span class="hljs-constant">true</span></span></<span class="hljs-property"><span class="hljs-property">Item</span></span>> <<span class="hljs-property"><span class="hljs-property">Item</span></span> <span class="hljs-property"><span class="hljs-property">name</span></span>=<span class="hljs-string"><span class="hljs-string">"android:windowbackground"</span></span>> @drawable/dialog_bg</<span class="hljs-property"><span class="hljs-property">Item</span></span>> <<span class="hljs-property"><span class="hljs-property">Item</span></span> <span class="hljs-property"><span class="hljs-property">name</span></span>=<span class="hljs-string"><span class="hljs-string">"android:backgrounddimenabled"</span></span>><span class="hljs-constant"><span class="hljs-constant">true</span></span></<span class="hljs-property"><span class="hljs-property">Item</span></span>> </style></code></pre><p><p>At this point, we create a new Exitdialog class to inherit the dialog class, call the style we wrote in the constructor, and assign values to the context object, override the OnCreate method, set our layout</p></p><pre class="prettyprint"><code class=" hljs java"><span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-class"><span class="hljs-class"> <span class="hljs-keyword">class</span> <span class="hljs-title">exitdialog</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Dialog</span> {</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Private</span></span>Context mcontext;<span class="hljs-keyword"><span class="hljs-keyword">Private</span></span>Button mconfirm;<span class="hljs-keyword"><span class="hljs-keyword">Private</span></span>Button mcancel;<span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-title"><span class="hljs-title">Exitdialog</span></span>(context Context) {<span class="hljs-keyword"><span class="hljs-keyword">Super</span></span>(context,r.style.exitdialog); mcontext=context; }<span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-title"><span class="hljs-title">Exitdialog</span></span>(context context,<span class="hljs-keyword"><span class="hljs-keyword">int</span></span>Theme) {<span class="hljs-keyword"><span class="hljs-keyword">Super</span></span>(context, theme); mcontext=context; }<span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword">protected</span></span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">onCreate</span></span>(Bundle Savedinstancestate) {<span class="hljs-keyword"><span class="hljs-keyword">Super</span></span>. onCreate (savedinstancestate); Setcontentview (r.layout.layout_dialog); }}</code></pre><p><p>And we're going to let this diaglog click outside of the dialog box does not disappear, we need to set Setcanceledontouchoutside (false). then, We need to set the click event, Click OK button to exit the app, Click Cancel the current dialog box will disappear</p></p><pre class="prettyprint"><code class=" hljs java"><span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword">protected</span></span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">onCreate</span></span>(Bundle Savedinstancestate) {<span class="hljs-keyword"><span class="hljs-keyword">Super</span></span>. onCreate (savedinstancestate); Setcontentview (r.layout.layout_dialog);<span class="hljs-comment"><span class="hljs-comment">//set to our layout</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> this</span>. Setcanceledontouchoutside (<span class="hljs-keyword"><span class="hljs-keyword">false</span></span>);<span class="hljs-comment"><span class="hljs-comment">//set to the area outside of the Click dialog box does not disappear</span></span>mconfirm= (Button) Findviewbyid (r.id.dialog_confirm); mcancel= (Button) Findviewbyid (r.id.dialog_cancel);<span class="hljs-comment"><span class="hljs-comment">//set Events</span></span>Mconfirm.setonclicklistener (<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>View.onclicklistener () {<span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">OnClick</span></span>(View V) {system.exit (<span class="hljs-number"><span class="hljs-number">0</span></span>); } }); Mcancel.setonclicklistener (<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>View.onclicklistener () {<span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">OnClick</span></span>(View V) {exitdialog.<span class="hljs-keyword"><span class="hljs-keyword"></span> this</span>. Dismiss (); } }); }</code></pre>Use<p><p>Implementation is also very easy, between the context object is passed into the construction of a exitdialog object, called the Show method display can be</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs cs">ExitDialog dialog=<span class="hljs-keyword">new</span> ExitDialog(MainActivity.<span class="hljs-keyword">this</span>);dialog.show();</code></pre></pre>Source code Download<p><p>http://download.csdn.net/detail/sbsujjbcy/8842547</p></p> <p><p>Android self-defined dialog box to implement QQ exit interface</p></p></span>

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.