Android Studio:如何根据用户单击的位置创建特定的警报对话框

发布于 2025-02-09 15:48:21 字数 1264 浏览 0 评论 0原文

我是Android Studio编程的新手。我的程序有一个带有3个文本视图的Recyclerview和一个图像。我希望根据用户在屏幕上单击的位置出现一个警报对话框,例如,如果用户单击对话框的图像,则“您单击了图像”。如果用户单击文本,则会说“您单击文本”。 我的单击听众如下:itemview.setOnClickListener(this);

   @Override
    public void onClick(View view) {

        int index = getAdapterPosition();
        if (playerNames.equals(index)) {
            showDialogPlayerNames();
        }
        if (numberPlayers.equals(index)){
            showDialogPlayerNumber();
        }
        if (ScorePlayers.equals(index)){
            showDialogPlayerScore();
        }
        if (picturePlayers.equals(index)) {
            showDialogPlayerImage();
        }
    }
    }

这是我的警报对话框代码:

void showDialogPlayerNames() {
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
    alertDialog.setTitle("Click Information");
    alertDialog.setIcon(R.drawable.ic_baseline_info_24);
    alertDialog.setMessage("You clicked Player Name");
    alertDialog.setNegativeButton("Dismiss", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            dialogInterface.dismiss();
        }
    });

    alertDialog.create().show();
}

谢谢

I am new to Android Studio programming. My program has a recyclerview with 3 Textviews, and one image. I would like an alert dialog to appear based on where the user clicks on the screen, for instance if the user clicks on the image the dialog says, "You clicked on image". If the user clicks on the text, it would say "You clicked on the text".
My onclick listener is as follows: itemView.setOnClickListener(this);

   @Override
    public void onClick(View view) {

        int index = getAdapterPosition();
        if (playerNames.equals(index)) {
            showDialogPlayerNames();
        }
        if (numberPlayers.equals(index)){
            showDialogPlayerNumber();
        }
        if (ScorePlayers.equals(index)){
            showDialogPlayerScore();
        }
        if (picturePlayers.equals(index)) {
            showDialogPlayerImage();
        }
    }
    }

Here is my alert dialog code:

void showDialogPlayerNames() {
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
    alertDialog.setTitle("Click Information");
    alertDialog.setIcon(R.drawable.ic_baseline_info_24);
    alertDialog.setMessage("You clicked Player Name");
    alertDialog.setNegativeButton("Dismiss", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            dialogInterface.dismiss();
        }
    });

    alertDialog.create().show();
}

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

榕城若虚 2025-02-16 15:48:21

您需要在方法中添加参数

showDialogPlayerNames("Something text here");


void showDialogPlayerNames(String myText) {
    alertDialog.setMessage(myText);
}

You need to add parameters in your methods

showDialogPlayerNames("Something text here");


void showDialogPlayerNames(String myText) {
    alertDialog.setMessage(myText);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文