如何在单独的Java文件中创建警报对话框?

时间:2019-05-17 10:01:15

标签: android android-alertdialog

我要创建一个如下所示的警报对话框:

enter image description here

我可以使用以下代码以编程方式创建警报对话框:

private fun openDataEntryDialog(title: String, description: String) {



        val mBuilder = AlertDialog.Builder(mContext)
        val mView = layoutInflater.inflate(R.layout.dialog_entry_data,null)
        mBuilder.setView(mView)
        val dataEntryDialog = mBuilder.create()

        val titleTextView : TextView = mView.findViewById(R.id.textView_title_data_entry_dialog)
        val descriptionTextView : TextView = mView.findViewById(R.id.textView_description_data_entry_dialog)
        val cancelTextView : TextView = mView.findViewById(R.id.textView_cancel_action_entry_data_dialog)
        val changeTextView : TextView = mView.findViewById(R.id.textView_change_action_entry_data_dialog)

        titleTextView.text = title
        descriptionTextView.text = description

        cancelTextView.setOnClickListener {
            dataEntryDialog.dismiss()

        }

        changeTextView.setOnClickListener {
            dataEntryDialog.dismiss()
        }


        dataEntryDialog.show()





    }

但是我实际上想创建一个处理警报对话框的类,就像在制作模态底部工作表时一样,从该类中,我将使用interface(listener)带来数据

下面的代码是制作模态底板时的单独类

class UserControlModalBottomSheet : BottomSheetDialogFragment() {

    lateinit var mListener : UserControlModalBottomSheetListener

    fun setListener(listener: UserControlModalBottomSheetListener) {
        mListener = listener
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {

        val view = inflater.inflate(R.layout.modal_bottom_sheet_user_control, container, false)

        val helpUsTextView : TextView = view.findViewById(R.id.textView_help_us_user_control_modal_bottom_sheet)
        val settingsTextView : TextView = view.findViewById(R.id.textView_setting_user_control_modal_bottom_sheet)
        val editProfileTextView : TextView = view.findViewById(R.id.textView_edit_profile_user_control_modal_bottom_sheet)

        helpUsTextView.setOnClickListener {
            mListener.onHelpUsSectionClicked()
            dismiss()
        }

        editProfileTextView.setOnClickListener {
            mListener.onEditProfileSectionClicked()
            dismiss()
        }

        settingsTextView.setOnClickListener {
            mListener.onSettingSectionClicked()
            dismiss()
        }

        return view


    }
}

然后在片段中,我可以像下面这样使用该类:

 private fun openBottomSheetDialog() {

        val fragManager = fragmentManager ?: return
        val bottomSheet = UserControlModalBottomSheet()

        bottomSheet.setListener(object:
            UserControlModalBottomSheetListener {
            override fun onHelpUsSectionClicked() {
                val helpUsDestination = UserControlFragmentDirections.actionToHelpUs()
                Navigation.findNavController(fragmentView).navigate(helpUsDestination)

            }

            override fun onEditProfileSectionClicked() {
                val editProfileDestination = UserControlFragmentDirections.actionToEditProfile()
                Navigation.findNavController(fragmentView).navigate(editProfileDestination)

            }

            override fun onSettingSectionClicked() {
                val settingsDestination = UserControlFragmentDirections.actionToSettings()
                Navigation.findNavController(fragmentView).navigate(settingsDestination)

            }

        })


        bottomSheet.show(fragManager,"UserControlFragment")

    }

那么如何像上面的代码一样在单独的Java文件中创建自定义警报对话框?我是Android新手,似乎所有教程都没有在单独的Java文件中创建

这是对话框的xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
                                                   xmlns:app="http://schemas.android.com/apk/res-auto"
                                                   xmlns:tools="http://schemas.android.com/tools"
                                                   android:layout_width="match_parent"
                                                   android:layout_height="wrap_content"
                                                   android:background="@color/colorBackgroundDark">


