最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android開(kāi)發(fā)必知 九種對(duì)話(huà)框的實(shí)現(xiàn)方法

 更新時(shí)間:2015年08月13日 10:31:21   投稿:mrr  
App中少不了與用戶(hù)交互的各種dialog,以此達(dá)到很好的用戶(hù)體驗(yàn),下面給大家介紹Android開(kāi)發(fā)必知 九種對(duì)話(huà)框的實(shí)現(xiàn)方法,有需要的朋友可以參考下

在開(kāi)發(fā)過(guò)程中,與用戶(hù)交互式免不了會(huì)用到對(duì)話(huà)框以實(shí)現(xiàn)更好的用戶(hù)體驗(yàn),所以掌握幾種對(duì)話(huà)框的實(shí)現(xiàn)方法還是非常有必要的。在看具體實(shí)例之前先對(duì)AlertDialog做一個(gè)簡(jiǎn)單介紹。AlertDialog是功能最豐富、實(shí)踐應(yīng)用最廣的對(duì)話(huà)框,它可以生成各種內(nèi)容的對(duì)話(huà)框。但實(shí)際上AlertDialog生成的對(duì)話(huà)框總體可分為以下4個(gè)區(qū)域:圖標(biāo)區(qū)、標(biāo)題區(qū)、內(nèi)容區(qū)、按鈕區(qū)。

這里總結(jié)了九種對(duì)話(huà)框的實(shí)現(xiàn)方法,有需要的朋友可以來(lái)學(xué)習(xí)下了

 

除了popupwindow實(shí)現(xiàn)稍微麻煩一點(diǎn),其他形似都相對(duì)簡(jiǎn)單,熟悉2便即可

直接上源碼

