欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android UI設(shè)計之AlertDialog彈窗控件

 更新時間:2016年08月18日 14:31:38   作者:qq_27630169  
這篇文章主要為大家詳細介紹了Android UI設(shè)計之AlertDialog彈窗控件的使用方法,感興趣的小伙伴們可以參考一下

有關(guān)android的彈窗界面相信大家見過不少了,手機上很多應(yīng)用軟件都涉及到彈窗控件,比如典型的每次刪除一個圖片或者卸載一個等都會彈出一個窗口詢問是否刪除/卸載等,還有我們系統(tǒng)的設(shè)置時間/日期等,都用到了這樣的控件,下面我將通過代碼來總結(jié)下常用的幾個彈窗控件

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical"
 tools:context="com.company.alertdialog.MainActivity">

 <Button
  android:id="@+id/btn1"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:onClick="onClick"
  android:text="列表彈窗" />

 <Button
  android:id="@+id/btn2"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:onClick="onClick"
  android:text="單選彈窗" />

 <Button
  android:id="@+id/btn3"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:onClick="onClick"
  android:text="多選彈窗" />

 <Button
  android:id="@+id/btn4"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:onClick="onClick"
  android:text="日期彈窗" />

 <Button
  android:id="@+id/btn5"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:onClick="onClick"
  android:text="時間彈窗" />

 <Button
  android:id="@+id/btn6"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:onClick="onClick"
  android:text="進度條彈窗" />
</LinearLayout>

strings.xml

<resources>
 <string name="app_name">AlertDialog</string>
 <string-array name="list">
  <item>列表一</item>
  <item>列表二</item>
  <item>列表三</item>
  <item>列表四</item>
  <item>列表五</item>
  <item>列表六</item>
 </string-array>
</resources>

