Android中的AlertDialog使用樣本三(單向選擇確定對話方塊),androidalertdialog

來源:互聯網
上載者:User

Android中的AlertDialog使用樣本三(單向選擇確定對話方塊),androidalertdialog

在Android開發中,我們經常會需要在Android介面上彈出一些對話方塊,比如詢問使用者或者讓使用者選擇。這些功能我們叫它Android Dialog對話方塊,AlertDialog實現方法為建造者模式。下面我們簡單類比一個挑媳婦的選擇確定對話方塊(單選)對話方塊,不同於樣本二之處在於本次只要不確定就可以後悔哦,如:

 

Layout介面代碼:

 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3     android:orientation="vertical" android:layout_width="match_parent" 4     android:layout_height="match_parent"> 5     <Button 6         android:text="單向選擇" 7         android:layout_width="match_parent" 8         android:layout_height="wrap_content" 9         android:onClick="show3"10         android:id="@+id/button3" />11 </LinearLayout>

 

Java功能實現代碼:

 1 public class AlertDialogDemo extends AppCompatActivity { 2     int index=0; 3     @Override 4     protected void onCreate(@Nullable Bundle savedInstanceState) { 5         super.onCreate(savedInstanceState); 6         setContentView(R.layout.alertdialog); 7     } 8     public void show3(View v){ 9         AlertDialog.Builder builder = new AlertDialog.Builder(this);10         builder.setTitle("挑媳婦兒");11         final String[] beautifulGirls = {"芙蓉姐姐","鳳姐","範爺","我自己"};12         builder.setSingleChoiceItems(beautifulGirls, 0, new DialogInterface.OnClickListener() {13             @Override14             public void onClick(DialogInterface dialog, int which) {15                 index = which;16             }17         });18         builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {19             @Override20             public void onClick(DialogInterface dialog, int which) {21                 Toast.makeText(AlertDialogDemo.this,beautifulGirls[index]+"是你的媳婦了",Toast.LENGTH_SHORT).show();22             }23         });24         builder.show();25     }26 }

最佳化空間很大,大家都可以玩玩。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.