    <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content" android:id="@+id/textView_title_data_entry_dialog"
            tools:text="Title Data Entry"
            android:textColor="@android:color/black"
            app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="8dp"
            app:layout_constraintStart_toStartOf="parent" android:layout_marginStart="8dp"
            android:layout_marginTop="16dp" app:layout_constraintTop_toTopOf="parent" android:textAlignment="center"/>
    <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content" android:id="@+id/textView_description_data_entry_dialog"
            android:textColor="@android:color/black"
            android:layout_marginTop="16dp"
            app:layout_constraintTop_toBottomOf="@+id/textView_title_data_entry_dialog"
            app:layout_constraintStart_toStartOf="parent"
            android:layout_marginStart="32dp" app:layout_constraintEnd_toEndOf="parent"
            android:layout_marginEnd="32dp" android:maxLines="2" tools:text="This is the description of data entry."
            app:layout_constraintHorizontal_bias="0.495" android:textAlignment="center"/>
    <EditText
            android:layout_width="0dp"
            android:layout_height="35dp"
            android:background="@drawable/rectangle_outline"
            android:inputType="textPersonName"
            android:maxLines="1"
            android:ems="10"
            android:id="@+id/editText_input_data_dialog_entry_data" android:layout_marginStart="32dp"
            app:layout_constraintStart_toStartOf="parent" android:layout_marginEnd="32dp"
            app:layout_constraintEnd_toEndOf="parent" android:layout_marginTop="16dp"
            app:layout_constraintTop_toBottomOf="@+id/textView_description_data_entry_dialog"
            android:paddingStart="8dp" android:paddingEnd="8dp"/>
    <View
            android:layout_width="0dp"
            android:layout_height="1dp"
            android:id="@+id/view_horizontal_entry_data_dialog"
            android:background="@color/colorLine"
            android:layout_marginTop="16dp"
            app:layout_constraintTop_toBottomOf="@+id/editText_input_data_dialog_entry_data"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"/>

    <TextView
            android:text="Batal"
            android:layout_width="0dp"
            android:layout_height="35dp"
            app:layout_constraintWidth_percent="0.5"
            android:id="@+id/textView_cancel_action_entry_data_dialog"
            android:gravity="center"
            android:textAlignment="center"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/editText_input_data_dialog_entry_data"
            android:layout_marginTop="16dp"/>


    <TextView
            android:text="Ubah"
            android:layout_width="0dp"
            android:layout_height="35dp"
            app:layout_constraintWidth_percent="0.5"
            android:id="@+id/textView_change_action_entry_data_dialog"
            android:gravity="center"
            android:textAlignment="center"
            app:layout_constraintStart_toEndOf="@+id/textView_cancel_action_entry_data_dialog"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/editText_input_data_dialog_entry_data"
            android:layout_marginTop="16dp"/>
    <View
            android:layout_width="1dp"
            android:layout_height="49dp"
            android:id="@+id/view_vertical_entry_data_dialog"
            android:background="@color/colorLine"
            app:layout_constraintEnd_toStartOf="@+id/textView_change_action_entry_data_dialog"
            app:layout_constraintStart_toEndOf="@+id/textView_cancel_action_entry_data_dialog"
            app:layout_constraintTop_toBottomOf="@+id/view_horizontal_entry_data_dialog"
            app:layout_constraintBottom_toBottomOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

java没问题

1 个答案:

答案 0 :(得分:0)

您可以使用DialogFragment

在您单击按钮时的类或片段中:

someBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                     FragmentTransaction ft = getFragmentManager().beginTransaction();
                    Fragment prev = getFragmentManager().findFragmentByTag("dialog");
                    if (prev != null) {
                        ft.remove(prev);
                    }
                    ft.addToBackStack(null);
                    DialogFragment dialogFragment = new OpportunityDialogFragment();
                    Bundle bundle = new Bundle();
                    bundle.putSerializable(Constants.visitObject,visit);
                    dialogFragment.setArguments(bundle);
                    dialogFragment.show(ft, "dialog");
                }
            });

