VS/Xamarin Android入門(對話框控件)三


一、對話框和提示信息

  一、對話框

      我們首先從簡單的對話框開始。

      1、普通對話框

      在android里面,對話框用的是AlertDialog,這個呢,其實就和winform里面的MessageBox一樣的。最簡單的

AlertDialog.Builder ad_build =  new  AlertDialog.Builder( this )
                               .SetTitle(Resource.String.warming) //標題(警告)
                               .SetMessage(Resource.String.info) //獲取本地string.xml定義的數據
                               .SetNegativeButton( "確定" this )
                               .SetPositiveButton( "取消" this )
                               .SetNeutralButton( "中間按鈕" this )
                               .SetIcon(Android.Resource.Drawable.StatSysWarning);
                     ad_build.Show();

       其中,SetNeutralButton這里是設置的一個中間按鈕,這個東西,可以有,也可以沒有,代碼可以直接添加到程序里面運行即可。在代碼中,我們可以看到提示信息里面的,  獲取本地strings.xml定義的數據。我們可以看下本地的xml數據。

<?xml version= "1.0"  encoding= "utf-8" ?>
<resources>
   < string  name= "myapp" >爺的APP</ string >
   < string  name= "info" >您確定結束本次任務?</ string >
   < string  name= "yesorno" >確定要退出嗎?</ string >
   < string  name= "warming" >警告</ string >
   < string  name= "jiazai" >正在加載……</ string >
   < string  name= "Img_Info" >您有一個未接電話</ string >
   < string  name= "MyToast" >自定義提示信息</ string >
</resources>

      在這里,不得不說一下,這里的SetIcon的問題,這里的Android.Resource.Drawable這個是系統自帶的圖片,可能我們有時候需要去訪問本地自己的圖片。

      在dialog顯示gif圖片 由於dialog不容易取到里面空間對象,推薦使用透明樣式的activity,只需把該activity的樣式設置為透明樣式 即android:theme="@android:style/Theme.Translucent",同時在oncreat()方法的setcontenview()之前設置requestWindowFeature(Window.FEATURE_NO_TITLE);去掉標題. 播放gif圖片 由於android控件不支持播放gif。推薦使用webview 里面放入html中含有img標簽 src便是圖片的地址 可以使網絡地址 也可以是本地地址 然后webview加載該html即實現了播放

      2、單選對話框

      單選對話框,還用的是AlertDialog

AlertDialog.Builder ad_build =  new  AlertDialog.Builder( this )
                              .SetTitle(Resource.String.warming) //標題(警告)
                              .SetSingleChoiceItems( new  string [] {  "中國" "日本" "韓國"  }, 0,  this ) //自定義的單選數組
                              .SetNegativeButton( "確定" this )
                              .SetPositiveButton( "取消" this )                          
                                      .SetIcon(Android.Resource.Drawable.StatSysWarning);
                    ad_build.Show();

       在這里,任然可以添加中間按鈕,直接在后面繼續加點,加Set就可以,但是單選要實現 IDialogInterfaceOnClickListener 接口,通過這個接口,我們可以獲取到現在選擇的到底是哪一個的值。其實,單選就是多了一個SetSingleChoiceItems這個參數,然后傳值就可以。

/// <summary>
/// 單選和普通對話框
/// </summary>
/// <param name="dialog"></param>
/// <param name="which"></param>
public  void  OnClick(IDialogInterface dialog,  int  which)
{
     Toast.MakeText( this , which +  "" , ToastLength.Short).Show();
}

       這里的which就是選擇的是哪一個的值,獲取到值一般來說才是最重要的,我們才可以繼續其他的事情。

      3、多選對話框

      多選對話框,還用的是AlertDialog

