有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!


共 (2) 个答案

  1. # 1 楼答案

    试试这个

      private void AlertDialogView() {
        final CharSequence[] items = { "One", "Two", "Three", "Four" };
    
        AlertDialog.Builder builder = new AlertDialog.Builder(ShowDialog.this);//ERROR ShowDialog cannot be resolved to a type
        builder.setTitle("Alert Dialog with ListView and Radio button");
        builder.setSingleChoiceItems(items, -1,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int item) {
                        Toast.makeText(getApplicationContext(), items[item],
                                Toast.LENGTH_SHORT).show();
                    }
                });
    
        builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                Toast.makeText(ShowDialog.this, "Success", Toast.LENGTH_SHORT)
                        .show();
            }
        });
    
        builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                Toast.makeText(ShowDialog.this, "Fail", Toast.LENGTH_SHORT)
                        .show();
            }
        });
    
        AlertDialog alert = builder.create();
        alert.show();
    }
    
  2. # 2 楼答案

    考虑到listView是您的列表视图,MainActivity是您活动的名称

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            new AlertDialog.Builder(MainActivity.this)
                    .setTitle("Title")
                    .setMessage("Position clicked: " + position)
                    .create()
                    .show();
        }
    });