MainActivity.java

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

 //表示列表彈窗
 private Button mBtn1;

 //表示單選彈窗
 private Button mBtn2;

 //表示多選彈窗
 private Button mBtn3;

 //表示日期彈窗
 private Button mBtn4;

 //表示時間彈窗
 private Button mBtn5;

 //表示進度條彈窗
 private Button mBtn6;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  init();
  event();
 }

 /**
  * 設(shè)置監(jiān)聽事件
  */
 private void event() {
  mBtn1.setOnClickListener(this);
  mBtn2.setOnClickListener(this);
  mBtn3.setOnClickListener(this);
  mBtn4.setOnClickListener(this);
  mBtn5.setOnClickListener(this);
  mBtn6.setOnClickListener(this);
 }

 /**
  * 初始化控件
  */
 private void init() {
  mBtn1 = (Button) findViewById(R.id.btn1);
  mBtn2 = (Button) findViewById(R.id.btn2);
  mBtn3 = (Button) findViewById(R.id.btn3);
  mBtn4 = (Button) findViewById(R.id.btn4);
  mBtn5 = (Button) findViewById(R.id.btn5);
  mBtn6 = (Button) findViewById(R.id.btn6);
 }

 @Override
 public void onClick(View v) {
  switch (v.getId()) {
   case R.id.btn1:
    createListDialog();
    break;
   case R.id.btn2:
    createSingleDialog();
    break;
   case R.id.btn3:
    createMutilDialog();
    break;
   case R.id.btn4:
    createDateDialog();
    break;
   case R.id.btn5:
    createTimeDialog();
    break;
   case R.id.btn6:
    createProgressBarDialog();
    break;


  }
 }

 /**
  * 創(chuàng)建一個進度條彈窗
  */
 private void createProgressBarDialog() {
  //創(chuàng)建進度條彈窗對象
  ProgressDialog progressDialog = new ProgressDialog(this);
  //設(shè)置標題
  progressDialog.setTitle("進度條彈窗");
  //設(shè)置標題圖標
  progressDialog.setIcon(R.mipmap.ic_launcher);
  //設(shè)置文本
  progressDialog.setMessage("正在加載...");
  //設(shè)置最大進度
  progressDialog.setMax(100);
  //設(shè)置進度條的類型
  progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
  //顯示進度條彈窗
  progressDialog.show();
  //如果設(shè)置這條語句的話,那么無論你點擊屏幕外的任何地方或者按返回鍵都取消不了這個彈窗,
  //除非在完成進度后,設(shè)置取消事件。一般情況這種設(shè)置方式對界面很不友好
  //不過有時候軟件有重大bug,用戶不得不更新該軟件,如果不更新,就只能
  //強制退出程序了
//  progressDialog.setCancelable(false);//不允許被某些方式取消,比如按對話框之外的區(qū)域或者是返回鍵
  progressDialog.setProgress(50);
 }

 /**
  * 創(chuàng)建一個日期彈窗
  */
 private void createDateDialog() {
  new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
   /**
    *
    * @param view 當前日期選擇的 view
    * @param year 當前選擇的年
    * @param monthOfYear 當前選擇的月,從0開始算
    * @param dayOfMonth,當前選擇的日,從1開始算
    */
   @Override
   public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
    Toast.makeText(MainActivity.this, "view = " + view + "年:" + year + "月:" + monthOfYear + "日" + dayOfMonth, Toast.LENGTH_SHORT).show();
   }
  }, 2016, 7, 15)//這里注意一下的是月份系統(tǒng)表示的是從0開始的,0表示1月,1表示2月.....11表示12月
  .show();
 }


 /**
  * 創(chuàng)建一個時間彈窗
  */
 private void createTimeDialog() {
  new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
   /**
    *
    * @param view 當前時間選擇的view
    * @param hourOfDay 小時
    * @param minute 分鐘
    */
   @Override
   public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
    Toast.makeText(MainActivity.this, "時間彈窗 view = " + view + "hourOfDay = " + hourOfDay + "minute = " + minute, Toast.LENGTH_SHORT).show();
   }
  }, 11, 22, true)
  .show();
 }


 /**
  * 創(chuàng)建一個多選彈窗
  */
 private void createMutilDialog() {
  new AlertDialog.Builder(this)
    .setTitle("多選彈框")
    .setIcon(R.mipmap.ic_launcher)
    //第二個參數(shù) boolean數(shù)組, 如果寫 null 代表默認全部是非選中, 如果想指定某幾個選中, 
    //需要創(chuàng)建對應(yīng)長度的數(shù)據(jù),按照位置的順序,將指定位置設(shè)置為 true 即可, 數(shù)組長度不能小
    //于數(shù)據(jù)源的長度,否則會越界,但是可以大于數(shù)據(jù)源的長度
    .setMultiChoiceItems(R.array.list, new boolean[]{true, false, false, true, false, false, false, false, false, false, false, false, false}, new DialogInterface.OnMultiChoiceClickListener() {
     /**
      *
      * @param dialog 當前點擊的對話框
      * @param which 當前點擊的條目
      * @param isChecked 被點擊條目的選中狀態(tài)
      */
     @Override
     public void onClick(DialogInterface dialog, int which, boolean isChecked) {
      Toast.makeText(MainActivity.this, "當前點擊的是" + which + " 是否選中" + isChecked, Toast.LENGTH_SHORT).show();
     }
    })
    //設(shè)置取消按鈕,并且設(shè)置監(jiān)聽事件
    .setNegativeButton("cancel", new DialogInterface.OnClickListener() {
     @Override
     public void onClick(DialogInterface dialog, int which) {
      dialog.dismiss();
     }
    })
    //確認按鈕,默認點擊會直接取消該窗口
    .setPositiveButton("sure", new DialogInterface.OnClickListener() {
     @Override
     public void onClick(DialogInterface dialog, int which) {

     }
    })
    .setCancelable(false)
    .show();
 }



 /**
  * 創(chuàng)建一個單選彈窗
  */
 private void createSingleDialog() {
  new AlertDialog.Builder(this)
    .setTitle("單選彈窗")
    .setIcon(R.mipmap.ic_launcher)
    //構(gòu)造參數(shù), 1 數(shù)據(jù)源,2 默認被選中的索引,3 條目的點擊事件
    .setSingleChoiceItems(R.array.list, 1, new DialogInterface.OnClickListener() {
     /**
      *
      * @param dialog 當前的對話框
      * @param which 當前點擊的是列表的第幾個 item
      */
     @Override
     public void onClick(DialogInterface dialog, int which) {
      Toast.makeText(MainActivity.this, "單選彈窗 dialog = " + dialog + "which = " + which, Toast.LENGTH_SHORT).show();
     }
    })
    .setNegativeButton("cancel", new DialogInterface.OnClickListener() {
     @Override
     public void onClick(DialogInterface dialog, int which) {
      dialog.dismiss();
     }
    })
    .setPositiveButton("sure", new DialogInterface.OnClickListener() {
     @Override
     public void onClick(DialogInterface dialog, int which) {

     }
    })
    .setCancelable(false)//不允許被某些方式取消,比如按對話框之外的區(qū)域或者是返回鍵
    .show();
 }


 /**
  * 創(chuàng)建一個列表彈窗
  */
 private void createListDialog() {
  AlertDialog.Builder builder = new AlertDialog.Builder(this);
  builder.setTitle("列表彈窗");
  builder.setItems(R.array.list, new DialogInterface.OnClickListener() {
   /**
    *
    * @param dialog 當前的對話框
    * @param which 當前點擊的是列表的第幾個 item
    */
   @Override
   public void onClick(DialogInterface dialog, int which) {
    Toast.makeText(MainActivity.this, "列表 dialog = " + dialog + "which = " + which, Toast.LENGTH_SHORT).show();
   }
  });
  builder.setCancelable(false);//不允許被某些方式取消,比如按對話框之外的區(qū)域或者是返回鍵
  builder.show();
 }
}

