Creating a Dialog after an onClick event

后端 未结 4 867
梦如初夏
梦如初夏 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:49

    just use

    public void onItemClick(AdapterView<?> parent, View
                view, int position, long id)
        {
            String[] listItems = {"Colour", "Font Size",};
            if(listItems[position].equals("Font Size"))
            {
    
    
                        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                        builder.setTitle("Choose Font Size").show();
    
    
            }
    
        }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-27 18:52

    move

     public dialog onCreateDialog(Bundle savedInstanceState) {
         AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
         builder.setTitle("Choose Font Size");
     }
    

    int the class scope, and call it from the onItemClick

    0 讨论(0)
  • 2021-01-27 19:01

    try this one

    String[] listItems = {"Colour", "Font Size",};
    ListView lv = (ListView) findViewById(R.id.settings_list);
    
    lv.setAdapter(new ArrayAdapter<String>
    (this, android.R.layout.simple_list_item_1, listItems));
    
    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"))
            {
    
    
                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                getApplicationContext());
    
                // set title
                alertDialogBuilder.setTitle("Choose Font Size");
    
                //create alert dialog
                AlertDialog alertDialog = alertDialogBuilder.create();
    
                // show it
                alertDialog.show();
    
            }
    
        }
    });
    
    0 讨论(0)
提交回复
热议问题