在onClick事件后创建一个Dialog

时间:2013-10-18 17:29:59

标签: java android dialog onclick

嗨,我第一次使用对话框。 我想在单击listView中的项目后创建一个对话框:

    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"))
            {


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



            }

        }
    });

我在onCreateDialog上遇到错误(Bundle savedInstanceState) Eclipse想要一个;在Bundle savedInstanceState周围,但developer.android说不然。 有什么提示吗?

4 个答案:

答案 0 :(得分:2)

在onItemClick方法中调用此方法:

 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();
    }

如下所示:

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.
                    }
            }
        }
    });

希望它会有所帮助。

答案 1 :(得分:0)

移动

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

,并从onItemClick

调用它

答案 2 :(得分:0)

只需使用

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();


        }

    }

答案 3 :(得分:0)

尝试这个

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();

        }

    }
});