Creating a Dialog after an onClick event

后端 未结 4 868
梦如初夏
梦如初夏 2021-01-27 18:26

Hi i am working the first time with dialogs. i want to create a dialog after an item in a listView was clicked:

    String[] listItems = {\"Colour\", \"Font Size         


        
4条回答
  •  鱼传尺愫
    2021-01-27 18:51

    Call this method in your onItemClick method :

     public void showMyCustomDialog(Context context, String title, String message)
        {
            final Dialog dialog = new Dialog(context);
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            //tell the Dialog to use the customalertdialog.xml as it's layout description
            dialog.setContentView(R.layout.customalertdialog);
            TextView titleText = (TextView) dialog.findViewById(R.id.txtTitleAlertDialog);
            titleText.setText(title);
            TextView txt = (TextView) dialog.findViewById(R.id.txtAlertDialog);
            txt.setText(message);
            Button dialogButton = (Button) dialog.findViewById(R.id.buttonAlertDialogCancel);
            dialogButton.setText("OK");
            dialogButton.setOnClickListener(new View.OnClickListener() 
            {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
            dialog.show();
        }
    

    Something like below :

    lv.setOnItemClickListener(new OnItemClickListener()
        {
            public void onItemClick(AdapterView parent, View
                    view, int position, long id)
            {
                String[] listItems = {"Colour", "Font Size",};
                if(listItems[position].equals("Font Size"))
                {
                        public dialog onCreateDialog(Bundle savedInstanceState)
                        {
                            showMyCustomDialog(Context context, String title, String message) // pass title, message and activity context in this method.
                        }
                }
            }
        });
    

    Hope it will help.

提交回复
热议问题