列表彈窗:

單選彈窗:

多選彈窗:

日期彈窗:

時間彈窗:

進度條彈窗:

差不多常見的幾種都在這里了,至于還有一個PopupWindow這里暫時不作介紹。

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • RecyclerView實現(xiàn)流式標簽單選多選功能

    RecyclerView實現(xiàn)流式標簽單選多選功能

    RecyclerView是Android一個更強大的控件,其不僅可以實現(xiàn)和ListView同樣的效果,還有優(yōu)化了ListView中的各種不足。這篇文章主要介紹了RecyclerView實現(xiàn)的流式標簽單選多選功能,需要的朋友可以參考下
    2019-11-11
  • 你值得擁有的Android Studio開發(fā)小技巧

    你值得擁有的Android Studio開發(fā)小技巧

    這篇文章主要為大家分享了值得擁有的Android Studio開發(fā)小技巧,介紹幾個比較好用的技巧和快捷鍵,提升我們的編碼效率,感興趣的小伙伴們可以參考一下
    2016-06-06
  • Android?AMS啟動App進程原理分析

    Android?AMS啟動App進程原理分析

    這篇文章主要介紹了Android?AMS啟動App進程原理,系統(tǒng)fork函數(shù)是如何創(chuàng)建進程,文中有詳細的代碼示例,對大家的學習或工作有一定的幫助,需要的朋友可以參考下
    2023-05-05
  • Android?Compose之Animatable動畫停止使用詳解

    Android?Compose之Animatable動畫停止使用詳解

    這篇文章主要為大家介紹了Android?Compose之Animatable動畫停止使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-03-03
  • Flutter中ListView 的使用示例

    Flutter中ListView 的使用示例

    這篇文章主要介紹了Flutter中ListView 的使用示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-07-07
  • 實現(xiàn)一個Android鎖屏App功能的難點總結(jié)

    實現(xiàn)一個Android鎖屏App功能的難點總結(jié)

    這篇文章主要介紹了實現(xiàn)一個Android鎖屏App功能的難點總結(jié),可以有效的解決鎖屏開發(fā)的問題,有需要的可以參考一下。
    2016-11-11
  • Android實現(xiàn)截屏功能

    Android實現(xiàn)截屏功能

    這篇文章主要為大家詳細介紹了Android實現(xiàn)截屏功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-07-07
  • 解決android studio android monitor打不開的問題

    解決android studio android monitor打不開的問題

    下面小編就為大家分享一篇解決android studio android monitor打不開的問題,具有很的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-01-01
  • 詳解Android中Application設(shè)置全局變量以及傳值

    詳解Android中Application設(shè)置全局變量以及傳值

    這篇文章主要介紹了詳解Android中Application設(shè)置全局變量以及傳值的相關(guān)資料,希望通過本文大家能夠理解掌握這部分內(nèi)容,需要的朋友可以參考下
    2017-09-09
  • Android常用設(shè)計模式之原型模式詳解

    Android常用設(shè)計模式之原型模式詳解

    這篇文章主要為大家介紹了Android常用設(shè)計模式之原型模式詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-10-10

最新評論