亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

android幾種不同對話框的實(shí)現(xiàn)方式

 更新時(shí)間:2015年08月13日 11:00:59   作者:codingblock  
這篇文章介紹了android幾種不同對話框的實(shí)現(xiàn),主要包括:1、顯示提示消息的對話框.2、簡單列表項(xiàng)對話框。3、單選列表項(xiàng)對話框。4、多選列表對話框。5、自定義列表項(xiàng)對話框。6、自定義View的對話框,需要的朋友可以參考下

app中肯定是少不了與用戶交互的各種dialog,下面給大家介紹幾種提示框的提示。

一般創(chuàng)建一個(gè)對話框需要經(jīng)過以下幾步:

  1、創(chuàng)建AlertDialog.Builder對象。

  2、調(diào)用AlertDialog.Builder的setTitle()或者setCustomTitle()方法設(shè)置標(biāo)題。

  3、調(diào)用AlertDialog.Builder的setIcon()方法設(shè)置標(biāo)題logo。

  4、調(diào)用AlertDialog.Builder的相關(guān)方法設(shè)置對話框內(nèi)容。

  5、調(diào)用AlertDialog.Builder的setPositiveButton()、setNegativeButton()或setNeutralButton()方法添加多個(gè)按鈕。

  6、調(diào)用AlertDialog.Builder的create()方法創(chuàng)建AlertDialog對象,再調(diào)用AlertDialog對象的show()方法將該對話框顯示出來。

其中,第4步設(shè)置對話框的內(nèi)容,這里有6種方法來指定:

  ·setMessage():設(shè)置對話框內(nèi)容為簡單文本內(nèi)容。

  ·setItems():設(shè)置對話框內(nèi)容為簡單列表項(xiàng)。

  ·setSingleChoiceItems():設(shè)置對話框內(nèi)容為單選列表項(xiàng)。

  ·setMultiChoiceItems():設(shè)置對話框內(nèi)容為多選列表項(xiàng)。

  ·setAdapter():設(shè)置對話框內(nèi)容為自定義列表項(xiàng)。

  ·setView():設(shè)置對話框內(nèi)容為自定義View。

下面通過幾個(gè)實(shí)例來介紹一下AlertDialog的用法。

1、顯示提示消息的對話框。

  

/**
  * 顯示提示消息的對話框
  * @author codingblock --
  * @param context  上下文
  * @param title  對話框標(biāo)題
  * @param message  對話框提示內(nèi)容
  * @return
  */
  public AlertDialog.Builder simpleDialog(final Context context, String title, String message){
   AlertDialog.Builder builder = new AlertDialog.Builder(context)
   .setTitle(title)
   .setIcon(R.drawable.ic_launcher)
   .setMessage(message)
   .setPositiveButton("完成", null)
   .setNegativeButton("取消", null);
   return builder;
  }

   上面的代碼是將一個(gè)簡單提示對話框封裝成了一個(gè)方法,調(diào)用時(shí)可以省去重復(fù)代碼,直接傳遞title,message等參數(shù)即可,其中該對話框用設(shè)置了icon,title等屬性,還調(diào)用了setPositiveButton()和setNegativeButton()方法添加按鈕,因?yàn)樵摲椒ǎ╯impleDialog())在這里僅提供調(diào)用,所以沒有實(shí)現(xiàn)按鈕的具體功能,可在實(shí)際調(diào)用中重寫這兩個(gè)方法從而實(shí)現(xiàn)具體功能。

   調(diào)用方式如下,其他幾種方式的對話框與此方法調(diào)用方式基本一致,以下就不再一一給出。

public void onClickSimple(View v){
   builder = new Dialog().simpleDialog(this, "簡單對話框", "對話框內(nèi)容");
   builder.setPositiveButton("確定", new OnClickListener() {
    @Override
    public void onClick(DialogInterface arg, int arg) {
     //確定
    }
   })
   .setNegativeButton("取消", new OnClickListener() {
    @Override
    public void onClick(DialogInterface arg, int arg) {
     //取消
    }
   });
   builder.create().show();
  }

  除此之外,AlertDialog.Builder還提供了setNeutralButton()方法來添加一個(gè)裝飾性的按鈕。因此Android的對話一共可以生成三個(gè)按鈕的對話框。

