Android中的AlertDialog使用樣本四(多項選擇確定對話方塊)

來源:互聯網
上載者:User

標籤:交流   alert   idt   schema   block   over   樣本   context   pre   

在Android開發中,我們經常會需要在Android介面上彈出一些對話方塊,比如詢問使用者或者讓使用者選擇。這些功能我們叫它Android Dialog對話方塊,AlertDialog實現方法為建造者模式。下面我們簡單類比一個皇帝選妃的選擇確定對話方塊(多選),如:

 

Layout(僅布置一個按鈕)介面代碼:


 1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3     xmlns:tools="http://schemas.android.com/tools" 4     android:id="@+id/activity_main" 5     android:layout_width="match_parent" 6     android:layout_height="match_parent" 7     tools:context="com.example.administrator.multichoice.MainActivity"> 8     <Button 9         android:text="多項選擇"10         android:layout_width="match_parent"11         android:layout_height="wrap_content"12         android:id="@+id/button"13         android:onClick="show" />14 </RelativeLayout>

Java實現代碼:

 1 import android.content.DialogInterface; 2 import android.support.v7.app.AlertDialog; 3 import android.support.v7.app.AppCompatActivity; 4 import android.os.Bundle; 5 import android.view.View; 6 import android.widget.Toast; 7 import java.util.ArrayList; 8 import java.util.List; 9 10 public class MainActivity extends AppCompatActivity {11     //執行個體化儲存使用者最終選擇的集合12     List<String> choose = new ArrayList<>();13     @Override14     protected void onCreate(Bundle savedInstanceState) {15         super.onCreate(savedInstanceState);16         setContentView(R.layout.activity_main);17     }18     public void show(View v){19         AlertDialog.Builder builder = new AlertDialog.Builder(this);20         builder.setTitle("皇上選妃了");21         final String[] names={"西施","王昭君","貂蟬","楊玉環"};22         choose.clear();//每次的點擊事件前需要清空集合內的元素23         //設定Dialog為多選框,且無預設選項(null)24         builder.setMultiChoiceItems(names, null, new DialogInterface.OnMultiChoiceClickListener() {25             @Override26             //設定點擊事件:如果選中則添加進choose,如果取消或者未選擇則移出choose27             public void onClick(DialogInterface dialog, int which, boolean isChecked) {28                 if(isChecked){29                     choose.add(names[which]);30                 }else{31                     choose.remove(names[which]);32                 }33             }34         });35         //設定正面按鈕以及點擊事件(土司顯示choose內容)36         builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {37             @Override38             public void onClick(DialogInterface dialog, int which) {39                 Toast.makeText(MainActivity.this,choose.toString()+"是您的妃子了",Toast.LENGTH_SHORT).show();40             }41         });42         builder.show();//顯示Dialog對話方塊43     }44 }

 大家可以多多交流,完善該對話方塊,祝大家擼的開心


Android中的AlertDialog使用樣本四(多項選擇確定對話方塊)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.