AlertDialog.Builder ad_build =  new  AlertDialog.Builder( this )
                            .SetTitle(Resource.String.warming) //標題(警告)
                            .SetMultiChoiceItems( new  string [] {  "中國" "日本" "韓國"  },  new  bool [] {  false true true  },  this ) //多選自定義數組
                            .SetNegativeButton( "確定" this )
                            .SetPositiveButton( "取消" this )
                            .SetIcon(Android.Resource.Drawable.StatSysWarning);
                   ad_build.Show();

       其中,多選框實現的 IDialogInterfaceOnMultiChoiceClickListener 是這個接口。

    /// <summary>
    /// 多選接口的實現
    /// </summary>
    /// <param name="dialog"></param>
    /// <param name="which"></param>
    /// <param name="isChecked"></param>
    public  void  OnClick(IDialogInterface dialog,  int  which,  bool  isChecked)
    {
        Toast.MakeText( this , which.ToString() + "    " + isChecked.ToString(), ToastLength.Short).Show();
    }

       同樣的,這里的which是在這個多選框中的唯一ID,后面的isChecked,是否選擇,通過這些,我們就可以獲取到很多信息了。

      4、正在加載對話框

      正在加載用的是ProgressDialog 這個方法,這個方法同樣可以 Builder,但是和SetIcon一樣,如果想采取自定義的圖片,同樣需要前面的圖片自定義的辦法。

ProgressDialog p_dialog =  new  ProgressDialog( this );
p_dialog.SetMessage( "正在加載……" );
p_dialog.Show(); 

       這個效果就是登陸或者其他的那個,如果這里用 ProgressDialog.Builder 也是可以,但是要自定義顯示信息,包括圖片信息等等。

      5、自定義對話框

      這里自定義對話框用的還是AlertDialog,但是不同的是,自定義的對話框,要注意。自定義對話框,要完全自定義布局,也就是說,要完全定義所有的相關信息,這就相當於我們做web的時候,填出一個提示框一樣,在Android里面,要完全彈出自定義對話框,那就需要View,因為所有的界面都是View,直接右鍵添加一個Android Layout就可以,哇咔咔,繼續開始設計。

      我的界面是這樣定義的:

<?xml version= "1.0"  encoding= "utf-8" ?>
<LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android"
     android:orientation= "vertical"
     android:layout_width= "match_parent"
     android:layout_height= "match_parent" >
     <TextView
         android:layout_width= "fill_parent"
         android:layout_height= "wrap_content"
         android:gravity= "center"
         android:text= "系統提示"
         android:background= "#0094FF"
         android:textColor= "#ffffff"  />
     <LinearLayout
         android:layout_width= "fill_parent"
         android:layout_height= "wrap_content"
         android:gravity= "center"
         android:background= "#848484" >
     <!--@android:drawable/stat_notify_missed_call 引用的是系統的圖片-->
         <ImageView
             android:layout_width= "wrap_content"
             android:layout_height= "wrap_content"
             android:src= "@android:drawable/stat_notify_missed_call"  />
         <TextView
             android:layout_width= "wrap_content"
             android:layout_height= "wrap_content"
             android:text= "一個未接電話"  />
     </LinearLayout>
</LinearLayout>

      OK,這里要注意一點 關於 ImageView 的 src 的問題 android:src="@drawable/myapk" 這么寫,引用的本地定義的myapk的圖片,最好能是 png、jpg等等此類的,要是gif的好像還是要重新加載一下,這里的引用也就是自己在 drawable 文件夾添加的圖片的 名字。 @android:drawable/stat_notify_missed_call 這么寫就是引用的 Android SDK文件夾下面的 drawable 的文件,這些文件你得先找到你自己的文件安裝路徑,也就是你定義的SDK的安裝路徑,找到安裝路徑之后,platforms→android-15→data→res→drawable-ldpi 在這個文件夾下面,你就可以看到很多圖片了,如果實在找不到,你還是用Android Studio安裝的話,我告訴一個好消息,SDK Manager (sdk管理器)或者 AVD Manager (虛擬機管理器)的快捷方式,找到安裝的根目錄,這個可以做到吧。

      如圖,找到對應的文件夾,依次按照platforms→android-15→data→res→drawable-ldpi 這個順序往下點擊就可以了,你就能看到你想要的了。

      后台代碼:

                    View view = LayoutInflater.From(this).Inflate(Resource.Layout.MyDialog, null);
                    AlertDialog.Builder a_bulid = new AlertDialog.Builder(this);
                    a_bulid.SetMessage("自定義對話框").SetView(view);
                    a_bulid.Show();

      先用View來接收我剛定義的界面,然后給這個界面設置一個標題,然后直接用 AlertDialog 直接 Show 就可以,其中 LayoutInflater 這就是一點要注意的,這個和本身的 FindViewById就是一個相同的意思,一個找布局文件,一個找界面元素。

      6、列表對話框

      列表對話框用的還是AlertDialog

      