2、簡單列表項(xiàng)對話框

   

/**
  * 簡單列表項(xiàng)對話框
  * @author codingblock --
  * @param context  上下文
  * @param title  對話框標(biāo)題
  * @param items  對話框列表項(xiàng)CharSequence類型數(shù)組,也可根據(jù)需要改成其他類型
  * @return
  */
  public AlertDialog.Builder simpleListDialog(final Context context, String title, final CharSequence[] items){
   AlertDialog.Builder builder = new AlertDialog.Builder(context)
   .setTitle(title)
   .setIcon(R.drawable.ic_launcher)
   .setItems(items, new OnClickListener() {
    
    @Override
    public void onClick(DialogInterface dialog, int which) {
     Toast.makeText(context, "您選中了:"+ items[which], Toast.LENGTH_SHORT).show();
    }
   });
   return builder;
  }

  上面的代碼通過調(diào)用setItems()方法為對話框設(shè)置了多個(gè)列表項(xiàng),其中setItems的第一個(gè)參數(shù)可以是Charsequence和int類型。

3、單選列表項(xiàng)對話框

  

/**
  * 單選列表項(xiàng)對話框
  * @author codingblock --
  * @param context  上下文
  * @param title  對話框標(biāo)題
  * @param items  對話框列表項(xiàng) CharSequence類型數(shù)組
  * @return
  */
  public AlertDialog.Builder simpleChoiceDialog(final Context context, String title, final CharSequence[] items){
   AlertDialog.Builder builder = new AlertDialog.Builder(context)
   .setTitle(title)
   .setIcon(R.drawable.ic_launcher)
   //第二個(gè)參數(shù)為默認(rèn)選中項(xiàng), :代表默認(rèn)選中第一項(xiàng)
   .setSingleChoiceItems(items, , new OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
     Toast.makeText(context, "您選中了:"+ items[which], Toast.LENGTH_SHORT).show();
    }
   });
   return builder;
  }

  以上代碼通過調(diào)用setSingleChoiceItems()方法創(chuàng)建了帶單選列表的對話框。調(diào)用setSingleChoiceItems()方法時(shí)既可傳入數(shù)組作為參數(shù),也可傳入Cursor(相當(dāng)于數(shù)據(jù)庫查詢結(jié)果集)作為參數(shù),也可傳入ListAdapter作為參數(shù)。另外,如果傳入ListAdapter作為參數(shù),則由ListAdapter來提供多個(gè)列表項(xiàng)組件。

4、多選列表對話框

  

    /**
  * 多選列表項(xiàng)對話框
  * @author codingblock --
  * @param context  上下文
  * @param title   對話框標(biāo)題
  * @param items  對話框列表項(xiàng) CharSequence類型數(shù)組
  * @param checked  對話框初始選定狀態(tài) boolean類型數(shù)組
  * @return
  */
  public AlertDialog.Builder multiChoiceDialog(final Context context, String title, final CharSequence[] items, final boolean[] checked){
   AlertDialog.Builder builder = new AlertDialog.Builder(context)
   .setTitle(title)
   .setIcon(R.drawable.ic_launcher)
   //第二個(gè)參數(shù)為默認(rèn)選中項(xiàng),是一個(gè)boolean型的數(shù)組
   .setMultiChoiceItems(items, checked, null)
   .setPositiveButton("完成", null)
   .setNegativeButton("取消", null);
   return builder;
  }

  以上代碼通過調(diào)用setMultiChoiceItems()方法創(chuàng)建了一個(gè)多選列表的對話框。在調(diào)用setMultiChoiceItems()時(shí)既可傳入數(shù)組作為參數(shù),也可傳入Cursor作為參數(shù)。需要注意的時(shí)在調(diào)用setMultiChoiceItems()方法添加多選列表時(shí),還需要傳入一個(gè)boolean[]參數(shù),該參數(shù)有兩個(gè)作用:①設(shè)置初始化時(shí)選中哪些列表項(xiàng)。②該boolean[]類型的參數(shù)還可用于動(dòng)態(tài)的獲取多選列表項(xiàng)的選中狀態(tài)。

