Android 多個選項的彈出框的簡單實現

在佈局頁面添加一個fab按鈕(fab_user_Add),可以簡單的Button按鈕就可以

  <cc.trity.floatingactionbutton.FloatingActionButton
            android:id="@+id/fab_user_add"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_weight="1"
            android:layout_gravity="right"
            android:layout_marginRight="2dp"
            android:src="@drawable/ic_add"
            android:backgroundTint="#4b98fb"
            app:rippleColor="#9CC4F7"
            app:borderWidth="0dp"
            android:clickable="true"
            android:layout_marginBottom="20dp" />

在Activity的調用方法

 FloatingActionButton fabUserAdd = findViewById(R.id.fab_user_add);
        fabUserAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog dialog1 = new AlertDialog.Builder(CollectActivity.this)
                        .setTitle("選擇興趣愛好")
                        //正極按鈕
                        .setPositiveButton("讀書", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Log.i("興趣愛好","點擊【游泳】");
                            }
                        })
                        //負極按鈕
                        .setNegativeButton("游泳", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Log.i("興趣愛好","點擊【游泳】");
                            }
                        })
                        //空檔按鈕
                        .setNeutralButton("運動", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Log.i("興趣愛好","點擊【運動】");
                            }
                        })
                        .create();
                dialog1.show();      
            }
        });

效果圖

 如果需要彈窗選擇按鈕對齊,在dialog1.show()後面添加如下代碼

                //按鈕位置調整
                Button mNegativeButton = dialog1.getButton(AlertDialog.BUTTON_NEGATIVE);
                Button mPositiveButton = dialog1.getButton(AlertDialog.BUTTON_POSITIVE);
                Button mNeutralButton = dialog1.getButton(AlertDialog.BUTTON_NEUTRAL);

                LinearLayout.LayoutParams positiveButtonLL = (LinearLayout.LayoutParams) mPositiveButton.getLayoutParams();
                positiveButtonLL.weight = 1;
                mPositiveButton.setLayoutParams(positiveButtonLL);
                LinearLayout.LayoutParams mNegativeButtonLL = (LinearLayout.LayoutParams) mNegativeButton.getLayoutParams();
                mNegativeButtonLL.weight = 1;
                mNegativeButton.setLayoutParams(mNegativeButtonLL);
                LinearLayout.LayoutParams mNeutralButtonLL = (LinearLayout.LayoutParams) mNeutralButton.getLayoutParams();
                mNeutralButtonLL.weight = 1;
                mNeutralButton.setLayoutParams(mNeutralButtonLL);

調整後效果圖

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章