setText 什么都不做

setText does nothing

public class gameHomeScreen extends Activity implements View.OnClickListener{

    public static Socket socket;
    private Button button;
    private PrintWriter printwriter;
    private BufferedReader bufferedReader;
    String serverReply = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_game_home_screen);
        if(login.client!=null){
            socket = login.client;
        }else if(signup.client!=null){
            socket = signup.client;
        }
        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(this);
    }



    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
    }

    public void onBackPressed() {
        moveTaskToBack(true);
    }
    @Override
    public void onClick(View v) {
        button.setText("Waiting...");
        SendMessage sm = new SendMessage();
        sm.execute();
        while(!serverReply.equals("game_found")) {
            //do nothing
        }
        Intent i = new Intent(this, InGame.class);
        startActivity(i);
        finish();


    }
    private class SendMessage extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {
            try {
                printwriter = new PrintWriter(socket.getOutputStream(), true);
                printwriter.println("game_queue"); // write the message to output stream

                InputStreamReader inputStreamReader = new InputStreamReader(socket.getInputStream());
                bufferedReader = new BufferedReader(inputStreamReader);
                do {
                    serverReply = bufferedReader.readLine();
                }while(!serverReply.equals("game_found"));


            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }


        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);

        }
    }
}

这行代码不起作用:

button.setText("Waiting...");

我试过使用处理程序并使用 UIThread 但没有任何效果。每当找到游戏时,文本只会在半秒内变为 "waiting...",这很奇怪...

您正在挂起 UI 话题:

    while(!serverReply.equals("game_found")) {
        //do nothing
    }

不要在 onClick 方法上执行此操作。此方法是从 UI 线程调用的,您将其锁定(这也可能会给您带来 ANR)。

onClick 中删除循环:

@Override
public void onClick(View v) {
    button.setText("Waiting...");
    SendMessage sm = new SendMessage();
    sm.execute();
    //while(!serverReply.equals("game_found")) {
    //    do nothing
    //}
    //Intent i = new Intent(this, InGame.class);
    //startActivity(i);
    //finish();
}

并在您的 AsyncTask 中启动意图:

private class SendMessage extends AsyncTask<Void, Void, Void> {

    // ...

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);

        Intent i = new Intent(this, InGame.class);
        startActivity(i);
        finish();
    }
}