如何以编程方式将自定义按钮添加到AlertDialog?

时间:2019-02-16 06:37:17

标签: android xml alertdialog

我想要这样的布局

enter image description here

当前,我已经通过在我的AlertDialog的自定义布局的XML文件中手动添加按钮来实现此目的。但是城市的数量可以不断变化,因此我想动态地添加它们。

我试图这样添加它们,

jobActivity.java

"use strict";
React.createElement("div", {
    className: "commentBox"
}, "Hello, world! I am a CommentBox.");

我的AlertDialog布局, city_filter_layout.xml

private FlexboxLayout fbBottomPopup;
private Dialog mBottomSheetDialog;
private String[] cities = {"Chennai", "Bangalore", "Tirichi", "Mumbai", "Coimbatore", "Vellore", "Karaikal", "Karaikudi", "Delhi", "Nellore", "Goa", "Kochi", "Manglore", "Jaipur", "Mysore"};

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_job);

    fbBottomPopup = findViewById(R.id.fb_bottom_popup);

    addButtonsToBottomPopup();
    createBottomPopupView();
}

private void createBottomPopupView(){
    mBottomSheetDialog = new Dialog(JobActivity.this, R.style.MaterialDialogSheet);
    mBottomSheetDialog.setContentView(R.layout.city_filter_layout); // your custom view.
    mBottomSheetDialog.setCancelable(true);
    mBottomSheetDialog.getWindow().setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    mBottomSheetDialog.getWindow().setGravity(Gravity.BOTTOM);
}

private void addButtonsToBottomPopup(){
    for(int i=0; i<cities.length;i++) {
        Button btnCities = new Button(this);
        btnCities.setId(i);
        btnCities.setLayoutParams(new FlexboxLayout.LayoutParams(FlexboxLayout.LayoutParams.WRAP_CONTENT, FlexboxLayout.LayoutParams.WRAP_CONTENT));
        btnCities.setText(cities[i]);
        btnCities.setBackgroundResource(R.drawable.round_filter_city_ns);
        btnCities.setTextColor(Color.WHITE);
        btnCities.setTextSize(15);

        fbBottomPopup.addView(btnCities);
    }
}

public void onClickCityFilter(View view){
    mBottomSheetDialog.show();
}

但是这一直让我得到Null Pointer Exception, StackTrace:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary"
    android:paddingTop="10dp"
    android:paddingRight="10dp"
    android:paddingLeft="10dp"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <TextView
        android:layout_marginTop="5dp"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="5dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="@font/abeezee_regular"
        android:textStyle="bold"
        android:textSize="20sp"
        android:textColor="@color/grey5"
        android:text="Choose Cities: "/>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginBottom="10dp"
        android:background="@color/grey2"/>

    <com.google.android.flexbox.FlexboxLayout
        android:id="@+id/fb_bottom_popup"
        android:paddingBottom="50dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:flexWrap="wrap">
    <Button
        android:id="@+id/btn3"
        android:layout_margin="5dp"
        android:background="@drawable/round_filter_city_ns"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/colorWhite"
        android:fontFamily="@font/abeezee_regular"
        android:textSize="15sp"
        android:textAllCaps="false"
        android:textStyle="bold"
        android:minWidth="0dp"
        android:minHeight="0dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"
        android:drawableLeft="@drawable/bookmark1"
        android:drawablePadding="5dp"
        android:text="Bangalore"/>

</com.google.android.flexbox.FlexboxLayout>

<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="@color/grey1"/>

<LinearLayout
    android:layout_width="match_parent"
    android:weightSum="2"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <Button
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="55dp"
        android:text="CLOSE"
        android:clickable="true"
        android:fontFamily="@font/abeezee_regular"
        android:textColor="@color/grey3"
        android:textStyle="bold"
        android:textSize="15sp"
        android:background="@android:color/transparent"
        android:layout_gravity="end"/>

    <View
        android:layout_width="1dp"
        android:layout_height="match_parent"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        android:background="@color/grey1"/>

    <Button
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="55dp"
        android:text="APPLY FILTER"
        android:clickable="true"
        android:fontFamily="@font/abeezee_regular"
        android:textColor="@color/deep_purple_darken_1"
        android:textStyle="bold"
        android:textSize="15sp"
        android:background="@android:color/transparent"
        android:layout_gravity="end"/>

</LinearLayout>

请建议我一个更好的方法来执行相同的任务,而不是手动添加它们。

0 个答案:

没有答案