创建一个对话框片段类,以扩展自定义布局:

public class OpportunityDialogFragment extends DialogFragment implements View.OnClickListener  {

private VisitsAdded visitsAdded;
Button convertToOppBtn, editVisitBtn;
TextView companyName, location,visitSubject;
ImageView closeDialogImage;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.dialog_fragment_opportunity, container, false);
    // Do all the stuff to initialize your custom view
    return v;
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    //retrieving data from fragment/activity
    Bundle bundle = getArguments();
    visitsAdded = (VisitsAdded) bundle.getSerializable(Constants.visitObject);

    // Do whatever you want here
}

@Override
public void onClick(View v) {

    switch (v.getId()) {

        case R.id.btn_convert_to_opportunity:

            if (getActivity() != null) {
                Intent i = new Intent(getActivity(), AddEditOpportunityActivity.class);
                i.putExtra(Constants.visitObject,visitsAdded);
                getActivity().startActivity(i);

                OpportunityDialogFragment.this.dismiss();//dismisses the dialog
            }

            break;

        case R.id.close_opportunity_pop_up:

            OpportunityDialogFragment.this.dismiss();

            break;


    }
}
}

dialog_fragment_opportunity.xml(有点类似于您的要求):

  <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:minWidth="1000dp"
    android:minHeight="100dp"
    android:orientation="vertical">


<ImageView
    android:id="@+id/close_opportunity_pop_up"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="end"
    android:padding="6dp"
    android:src="@drawable/ic_close_grey_500_24dp" />


<TextView
    android:id="@+id/popup_visit_subject"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"

    android:gravity="center"
    android:paddingStart="16dp"
    android:paddingLeft="16dp"
    android:paddingEnd="16dp"
    android:paddingRight="16dp"
    android:text="Jal El dib, Lebanon"
    android:textColor="@color/grey_700" />

<TextView
    android:id="@+id/popup_visit_customer_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:paddingStart="16dp"
    android:paddingLeft="16dp"
    android:paddingEnd="16dp"
    android:paddingRight="16dp"
    android:layout_marginTop="8dp"
    android:text="STE. JEAN CLAUDE YARED "
    android:textColor="@color/colorPrimary" />

<TextView
    android:id="@+id/popup_visit_location_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="8dp"
    android:gravity="center"
    android:paddingStart="16dp"
    android:paddingLeft="16dp"
    android:paddingEnd="16dp"
    android:paddingRight="16dp"
    android:text="Jal El dib, Lebanon"
    android:textColor="@color/grey_700" />


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="36dp"
    android:layout_marginStart="12dp"
    android:layout_marginTop="64dp"
    android:layout_marginEnd="12dp"
    android:layout_marginBottom="16dp"
    android:orientation="horizontal"
    android:weightSum="1">

    <Button
        android:id="@+id/btn_convert_to_opportunity"
        android:layout_width="0dp"
        android:layout_height="32dp"
        android:layout_marginEnd="6dp"
        android:layout_weight="0.5"
        android:background="@drawable/curved_rectangle_red"
        android:maxLines="1"
        android:paddingStart="4dp"
        android:paddingEnd="4dp"
        android:text="@string/convert_to_opportunity"
        android:textAllCaps="false"
        android:textColor="@color/white"
        android:textSize="11sp" />

    <Button
        android:id="@+id/popup_btn_edit_visit"
        android:layout_width="0dp"
        android:layout_height="32dp"
        android:layout_marginStart="12dp"
        android:layout_weight="0.5"
        android:background="@drawable/curved_rectangle_red"
        android:text="@string/edit_visit"
        android:textAllCaps="false"
        android:textColor="@color/white"
        android:textSize="11sp" />

</LinearLayout>

希望这对您有帮助!