android Dialog Custom-triggered murders

Source: Internet
Author: User

I'm still working from the ground up! Recently, you need to add a voice-over camera to the cameras, which you need to achieve in your settings:

The "Voice capture" menu features that are set up are described below:

(1) When you click on the "Voice Photo" menu will pop up as shown in the dialog, click on the dialog inside the "photo"/"eggplant" will automatically play the sound.

(2) When the dialog appears, the dialog will automatically disappear as soon as you click outside the dialog area.

(3) When you click the Green Switch button at the far right of the "Voice capture" menu, the Voice capture function is turned on/off.

The above implementation is actually a preferenceactivity, so our new "voice capture" menu is added in this preferenceactivity (camerasettingactivity).

First of all, what we need to solve is how to create a Dialog of our own definition: In activity there is a method public Dialog oncreatedialog (int dialogid), so that he can create his own Dialog, The public final void ShowDialog (int id) of the activity is then called to display the dialog we created. The activity here is based on different dialogid to create and display different dialog, and dialogID is your own definition! In our camerasettingactivity as defined below

private static final int dialog_id_voice_command_show_tones = 111;
Then override the parent activity's Oncreatedialog method to define your own dialog, as in the following code:

    @Override public Dialog oncreatedialog (int dialogid) {if (dialogID = = dialog_id_voice_command_show_tones) {Di Alog Dialog = New dialog (this, R.style.transparent_dialog_them); Dialog.setcontentview (r.layout.setting_switch_sublist_layout); Voicemanager Voice_manager = ((Cameraapp) CameraSettingActivity.this.getApplication ()). Getvoicemanager (); Settingswitchsublistlayout mvoicesettinglayout = (settingswitchsublistlayout) Dialog.findviewbyid ( R.ID.SETTINGSWITCHSUBLISTLAYOUT_ID); Mvoicesettinglayout.initialize (Voice_manager.getvoiceentryvalues ()); Mvoicesettinglayout.setsettingchangedlistener (this);//dialog.getwindow (). Setcloseontouchoutside (True); View content = (view) Dialog.getwindow (). Getdecorview (). Findviewbyid (com.android.internal.r.id.content); Content.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {Dismissdia            Log (dialog_id_voice_command_show_tones); }}); return dialog;}  Return Super.oncreatedialog (dialogID);  } 
Here's a step-by-step explanation of the above code:

(1)

Second This parameter: R.style.transparent_dialog_them is a theme set parameters, where you need to set the background transparency, and so on, as shown in the following code:

 <style Name= "Transparent_dialog_them" parent= "@style/theme.hwdroid.ali.noactionbar" > <item name= "android: Windowbackground "> @android:color/transparent</item> <item name=" Android:colorbackgroundcachehint "  > @null </item> <item name= "android:windowistranslucent" >true</item> <!--Note that we             Use the base animation style here (so is no animations) because we really has no idea how this kind of Activity would be used. --<item name= "Android:windowanimationstyle" > @android:style/animation</item> </style>
Here I set the windowbackground to transparent, and also set the mode to Noactionbar and Notitle! Of course here windowbackground can actually be set to do transparent background! Do you see anything strange from here? Why create a dialog and window/action and so what is the relationship? If you look at the class implementation classes implemented by dialog, you will find that creating a dialog is tantamount to creating a window, and we know that a window has action,title properties! By learning activity We also know that creating an activity actually creates a window, in fact, the display of an interface originates from a window! A window In addition to the display of the management interface, in fact, all the device input events are starting from here!

(2)

Dialog.setcontentview (r.layout.setting_switch_sublist_layout);
This line of code is actually the configuration of the dialog layout that we show! Inside the details are very simple, not to say more!

(3)

View content = (view) Dialog.getwindow (). Getdecorview (). Findviewbyid (com.android.internal.r.id.content); Content.setonclicklistener (New Onclicklistener () {            @Override public            void OnClick (View v) {DismissDialog ( dialog_id_voice_command_show_tones);            }        );
These lines of code, can be implemented, when the user clicks outside the dialog area, dialog automatically disappears!

The above is about the creation of dialog! Do you think there's a problem?

Then, let's look at how to implement a click: When you click on the "Voice capture" menu, the dialog appears, and when you click the Green Switch button on the far right of the "Voice Photo" menu, the Voice Capture feature is turned on/off. Look at the following code:

        Switchpreference voicepref = (switchpreference) findpreference (getString (r.string.camera_setting_item_pref_voice_ Key));
Voicepref.setlayoutresource (r.layout.xunhu_voice_preference);//xunhu_ali_preferencestring key = GetString ( R.string.camera_setting_item_pref_voice_key); Boolean value = Getpreferencemanager (). Getsharedpreferences (). Getboolean (key, false);//LOG.D ("Pre_carmera", "Camerasettingactivity:initdefaultsavepath mvalue=" +value); Voicemanager Voice_manager = ((Cameraapp) this.getapplication ()). Getvoicemanager (); String Mvalue = Voice_manager.getvoicevalue ();
From the above code can be clearly known, in fact, I used the usual we use the switchpreference, just to define his layout it! Switchpreference method Setlayoutresource You can configure your own layout! So the key to solving this problem is on the use of this layout!

In fact, this layout is no different from the default switchpreference layout! Just add a property to the parent view of this layout android:onclick= "Voicecommandclicklistener" as shown in the following code:

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"     android:layout_width= "Match_ Parent "    android:layout_height=" wrap_content "    android:minheight="? Android:attr/listpreferreditemheight "    android:gravity= "center_vertical"    android:paddingend= "android:attr/scrollbarsize"    android:o nclick= "Voicecommandclicklistener"

Finally look at the definition of Voicecommandclicklistener:

public void Voicecommandclicklistener (View v) {        showDialog (dialog_id_voice_command_show_tones);}
is actually calling ShowDialog to show my own dialog.


When you get here, you will soon find that there are two questions: (1) We achieve when the click Dialog outside the area, dialog automatically disappear, whether there is a simple way! (2) Can we use Dialogfragment to replace the dialog here?

One question to solve first:

By looking at Dialog.java, you can see that the creation of a dialog actually creates a window for this dialog, and through the tool Hierarchyviewer, you will find that this dialog actually occupies the entire screen, Instead of just showing the area so much! Why so! This is what you will soon find, when an activity becomes Dialog mode, you need to configure the activity theme to Dialog mode: Theme.dialog. Just now, a dialog and activity show is the origin and a window, then dialog created when the theme is Theme.dialog can solve the problem! The actual result is indeed the same as mine!

Modify the above code as follows:

    @Override public Dialog oncreatedialog (int dialogid) {if (dialogID = = dialog_id_voice_command_show_tones) {Di Alog Dialog = New dialog (this, R.style.transparent_dialog_them); Dialog.setcontentview (r.layout.setting_switch_sublist_layout); Voicemanager Voice_manager = ((Cameraapp) CameraSettingActivity.this.getApplication ()). Getvoicemanager (); Settingswitchsublistlayout mvoicesettinglayout = (settingswitchsublistlayout) Dialog.findviewbyid ( R.ID.SETTINGSWITCHSUBLISTLAYOUT_ID); Mvoicesettinglayout.initialize (Voice_manager.getvoiceentryvalues ()); Mvoicesettinglayout.setsettingchangedlistener (This);d Ialog.getwindow (). Setcloseontouchoutside (true);/*View Content = (View) Dialog.getwindow (). Getdecorview (). Findviewbyid (com.android.internal.r.id.content); Content.setonclicklistener (New Onclicklistener () {@Override public void oncli                       CK (View v) {dismissdialog (dialog_id_voice_command_show_tones);               }        }); */dialoG.setcanceledontouchoutside (True); return dialog;}    Return Super.oncreatedialog (dialogID); }
The above code I annotated content.setonclicklistener This section of the content, the new add the following line of code:
Dialog.getwindow (). Setcloseontouchoutside (True);
You will find that when you click outside of the dialog, dialog simply can't disappear! Is this line of code above not work! In fact, just now we said, this dialog is actually occupy the entire screen! Only we set the background to full transparency! So at this point you simply can't click outside the dialog area! To prove that the above line of code is available! You only need to modify the theme settings! Configure the above R.style.transparent_dialog_them as: Theme.dialog: Change to the following:

    <style name= "Transparent_dialog_them" parent= "@style/theme.hwdroid.ali.dialog.noactionbar" >    <item Name= "Android:windowbackground" > @android:color/transparent</item>        <item name= "Android: Colorbackgroundcachehint "> @null </item>        <item name=" Android:windowistranslucent ">true</ Item>        <!--Note that we use the Base animation style here (that's no             animations) because we really have no Idea how this kind of             activity would be used.-        <item name= "Android:windowanimationstyle" > @android: Style/animation</item>    </style>
The original Transparent_dialog_them's father is: Theme.HWDroid.Ali.NoActionBar, now changed to: Theme.HWDroid.Ali.Dialog.NoActionBar. This is the solution to the question.
Take a look at the next question:
First open the Activity.java to see the dialog related to the source code, which has a description of dialog as follows:

     * @deprecated use of the new {@link dialogfragment} class with     * {@link fragmentmanager} instead; it is also     * Avai Lable on older platforms through the Android compatibility package.
It is clear that the use of dialog is not recommended, but it is recommended that you use Dialogfragment. Take a look at the Dialogfragment source code to know that in fact dialogfragment is a fragment, that is, the proposal to build with fragment to solve this problem!

So everyone will fragment, also will be dialogfragment, in fact Dialogfragment is encapsulated dialog fragment. The following code directly, to see how the adoption of dialogfragment implementation:

public void Voicecommandclicklistener (View v) {        //showdialog (dialog_id_voice_command_show_tones); Fragmenttransaction ft = Getfragmentmanager (). BeginTransaction (); Voicecommanddialogfragment prev = (voicecommanddialogfragment) Getfragmentmanager (). Findfragmentbytag ("Voice_ Dialog "); if (prev! = null) {ft.show (prev);} Else{prev = Voicecommanddialogfragment.newinstance ();p rev.show (ft, "voice_dialog");}}
The above code is called when the dialogfragment is displayed. Here's a look at the custom dialogfragment:voicecommanddialogfragment

    Private static final class Voicecommanddialogfragment extends Dialogfragment {//private static VOICECOMMANDDIALOGFRAGM ENT f;//private static Final Object mLock = new Object ();p rivate static class Singletonholder {private static Voicecommand Dialogfragment f = new voicecommanddialogfragment ();} /* Static voicecommanddialogfragment newinstance () {if (null = = f) {synchronized (MLock) {if (null =             = f) {f = new voicecommanddialogfragment ();}}               } return F; } */public static voicecommanddialogfragment newinstance () {/* if (null = = f) {f = new Voice        Commanddialogfragment ();            } return F;        */return SINGLETONHOLDER.F; } @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (Savedinstanc            EState);            Pick a style based on the Num. int style = Dialogfragment.style_no_title, theme = R.stylE.transparent_dialog_them;        SetStyle (style, theme); } @Override Public View oncreateview (layoutinflater inflater, ViewGroup container, Bundle sav edinstancestate) {View v = inflater.inflate (R.layout.setting_switch_sublist_layout, container, false); Voicemanager Voice_manager = ((Cameraapp) getactivity (). Getapplication ()). Getvoicemanager (); Settingswitchsublistlayout mvoicesettinglayout = (settingswitchsublistlayout) V.findviewbyid ( R.ID.SETTINGSWITCHSUBLISTLAYOUT_ID); Mvoicesettinglayout.initialize (Voice_manager.getvoiceentryvalues ());            Mvoicesettinglayout.setsettingchangedlistener ((camerasettingactivity) getactivity ());        return v; }    }
The above code needs to be aware of the following three points:

(1) You need to create the dialogfragment problem repeatedly, here we use the singleton mode. Here I use the internal class to solve;

(2) When the display, need to detect the current fragmentmanager inside whether there is a presence I need to display the dialogfragment;

(3) As above, the theme setting of the device must be set to Dialog_them;

That's it.

android Dialog Custom-triggered murders

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.