`
yezhiqiu-love
  • 浏览: 166412 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

How to display a custom dialog in your Android application

阅读更多

How to display a custom dialog in your Android application

Yesterday Jozsi showed you, how to make an alert dialog, today I'm going to show you, how to make a custom dialog/popup window.
Sometimes, it's better to make your own dialog, because this way, you can display whatewer you want., the way you want it.
First, make your own layout, with the needed elements. Here, I'm going to use two buttons, a textview inside a scrollview, and an imageview...

Here is my main layout, main.xml. It's just a textview, with a button:
1

  1. <? xml version= "1.0" encoding= "utf-8" ?>
  2.  
  3. < RelativeLayout android: id= "@+id/RelativeLayout01&quot;
  4. android: layout_width= "fill_parent"
  5. android: layout_height= "fill_parent"
  6. xmlns: android= "http://schemas.android.com/apk/res/android" >
  7.  
  8. < TextView android: id= "@+id/TextView01"
  9. android: layout_width= "wrap_content"
  10. android: layout_height= "wrap_content"
  11. android: text= "This is my main activity, from here, I want to display a dialog, after the user clicked the button below this text." >
  12. </ TextView>
  13.  
  14. < Button android: layout_height= "wrap_content"
  15. android: layout_below= "@+id/TextView01"
  16. android: layout_width= "wrap_content"
  17. android: id= "@+id/Button01main"
  18. android: text= "Hey! There is more..." ></ Button>
  19.  
  20. </ RelativeLayout>

Here is my dialog's layout, maindialog.xml:

  1. <? xml version= "1.0" encoding= "utf-8" ?>
  2. < RelativeLayout xmlns: android= "http://schemas.android.com/apk/res/android"
  3.  android: layout_width= "wrap_content" android: layout_height= "wrap_content" >
  4.  
  5. < ImageView android: id= "@+id/ImageView01"
  6.  android: layout_width= "wrap_content" android: layout_height= "wrap_content"
  7.  android: layout_centerHorizontal= "true" />
  8.  
  9.  < ScrollView android: id= "@+id/ScrollView01"
  10.  android: layout_width= "wrap_content" android: layout_below= "@+id/ImageView01"
  11.  android: layout_height= "200px" >
  12.  
  13.  < TextView android: text= "@+id/TextView01" android: id= "@+id/TextView01"
  14.  android: layout_width= "wrap_content" android: layout_height= "wrap_content" />
  15.  
  16.  </ ScrollView>
  17.  
  18.  < Button android: id= "@+id/Button01" android: layout_below= "@id/ScrollView01"
  19.  android: layout_width= "wrap_content" android: layout_height= "wrap_content"
  20.  android: layout_centerHorizontal= "true" android: text= "Cancel" />
  21.  
  22. </ RelativeLayout>

Now that the xml part is all set up, it's time to code.

  1. public class main extends Activity {
  2.     @Override
  3.     public void onCreate( Bundle savedInstanceState) {
  4.         super .onCreate ( savedInstanceState) ;
  5.         //set up main content view
  6.         setContentView( R.layout .main ) ;
  7.         //this button will show the dialog
  8.         Button button1main = ( Button ) findViewById( R.id .Button01main ) ;
  9.  
  10.         button1main.setOnClickListener ( new OnClickListener( ) {
  11.         @Override
  12.             public void onClick( View v) {
  13.                 //set up dialog
  14.                 Dialog dialog = new Dialog ( main.this ) ;
  15.                 dialog.setContentView ( R.layout .maindialog ) ;
  16.                 dialog.setTitle ( "This is my custom dialog box" ) ;
  17.                 dialog.setCancelable ( true ) ;
  18.                 //there are a lot of settings, for dialog, check them all out!
  19.  
  20.                 //set up text
  21.                 TextView text = ( TextView) dialog.findViewById ( R.id .TextView01 ) ;
  22.                 text.setText ( R.string .lots_of_text ) ;
  23.  
  24.                 //set up image view
  25.                 ImageView img = ( ImageView) dialog.findViewById ( R.id .ImageView01 ) ;
  26.                 img.setImageResource ( R.drawable .nista_logo ) ;
  27.  
  28.                 //set up button
  29.                 Button button = ( Button ) dialog.findViewById ( R.id .Button01 ) ;
  30.                 button.setOnClickListener ( new OnClickListener( ) {
  31.                 @Override
  32.                     public void onClick( View v) {
  33.                         finish( ) ;
  34.                     }
  35.                 } ) ;
  36.                 //now that the dialog is set up, it's time to show it    
  37.                 dialog.show ( ) ;
  38.             }
  39.         } ) ;
  40.     }
  41.  }

2

分享到:
评论

相关推荐

    Introduction.to.Android.Application.Development(4th,2013.12) pdf

    Adding Logging Support to Your Android Application 83 Adding Some Media Support to Your Application 84 Adding Location-Based Services to Your Application 88 Debugging Your Application on Hardware 90 ...

    UE(官方下载)

    The benefit of a column maker is that it can help you to format your text/code, or in some cases to make it easier to read in complex nested logic. Quick Open UltraEdit and UEStudio provide multiple ...

    Visual C++ 编程资源大全(英文源码 控件)

    you'll need to install a Windows keyboard hook if you want to get keyboard input to your dialog without other controls getting in the way. (19KB)&lt;END&gt;&lt;br&gt;46,treestore.zip Tree views are capable of...

    Swing Hacks

    This chapter covers four different ways to play sound, how to display waveforms, and how to embed MP3 support in your own programs. Chapter 11, Native Integration and Packaging The best software ...

    VB编程资源大全(英文源码 控制)

    start your Visual Basic program with your project code showing, right click and you should see "Rem Builder".&lt;END&gt;&lt;br&gt;3 , syntax.zip This is an excellent example of how to highlight HTML code in a ...

    Visual C++ 编程资源大全(英文源码 表单)

    (156KB)&lt;END&gt;&lt;br&gt;90,Dialog2.zip A step by step tutorial showing how to create your first windows program using MFC(112KB)&lt;END&gt;&lt;br&gt;91,MyMDIApp.zip A brief step-by-step tutorial that demonstrates ...

    VB编程资源大全(英文源码 其它)

    stopwatch.zip This shows how to count off time in a Stop Watch format.&lt;END&gt;&lt;br&gt;31 , taskhide.zip This will hide your application from the taskbar, Alt+Tab, and Alt+Ctrl+Del.&lt;END&gt;&lt;br&gt;32 , newbie...

    VB编程资源大全(英文源码 控件)

    1 , vb5dialog.zip This demonstrates how to subclass the Common Dialog Dialogs and manipulate a specific Dialog.&lt;END&gt;&lt;br&gt;2 , cpnl.zip Form_Taskbar is a control for Visual Basic which, once placed...

    端口查看工具

    whether you want to clear the log, in order to avoid from clearing the log file by mistake. * Version 1.91: o Added 'Beep On New Ports' option. * Version 1.90: o Added 'Tray Balloon On New Ports...

    Visual C++ 编程资源大全(英文源码 其它)

    demo.zip How to send debugging output to a console in a MFC application(12KB)&lt;END&gt;&lt;br&gt;56,statuslog.zip A very simple text logger that allows you to use printf-type formatting, with automatic ...

    数位板压力测试

    This specification was developed in response to a perceived need for a standardized programming inter-face to digitizing tablets, three dimensional position sensors, and other pointing devices by a ...

    VB编程资源大全(英文控件)

    labelblink.zip Blinks The Caption In A Label Box&lt;END&gt;&lt;br&gt;22,UnZipper.zip ActiveX control designed to easily manipulate ZIP archives from within your application. &lt;END&gt;&lt;br&gt;23,gurhancoolbuttonocx....

    BCGControlBarPro.v12.00

    The Ribbon bar Application ("main") Button can display a text label instead of icon in the "scenic" mode (usually "File" in English language UI). The following new methods were added to the ...

    SQLMemTable for Delphi / C++ Builder

    - Attach a test project where the problem could be reproduced (it helps us to solve your issue as soon as possible)Typically AidAim Software Support Team answer messages in 24 hours, but depending on...

    ICS delphixe10源码版

    ICS V9 is in early development and is planned to support Android. There are no current plans for ICS for iOS. Version Control repository: --------------------------- svn://svn.overbyte.be/ics or ...

    Visual C++ 编程资源大全(英文控件)

    扩展型多尺寸多色彩工具条(226KB)&lt;END&gt;&lt;br&gt;15,15.zip How to display tooltips for a toolbar in a dialog 在对话框如何为工具条显示工具提示(3KB)&lt;END&gt;&lt;br&gt;16,16.zip Displaying text on a Toolbar ...

Global site tag (gtag.js) - Google Analytics