package com.naoh.stu; 
import java.util.ArrayList; 
import android.app.Activity; 
import android.app.AlertDialog; 
import android.app.ProgressDialog; 
import android.content.DialogInterface; 
import android.graphics.drawable.BitmapDrawable; 
import android.os.Bundle; 
import android.view.Gravity; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.ViewGroup.LayoutParams; 
import android.view.WindowManager; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.PopupWindow; 
import android.widget.Toast; 
public class DiaAllActivity extends Activity implements Runnable { 
  private Button btn_diaNormal;  
  private Button btn_diaMulti; 
  private Button btn_diaList; 
  private Button btn_diaSinChos; 
  private Button btn_diaMultiChos; 
  private Button btn_diaProcess; 
  private Button btn_diaReadProcess; 
  private Button btn_diaCustom; 
  private Button btn_popUpDia; 
  private PopupWindow window=null; 
  private Button cusPopupBtn1; 
  private View popupView; 
  @Override 
  public void onCreate(Bundle savedInstanceState) 
  { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.dialog); 
    getView(); 
    setListener(); 
  } 
  private void getView() 
  { 
    btn_diaNormal=(Button)findViewById(R.id.btn_diaNormal); 
    btn_diaMulti=(Button)findViewById(R.id.btn_diaMulti); 
    btn_diaList=(Button)findViewById(R.id.btn_diaList); 
    btn_diaSinChos=(Button)findViewById(R.id.btn_diaSigChos); 
    btn_diaMultiChos=(Button)findViewById(R.id.btn_diaMultiChos); 
    btn_diaProcess=(Button)findViewById(R.id.btn_diaProcess); 
    btn_diaReadProcess=(Button)findViewById(R.id.btn_diaReadProcess); 
    btn_diaCustom=(Button)findViewById(R.id.btn_diaCustom); 
    btn_popUpDia=(Button)findViewById(R.id.btn_popUpDia); 
  } 
  private void setListener() 
  { 
    btn_diaNormal.setOnClickListener(btnListener); 
    btn_diaMulti.setOnClickListener(btnListener); 
    btn_diaList.setOnClickListener(btnListener); 
    btn_diaSinChos.setOnClickListener(btnListener); 
    btn_diaMultiChos.setOnClickListener(btnListener); 
    btn_diaProcess.setOnClickListener(btnListener); 
    btn_diaReadProcess.setOnClickListener(btnListener); 
    btn_diaCustom.setOnClickListener(btnListener); 
    btn_popUpDia.setOnClickListener(btnListener); 
  } 
  private Button.OnClickListener btnListener= new Button.OnClickListener() 
  { 
    public void onClick(View v) 
    { 
      if(v instanceof Button) 
      { 
        int btnId=v.getId(); 
        switch(btnId) 
        { 
          case R.id.btn_diaNormal: 
            showNormalDia(); 
            break; 
          case R.id.btn_diaMulti: 
            showMultiDia(); 
            break; 
          case R.id.btn_diaList: 
            showListDia(); 
            break; 
          case R.id.btn_diaSigChos: 
            showSinChosDia(); 
            break; 
          case R.id.btn_diaMultiChos: 
            showMultiChosDia(); 
            break; 
          case R.id.btn_diaReadProcess: 
            showReadProcess(); 
            break; 
          case R.id.btn_diaProcess: 
            showProcessDia(); 
            break; 
          case R.id.btn_diaCustom: 
            showCustomDia(); 
            break; 
          case R.id.btn_popUpDia: 
            showCusPopUp(v); 
            break; 
          default: 
            break; 
        } 
      } 
    } 
  }; 
  /*普通的對(duì)話(huà)框*/ 
  private void showNormalDia() 
  { 
    //AlertDialog.Builder normalDialog=new AlertDialog.Builder(getApplicationContext()); 
    AlertDialog.Builder normalDia=new AlertDialog.Builder(DiaAllActivity.this); 
    normalDia.setIcon(R.drawable.ic_launcher); 
    normalDia.setTitle("普通的對(duì)話(huà)框"); 
    normalDia.setMessage("普通對(duì)話(huà)框的message內(nèi)容"); 
    normalDia.setPositiveButton("確定", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
        // TODO Auto-generated method stub 
        showClickMessage("確定"); 
      } 
    }); 
    normalDia.setNegativeButton("取消", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
        // TODO Auto-generated method stub 
        showClickMessage("取消"); 
      } 
    }); 
    normalDia.create().show(); 
  } 
  /*多按鈕對(duì)話(huà)框*/ 
  private void showMultiDia() 
  { 
    AlertDialog.Builder multiDia=new AlertDialog.Builder(DiaAllActivity.this); 
    multiDia.setTitle("多選項(xiàng)對(duì)話(huà)框"); 
    multiDia.setPositiveButton("按鈕一", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
        // TODO Auto-generated method stub 
        showClickMessage("按鈕一"); 
      } 
    }); 
    multiDia.setNeutralButton("按鈕二", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
        // TODO Auto-generated method stub 
        showClickMessage("按鈕二"); 
      } 
    }); 
    multiDia.setNegativeButton("按鈕三", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
        // TODO Auto-generated method stub 
        showClickMessage("按鈕三"); 
      } 
    }); 
    multiDia.create().show(); 
  } 
  /*列表對(duì)話(huà)框*/ 
  private void showListDia() 
  { 
    final String[] mList={"選項(xiàng)1","選項(xiàng)2","選項(xiàng)3","選項(xiàng)4","選項(xiàng)5","選項(xiàng)6","選項(xiàng)7"}; 
    AlertDialog.Builder listDia=new AlertDialog.Builder(DiaAllActivity.this); 
    listDia.setTitle("列表對(duì)話(huà)框"); 
    listDia.setItems(mList, new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
        // TODO Auto-generated method stub 
        /*下標(biāo)是從0開(kāi)始的*/ 
        showClickMessage(mList[which]); 
      } 
    }); 
    listDia.create().show(); 
  } 
  /*單項(xiàng)選擇對(duì)話(huà)框*/ 
  int yourChose=-1; 
  private void showSinChosDia() 
  { 
    final String[] mList={"選項(xiàng)1","選項(xiàng)2","選項(xiàng)3","選項(xiàng)4","選項(xiàng)5","選項(xiàng)6","選項(xiàng)7"}; 
    yourChose=-1; 
    AlertDialog.Builder sinChosDia=new AlertDialog.Builder(DiaAllActivity.this); 
    sinChosDia.setTitle("單項(xiàng)選擇對(duì)話(huà)框"); 
    sinChosDia.setSingleChoiceItems(mList, 0, new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
        // TODO Auto-generated method stub 
        yourChose=which; 
      } 
    }); 
    sinChosDia.setPositiveButton("確定", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
        // TODO Auto-generated method stub 
        if(yourChose!=-1) 
        { 
          showClickMessage(mList[yourChose]); 
        } 
      } 
    }); 
    sinChosDia.create().show(); 
  } 
  ArrayList<Integer> myChose= new ArrayList<Integer>(); 
  private void showMultiChosDia() 
  { 
    final String[] mList={"選項(xiàng)1","選項(xiàng)2","選項(xiàng)3","選項(xiàng)4","選項(xiàng)5","選項(xiàng)6","選項(xiàng)7"}; 
    final boolean mChoseSts[]={false,false,false,false,false,false,false}; 
    myChose.clear(); 
    AlertDialog.Builder multiChosDia=new AlertDialog.Builder(DiaAllActivity.this); 
    multiChosDia.setTitle("多項(xiàng)選擇對(duì)話(huà)框"); 
    multiChosDia.setMultiChoiceItems(mList, mChoseSts, new DialogInterface.OnMultiChoiceClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which, boolean isChecked) { 
        // TODO Auto-generated method stub 
        if(isChecked) 
        { 
          myChose.add(which); 
        } 
        else 
        { 
          myChose.remove(which); 
        } 
      } 
    }); 
    multiChosDia.setPositiveButton("確定", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
        // TODO Auto-generated method stub 
        int size=myChose.size(); 
        String str=""; 
        for(int i=0;i<size;i++) 
        { 
          str+=mList[myChose.get(i)]; 
        } 
        showClickMessage(str); 
      } 
    }); 
    multiChosDia.create().show(); 
  } 
  //進(jìn)度讀取框需要模擬讀取 
  ProgressDialog mReadProcessDia=null; 
  public final static int MAX_READPROCESS = 100; 
  private void showReadProcess() 
  { 
    mReadProcessDia=new ProgressDialog(DiaAllActivity.this); 
    mReadProcessDia.setProgress(0); 
    mReadProcessDia.setTitle("進(jìn)度條窗口"); 
    mReadProcessDia.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
    mReadProcessDia.setMax(MAX_READPROCESS); 
    mReadProcessDia.show(); 
    new Thread(this).start(); 
  } 
  //新開(kāi)啟一個(gè)線(xiàn)程,循環(huán)的累加,一直到100然后在停止 
  @Override 
  public void run() 
  { 
    int Progress= 0; 
    while(Progress < MAX_READPROCESS) 
    { 
      try { 
        Thread.sleep(100); 
        Progress++; 
        mReadProcessDia.incrementProgressBy(1); 
      } catch (InterruptedException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
      } 
    } 
    //讀取完了以后窗口自消失 
    mReadProcessDia.cancel(); 
  } 
  /*讀取中的對(duì)話(huà)框*/ 
  private void showProcessDia() 
  { 
    ProgressDialog processDia= new ProgressDialog(DiaAllActivity.this); 
    processDia.setTitle("進(jìn)度條框"); 
    processDia.setMessage("內(nèi)容讀取中..."); 
    processDia.setIndeterminate(true); 
    processDia.setCancelable(true); 
    processDia.show(); 
  } 
  /*自定義對(duì)話(huà)框*/ 
  private void showCustomDia() 
  { 
    AlertDialog.Builder customDia=new AlertDialog.Builder(DiaAllActivity.this); 
    final View viewDia=LayoutInflater.from(DiaAllActivity.this).inflate(R.layout.custom_dialog, null); 
    customDia.setTitle("自定義對(duì)話(huà)框"); 
    customDia.setView(viewDia); 
    customDia.setPositiveButton("確定", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
        // TODO Auto-generated method stub 
        EditText diaInput=(EditText) viewDia.findViewById(R.id.txt_cusDiaInput); 
        showClickMessage(diaInput.getText().toString()); 
      } 
    }); 
    customDia.create().show(); 
  } 
  /*popup window 來(lái)實(shí)現(xiàn)*/ 
  private void showCusPopUp(View parent) 
  { 
    if(window == null) 
    { 
      popupView=LayoutInflater.from(DiaAllActivity.this).inflate(R.layout.dia_cuspopup_dia, null); 
      cusPopupBtn1=(Button)popupView.findViewById(R.id.diaCusPopupSure); 
      window =new PopupWindow(popupView,LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT); 
    } 
    window.setAnimationStyle(R.style.PopupAnimation); 
    /*必須調(diào)用setBackgroundDrawable, 因?yàn)閜opupwindow在初始時(shí),會(huì)檢測(cè)background是否為null,如果是onTouch or onKey events就不會(huì)相應(yīng),所以必須設(shè)置background*/ 
    /*網(wǎng)上也有很多人說(shuō),彈出pop之后,不響應(yīng)鍵盤(pán)事件了,這個(gè)其實(shí)是焦點(diǎn)在pop里面的view去了。*/ 
    window.setFocusable(true); 
    window.setBackgroundDrawable(new BitmapDrawable());  
    window.update(); 
    window.showAtLocation(parent, Gravity.CENTER_VERTICAL, 0, 0); 
    cusPopupBtn1.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
        // TODO Auto-generated method stub 
        showClickMessage("popup window的確定"); 
      } 
    }); 
  } 
  /*顯示點(diǎn)擊的內(nèi)容*/ 
  private void showClickMessage(String message) 
  { 
    Toast.makeText(DiaAllActivity.this, "你選擇的是: "+message, Toast.LENGTH_SHORT).show(); 
  } 
} 

