android edittext替换br.readLine()

时间:2014-11-12 07:21:11

标签: android android-edittext chat

我正在尝试更改我找到的一些示例代码。它是一对一的聊天应用程序,但所有命令都由缓冲的阅读器br.readLine();传送和读取。我需要改变它而不是(在一个while循环中)等待用户在控制台中输入文本(通过br.readLine();),而是等待输入编辑文本的回车键。

我到目前为止的代码示例是:

    String command;
    while(true) {
        try {
            command=br.readLine();
        } catch (IOException e) {
            System.err.println("Error reading command");
            if(pendingChatAccept) {
                sendThroughSocket(Constants.Client.CHAT_DENY, clientSocket, pendingAcceptClient.getIP(), pendingAcceptClient.getPort());    //deny the request
                pendingChatAccept=false;
                pendingAcceptClient=null;
            }
            continue;
        }

        if(pendingChatAccept) {
            char c=command.charAt(0);
            if(c=='y') 
            {
                System.out.println("\nRequest Accepted. Type 'm' to send a message.");
                sendThroughSocket(Constants.Client.CHAT_ACCEPT, clientSocket, pendingAcceptClient.getIP(), pendingAcceptClient.getPort());  //accept the request
                currentChatPartner=pendingAcceptClient;
                currentlyChatting=true;
                pendingChatAccept=false;
                pendingAcceptClient=null;
                msgQueue.clear();   //clear message queue
            } else {
                System.out.println("\nRequest Denied.");
                sendThroughSocket(Constants.Client.CHAT_DENY, clientSocket, pendingAcceptClient.getIP(), pendingAcceptClient.getPort());    //deny the request
                pendingChatAccept=false;
                pendingAcceptClient=null;
            }

因此,我不想在command=br.readLine();等待,而是等到从edittext中读取命令。有没有办法做到这一点??

我现在已经挣扎了两天,似乎无法让它发挥作用。

2 个答案:

答案 0 :(得分:0)

 EditText et=new EditText(getApplicationContext());


    et.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub

        }
    });

希望这会对你有所帮助

答案 1 :(得分:0)

您可以使用 KeyListener

 final EditText edittext = (EditText) findViewById(R.id.edittext);
    edittext.setOnKeyListener(new OnKeyListener() {
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            // If the event is a key-down event on the "enter" button
            if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
                (keyCode == KeyEvent.KEYCODE_ENTER)) {
              // Perform action on key press
              Toast.makeText(HelloFormStuff.this, edittext.getText(), Toast.LENGTH_SHORT).show();
              return true;
            }
            return false;
        }
    });

我从this question

复制代码