我正在开发我的第一个应用程序,这是我正在尝试开发的一种游戏。我从包含onclick的函数返回所需数据时遇到问题。
以下是调用该函数以返回task_tb_done字符串的代码。

name_win_player_ch_screen.setText(player2_name);
        name_lost_player_ch_screen.setText(player1_name);
        String challenge2= challenge_selector(task_type);
        task_tb_done.setText(player1_name+" "+challenge2);
        challenge2="";


这是处理该请求的代码。在while条件循环中分配的质询将返回,但不会立即返回,但应在下次调用此函数部分时返回值。我认为OnClik方法存在一些问题,因为在其他挑战选择中,我没有使用onClicks,而是它们为我提供了理想的价值以作为回报。

 else if(task_type.equals("Truth or Dare")){


        firstplayer_screen.setVisibility(View.INVISIBLE);
        secondplayer_screen.setVisibility(View.INVISIBLE);
        game_screen_table.setVisibility(View.INVISIBLE);
        truth_or_dare_selection_screen.setVisibility(View.VISIBLE);

        //A pop up to ask Either they want a truth or dare
        //upon selection will give them task to do or to tell the truth.

        truth_select_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                truesize =  truth.length;

                int i = (int) (Math.random() * ((truesize - 0) + 1));
                if(i<=truesize) {
                    c=1;
                    toast.setText("Hey" + truth[i]);
                    toast.show();
                    challenge = truth[i];
                    truth_or_dare_selection_screen.setVisibility(View.INVISIBLE);
                }
                else{
                    challenge_selector(task_type);
                    toast.setText("Please Click again");
                    c=0;
                    toast.show();

                }
            }
        });
        dare_select_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                daresize =  dare.length;

                int i = (int) (Math.random() * ((daresize - 0) + 1));

                if(i<=truesize) {
                    c=1;

                    toast.setText("Hey" + dare[i]);
                    toast.show();
                    challenge = dare[i];
                    truth_or_dare_selection_screen.setVisibility(View.INVISIBLE);
                }
                else{
                    challenge_selector(task_type);
                    toast.setText("Please Click again");
                    c=0;
                    toast.show();
                }
            }
        });

        while (c==1) {
            c=0;
            truth_or_dare_selection_screen.setVisibility(View.INVISIBLE);
            return challenge;
        }

最佳答案

第一次执行“ c”变量的值是什么?
如果不是1,它将不会进入while循环,也不会返回质询。

如果要一直停留在while循环内,直到用户单击其中一个按钮,则应尝试以下操作:

while (c != 1) {

}
return challenge;


这样,它将无限循环,直到用户单击(c = 1),然后循环结束,您将遇到“返回挑战”。

尽管您应该考虑针对此类行为使用回调,并在Activity onCreate方法中设置OnClickListeners。

10-07 19:18
查看更多