0

I'm trying to alter some sample code I found. It's a one on one chat application but all commands are delivered and read by a buffered reader's br.readLine();. I need to alter that and instead of (within a while loop) wait for the user to input text in the console (via br.readLine();), but rather to wait for the enter key on an edit text to be entered.

My code sample that I have so far is :

    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;
            }

So instead of waiting at command=br.readLine();, I'd like to wait till a command has been read from the edittext. Is there a way to do this??

I've been struggling for two days now and just can't seem to get it to work.

user1397978
  • 255
  • 1
  • 7
  • 24

2 Answers2

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

        }
    });

Hope this will help you

Arunkumar
  • 224
  • 2
  • 6
0

you can use a KeyListener like that:

 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;
        }
    });

i copy the code from this question

Community
  • 1
  • 1
Rami
  • 7,879
  • 12
  • 36
  • 66
  • And by putting this in the while loop, it will only exit the while loop (as is in my code) when the enter key is pressed? – user1397978 Nov 12 '14 at 07:54
  • use a boolean parameter to exit the loop (change it from true to false in this method), and while(YOUR_BOOLEAN) – Rami Nov 12 '14 at 08:00
  • Was thinking something along those lines, but wasn't too sure. Will check it out and let you know and mark as answer when I've attempted it. Thank you for such a quick and helpful response! – user1397978 Nov 12 '14 at 08:20