AlertDialog.Builder ad_build =  new  AlertDialog.Builder( this )
                             .SetTitle(Resource.String.warming) //標題(警告)
                             .SetItems( new  string [] {  "中國" "日本" "韓國"  },  this )
                             .SetNegativeButton( "確定" this )
                             .SetPositiveButton( "取消" this )
                             .SetIcon(Android.Resource.Drawable.StatSysWarning);
                    ad_build.Show();

       這里不同的就是列表對話框用的是 SetItems 這個屬性

    二、提示信息

      1、普通提示信息

var  item = Toast.MakeText( this , Resource.String.info, ToastLength.Short);
//設置垂直水平居中
item.SetGravity(GravityFlags.CenterHorizontal | GravityFlags.CenterVertical, 0, 0);
item.Show();

       這里的其實沒有什么注意的,就是一個 SetGravity 設置顯示的位置的屬性。

      2、含圖片提示信息

            var item = Toast.MakeText(this, Resource.String.Img_Info, ToastLength.Short);
                    //創建一個圖片視圖
                    ImageView iv = new ImageView(this);
                    iv.SetImageResource(Android.Resource.Drawable.StatNotifyMissedCall);
                    //得到Toast布局(強制改變為線型布局)
                    LinearLayout toastView = (LinearLayout)item.View;
                    //設置內容顯示位置
                    toastView.SetGravity(GravityFlags.Center);
                    //設置布局的方向
                    //
                    //Orientation.Horizontal 居於屏幕下方
                    //Orientation.Horizontal | Orientation.Vertical
                    //
                    toastView.Orientation = Orientation.Horizontal;
                    //給布局添加一個視圖,並且設置位置
                    toastView.AddView(iv, 0);
                    //顯示Toast
                    item.Show();

      3、完全自定義提示信息

View view = LayoutInflater.From( this ).Inflate(Resource.Layout.MyDialog,  null );
Toast toast =  new  Toast( this );
toast.View = view;
toast.Show();

       看到這里,相信大家都有一個簡單的了解了,我做了一個簡單的反思,就是Android的這個東西,當你要呈現一個新的元素或者其他的任務之類的,都需要去單獨接受,感覺和委托的意思一樣,是這樣嗎?

 

二、布局

     在android里面,不同的像素密度,一般使用的是dip做單位的,文字使用的是sp

            AbsoluteLayout 絕對布局(所有的信息都是寫死的)

            FramerLayout 幀布局 這個布局所有的東西都是從左上角開始的,就是會疊加顯示,不會像div那樣擠壓

            LinearLayout  線程布局(默認)默認從上到下依次
            android:orientation 設置布局方向  
            horizontal 水平均分
            layout_weight 在相同的情況下,呈現的是正好是對立的狀態,在同一個線型布局里面就可以看到,

            帶有layout_的都指的的是父控件的樣式
            如 layout_gravity 指的是自己在父控件里面對齊樣式 gravity    就是本身自己的樣式

            RelativeLayout 該布局是參照父控件或者是其他控件的位置進行布局。比如說我要把A控件 ,放到B控件的下面,並且A控件的右邊與B的左邊對齊。類似這樣的布局就可以使用相對布局來完成比較容易

            fill_parent 填滿當前視圖,在2.2之后的android版本 match_parent 即可(相同的意思)
            wrap_content 設置一個視圖的尺寸為wrap_content將強制性地使視圖擴展以顯示全部內容。

    TableLayout 布局頁面

     單元格屬性:
          android:layout_column:指定該單元格在第幾列顯示(從0開始)
          android:layout_span:跨列(意思就是當前的控件占據單元格多少列)

          列屬性:
          android:stretchColumns    設置可伸展的列。該列可以向行方向伸展,最多可占據一整行。
          android:shrinkColumns    設置可收縮的列。當該列子控件的內容太多,已經擠滿所在行,那么該子控件的內容將往列方向顯示。
          android:collapseColumns    設置要隱藏的列。

    我自己摸索的幾個簡單的布局,可以大家參考,參考