5、自定義列表項(xiàng)對話框

  
   

 /**
  * 自定義列表項(xiàng)對話框
  * @author codingblock --
  * @param context  上下文
  * @param title  對話框標(biāo)題
  * @param items  對話框列表項(xiàng) String類型數(shù)組,也可更具需要改成其他類型
  * @return
  */
  public AlertDialog.Builder customListDialog(final Context context, String title, String[] items){
   AlertDialog.Builder builder = new AlertDialog.Builder(context)
   .setTitle(title)
   .setIcon(R.drawable.ic_launcher)
   .setAdapter(new ArrayAdapter<String>(context, R.layout.array_item, R.id.tv_item, items), null)
   .setPositiveButton("完成", null)
   .setNegativeButton("取消", null);
   return builder;
  }

  以上代碼通過setAdapter()設(shè)置了對話框的內(nèi)容,該方法需要傳入一個(gè)Adapter參數(shù),這樣的話,就可以通過Adapter實(shí)現(xiàn)多個(gè)組件的繪制。其中setAdapter方法中調(diào)用的布局文件array_item.xml代碼如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/container"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:gravity="center"
  android:padding="dp"
  android:orientation="horizontal">
  <ImageView
   android:id="@+id/iv_img"
   android:layout_width="dp"
   android:layout_height="dp"
   android:src="@drawable/ic_launcher" />
  <TextView
   android:id="@+id/tv_item"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:layout_margin="dp"
   android:gravity="center"
   android:text="列表項(xiàng)" />
 </LinearLayout>

  其實(shí),不僅setAdapter()方法可以接受Adapter作為參數(shù),setSingleChoice()方法也可以接受Adapter參數(shù),也就是說,使用setSingleChoice()方法也可以實(shí)現(xiàn)自定義列表項(xiàng)對話框。

6、自定義View的對話框

  

/**
  * 自定義View的對話框
  * @author codingblock --
  * @param context  上下文
  * @param title  對話框標(biāo)題
  */
  public AlertDialog.Builder customeViewDialog(final Context context, String title){
   LinearLayout loginDialog = (LinearLayout)LayoutInflater.from(context).inflate(R.layout.login_dialog, null);
   AlertDialog.Builder builder = new AlertDialog.Builder(context)
   .setTitle(title)
   .setIcon(R.drawable.ic_launcher)
   .setView(loginDialog)
   .setPositiveButton("完成", null)
   .setNegativeButton("取消", null);
   return builder;
  }

      以上代碼通過setView()方法調(diào)用自定義的布局文件顯示界面。代碼中首先顯示裝載了login_dialog.xml文件,并返回該文件對應(yīng)的View,接下來程序調(diào)用了setView()方法來顯示View。

  其中的login_dialog.xml文件代碼如下:

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/container"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:padding="dp"
  android:orientation="vertical">
  <LinearLayout 
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:orientation="horizontal">
   <TextView 
    android:id="@+id/tv_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="用戶名:"/>
   <EditText 
    android:id="@+id/et_name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:focusable="true"
    android:hint="input name" />
  </LinearLayout>
  <LinearLayout 
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:orientation="horizontal">
   <TextView 
    android:id="@+id/tv_pwd"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="密碼:"/>
   <EditText 
    android:id="@+id/et_pwd"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="input password" />
  </LinearLayout>
 </LinearLayout>

以上介紹了六種不同對話框的實(shí)現(xiàn)方式,希望對大家有所幫助。

相關(guān)文章

最新評論