布局,就是一堆的button

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  android:layout_width="fill_parent" 
  android:layout_height="fill_parent" 
  android:orientation="vertical" > 
  <TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="各種Dialog合集" /> 
  <Button  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="普通Dialog" 
    android:id="@+id/btn_diaNormal"/> 
  <Button  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="多按鈕Dialog" 
    android:id="@+id/btn_diaMulti"/> 
  <Button  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="列表Dialog" 
    android:id="@+id/btn_diaList"/> 
  <Button  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="單項(xiàng)選擇Dialog" 
    android:id="@+id/btn_diaSigChos"/> 
  <Button  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="多項(xiàng)選擇Dialog" 
    android:id="@+id/btn_diaMultiChos"/> 
  <Button  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="進(jìn)度條Dialog" 
    android:id="@+id/btn_diaReadProcess"/> 
  <Button  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="讀取中Dialog" 
    android:id="@+id/btn_diaProcess"/> 
  <Button  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="自定義Dialog" 
    android:id="@+id/btn_diaCustom"/> 
  <Button  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="PopUpWindow實(shí)現(xiàn)的dialog" 
    android:id="@+id/btn_popUpDia"/> 
</LinearLayout> 

以上是Android開(kāi)發(fā)必知 九種對(duì)話(huà)框的實(shí)現(xiàn)方法,代碼主要用AlertDialog生成的對(duì)話(huà)框,它的功能豐富、使用最廣。

相關(guān)文章

  • Android編程常用技巧實(shí)例總結(jié)

    Android編程常用技巧實(shí)例總結(jié)

    這篇文章主要介紹了Android編程常用技巧實(shí)例總結(jié),包括Android對(duì)話(huà)框、分辨率、資源、字體等操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-11-11
  • 詳解ViewBinding用法

    詳解ViewBinding用法

    這篇文章主要介紹了ViewBinding用法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-03-03
  • Android Studio 運(yùn)行時(shí)出現(xiàn)的警告信息解決辦法

    Android Studio 運(yùn)行時(shí)出現(xiàn)的警告信息解決辦法

    這篇文章主要介紹了Android Studio 運(yùn)行時(shí)出現(xiàn)的警告信息解決辦法的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • Android編程之防止反編譯的實(shí)現(xiàn)方法

    Android編程之防止反編譯的實(shí)現(xiàn)方法

    這篇文章主要介紹了Android編程之防止反編譯的實(shí)現(xiàn)方法,涉及Android針對(duì)運(yùn)行環(huán)境、簽名及程序相關(guān)信息的獲取與判定技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-11-11
  • RxJava構(gòu)建流基本原理示例解析

    RxJava構(gòu)建流基本原理示例解析

    這篇文章主要為大家介紹了RxJava構(gòu)建流基本原理示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-12-12
  • Android 虛擬按鍵適配動(dòng)態(tài)調(diào)整布局的方法

    Android 虛擬按鍵適配動(dòng)態(tài)調(diào)整布局的方法

    今天小編就為大家分享一篇Android 虛擬按鍵適配動(dòng)態(tài)調(diào)整布局的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-07-07
  • Android?組件化神器之Arouter依賴(lài)配置使用

    Android?組件化神器之Arouter依賴(lài)配置使用

    這篇文章主要為大家介紹了Android?組件化神器之Arouter依賴(lài)配置使用,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-06-06
  • Android雷達(dá)掃描動(dòng)態(tài)界面制作

    Android雷達(dá)掃描動(dòng)態(tài)界面制作

    這篇文章主要為大家詳細(xì)介紹了Android雷達(dá)掃描動(dòng)態(tài)界面制作資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-10-10
  • Android編程實(shí)現(xiàn)Toast自定義布局簡(jiǎn)單示例

    Android編程實(shí)現(xiàn)Toast自定義布局簡(jiǎn)單示例

    這篇文章主要介紹了Android編程實(shí)現(xiàn)Toast自定義布局的方法,結(jié)合簡(jiǎn)單實(shí)例形式分析了Toast自定義布局的實(shí)現(xiàn)步驟與相關(guān)操作技巧,需要的朋友可以參考下
    2017-02-02
  • Android實(shí)現(xiàn)雙曲線(xiàn)折線(xiàn)圖

    Android實(shí)現(xiàn)雙曲線(xiàn)折線(xiàn)圖

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)雙曲線(xiàn)折線(xiàn)圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-09-09

最新評(píng)論

阳西县| 宜川县| 女性| 肃南| 临洮县| 太谷县| 天全县| 吉木乃县| 永平县| 专栏| 榆社县| 徐汇区| 修水县| 永城市| 宽甸| 尼勒克县| 石柱| 卓尼县| 通海县| 山丹县| 辉南县| 新民市| 任丘市| 泾阳县| 饶阳县| 八宿县| 万年县| 祁门县| 龙南县| 顺平县| 汽车| 巴南区| 江口县| 津南区| 平远县| 东源县| 呼伦贝尔市| 高要市| 巧家县| 扎赉特旗| 绥德县|