<?xml version= "1.0"  encoding= "utf-8" ?>
<LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android"
     android:layout_width= "match_parent"
     android:layout_height= "wrap_content"
     android:orientation= "horizontal"
     android:id= "@+id/absoluteLayout1" >
<!--android:orientation 設置的線型布局的方向-->
<!--在android里面,不同的像素密度,一般使用的是dip做單位的,文字使用的是sp-->
<!--layout_weight 在相同的情況下,呈現的是正好是對立的狀態-->
     <Button
         android:id= "@+id/MyButton1"
         android:layout_width= "fill_parent"
         android:layout_height= "wrap_content"
         android:text= "按鈕一"
         android:layout_weight= "3"  />
     <Button
         android:id= "@+id/MyButton2"
         android:layout_width= "fill_parent"
         android:layout_height= "wrap_content"
         android:text= "按鈕二"
         android:layout_weight= "3"  />
     <Button
         android:id= "@+id/MyButton3"
         android:layout_width= "fill_parent"
         android:layout_height= "wrap_content"
         android:text= "按鈕三"
         android:layout_weight= "3"  />
</LinearLayout>

 效果圖:

 

 

<?xml version= "1.0"  encoding= "utf-8" ?>
<RelativeLayout xmlns:android= "http://schemas.android.com/apk/res/android"
     android:layout_width= "fill_parent"
     android:layout_height= "fill_parent" >
     <EditText
         android:id= "@+id/edit"
         android:layout_width= "fill_parent"
         android:layout_height= "wrap_content"
         android:text= "下面有個按鈕"  />
     <Button
         android:layout_width= "wrap_content"
         android:layout_height= "wrap_content"
         android:text= "按鈕"
         android:layout_below= "@id/edit"
         android:layout_alignRight= "@id/edit"  />
</RelativeLayout>

 效果圖

 

來個帶后台代碼的。  

  

<?xml version= "1.0"  encoding= "utf-8" ?>
<LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android"
     android:orientation= "vertical"
     android:layout_width= "fill_parent"
     android:layout_height= "fill_parent" >
<!--wrap_content 包裹內容-->
<!--horizontal 水平-->
<!--android:layout_height= "wrap_content"  橫向包裹內容-->
     <LinearLayout
         android:orientation= "horizontal"
         android:layout_width= "fill_parent"
         android:layout_height= "wrap_content" >
         <Button
             android:id= "@+id/home"
             android:layout_width= "wrap_content"
             android:layout_height= "wrap_content"
             android:text= "首頁"  />
         <Button
             android:id= "@+id/jiankang"
             android:layout_width= "wrap_content"
             android:layout_height= "wrap_content"
             android:text= "第二個頁面"  />
         <Button
             android:text= "第三個"
             android:layout_width= "wrap_content"
             android:layout_height= "match_parent"
             android:id= "@+id/button1"  />
     </LinearLayout>
     <FrameLayout
         android:layout_width= "fill_parent"
         android:layout_height= "fill_parent" >
         <LinearLayout
             android:id= "@+id/layout1"
             android:layout_width= "fill_parent"
             android:layout_height= "fill_parent" >
             <TextView
                 android:layout_width= "fill_parent"
                 android:layout_height= "fill_parent"
                 android:text= "第一個布局"
                 android:background= "#0094ff"  />
         </LinearLayout>
         <LinearLayout
             android:id= "@+id/layout2"
             android:layout_width= "fill_parent"
             android:layout_height= "fill_parent" >
             <TextView
                 android:layout_width= "fill_parent"
                 android:layout_height= "fill_parent"
                 android:text= "第二個布局"
                 android:background= "#0045ff"  />
         </LinearLayout>
         <LinearLayout
             android:id= "@+id/layout3"
             android:layout_width= "fill_parent"
             android:layout_height= "wrap_content"
             android:orientation= "vertical" >
             <TextView
                 android:layout_width= "wrap_content"
                 android:layout_height= "match_parent"
                 android:text= "第三個布局"
                 android:textSize= "24sp"
                 android:id= "@+id/tv1"  />
             <TextView
                 android:layout_width= "wrap_content"
                 android:layout_height= "match_parent"
                 android:textSize= "36sp"
                 android:id= "@+id/tv2"
                 android:gravity= "center"  />
         </LinearLayout>
     </FrameLayout>
</LinearLayout>

 后台代碼

public  class  MainActivity : Activity, View.IOnClickListener
{
     public  void  OnClick(View v)
     {
         LinearLayout layout3 = FindViewById<LinearLayout>(Resource.Id.layout3);
         LinearLayout layout2 = FindViewById<LinearLayout>(Resource.Id.layout2);
         LinearLayout layout1 = FindViewById<LinearLayout>(Resource.Id.layout1);
         if  (v.Id == Resource.Id.home)
         {
             layout1.Visibility = ViewStates.Visible;
             layout2.Visibility = ViewStates.Invisible;
             layout3.Visibility = ViewStates.Invisible;
         }
         if  (v.Id == Resource.Id.jiankang)
         {
             layout2.Visibility = ViewStates.Visible;
             layout1.Visibility = ViewStates.Invisible;
             layout3.Visibility = ViewStates.Invisible;
         }
 
         if  (v.Id == Resource.Id.button1)
         {
             layout3.Visibility = ViewStates.Visible;
             layout1.Visibility = ViewStates.Invisible;
             layout2.Visibility = ViewStates.Invisible;
         }
     }
 
     protected  override  void  OnCreate(Bundle bundle)
     {
         base .OnCreate(bundle);
         // Set our view from the "main" layout resource
 
 
         //  RelativeLayout
         //該布局是參照父控件或者是其他控件的位置進行布局。比如說我要把A控件
         //放到B控件的下面,並且A控件的右邊與B的左邊對齊。類似這樣的布局就可
         //以使用相對布局來完成比較容易
         SetContentView(Resource.Layout.line);
 
         /*
         // 相對於給定ID控件
             android:layout_above 將該控件的底部置於給定ID的控件之上;
             android:layout_below 將該控件的底部置於給定ID的控件之下;
             android:layout_toLeftOf    將該控件的右邊緣與給定ID的控件左邊緣對齊;
             android:layout_toRightOf  將該控件的左邊緣與給定ID的控件右邊緣對齊;
 
             android:layout_alignBaseline  將該控件的baseline與給定ID的baseline對齊;
             android:layout_alignTop        將該控件的頂部邊緣與給定ID的頂部邊緣對齊;
             android:layout_alignBottom   將該控件的底部邊緣與給定ID的底部邊緣對齊;
             android:layout_alignLeft        將該控件的左邊緣與給定ID的左邊緣對齊;
             android:layout_alignRight      將該控件的右邊緣與給定ID的右邊緣對齊;
             // 相對於父組件
             android:layout_alignParentTop      如果為true,將該控件的頂部與其父控件的頂部對齊;
             android:layout_alignParentBottom 如果為true,將該控件的底部與其父控件的底部對齊;
             android:layout_alignParentLeft      如果為true,將該控件的左部與其父控件的左部對齊;
             android:layout_alignParentRight    如果為true,將該控件的右部與其父控件的右部對齊;
             // 居中
             android:layout_centerHorizontal 如果為true,將該控件的置於水平居中;
             android:layout_centerVertical     如果為true,將該控件的置於垂直居中;
             android:layout_centerInParent   如果為true,將該控件的置於父控件的中央;
             // 指定移動像素
             android:layout_marginTop      上偏移的值;
             android:layout_marginBottom 下偏移的值;
             android:layout_marginLeft   左偏移的值;
             android:layout_marginRight   右偏移的值;
 
         */
 
         /*
         設置line簡單布局
         Introduce/Test007 布局說明.txt
         */
         //SetContentView(Resource.Layout.line);
         //layoutExample();
 
     }
     /// <summary>
     /// Line布局
     /// </summary>
     private  void  layoutExample()
     {
         Button btn = FindViewById<Button>(Resource.Id.home);
         btn.SetOnClickListener( this );
         Button btn1 = FindViewById<Button>(Resource.Id.jiankang);
         btn1.SetOnClickListener( this );
         Button bt1 = FindViewById<Button>(Resource.Id.button1);
         bt1.SetOnClickListener( this );
     }
}

 今天就到這里……


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM