2017-01-05 74 views
1
public class Chat_Room extends AppCompatActivity { 

    private Button btn_send_msg; 
    private EditText input_msg; 
    private TextView chat_conversation; 

    private String user_name,room_name; 
    private DatabaseReference root; 
    private String temp_key; 

@Override 
protected void onCreate(@Nullable Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.chat_room); 

    btn_send_msg = (Button) findViewById(R.id.button13); 
    input_msg = (EditText) findViewById(R.id.editText3); 
    chat_conversation = (TextView) findViewById(R.id.textView4); 

    user_name = getIntent().getExtras().get("user_name").toString(); 
    room_name = getIntent().getExtras().get("room_name").toString(); 
    setTitle("Room - " + room_name); 

    root = FirebaseDatabase.getInstance().getReference().child(room_name); 

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

              Map<String, Object> map = new HashMap<String, Object>(); 
              temp_key = root.push().getKey(); 
              root.updateChildren(map); 

              DatabaseReference message_root = root.child(temp_key); 
              Map<String, Object> map2 = new HashMap<String, Object>(); 
              map2.put("name", user_name); 
              map2.put("msg", input_msg.getText().toString()); 

              message_root.updateChildren(map2); 
             } 
            }); 


    root.addChildEventListener(new ChildEventListener() { 
     @Override 
     public void onChildAdded(DataSnapshot dataSnapshot, String s) { 
      append_chat_conversation(dataSnapshot); 
     } 

     @Override 
     public void onChildChanged(DataSnapshot dataSnapshot, String s) { 

      append_chat_conversation(dataSnapshot); 
     } 

     @Override 
     public void onChildRemoved(DataSnapshot dataSnapshot) { 

     } 

     @Override 
     public void onChildMoved(DataSnapshot dataSnapshot, String s) { 

     } 

     @Override 
     public void onCancelled(DatabaseError databaseError) { 

     } 
    }); 

} 
private String chat_msg,chat_user_name; 
private void append_chat_conversation(DataSnapshot dataSnapshot) { 

    Iterator i = dataSnapshot.getChildren().iterator(); 

    while (i.hasNext()){ 

     chat_msg = (String) ((DataSnapshot)i.next()).getValue(); 
     chat_user_name = (String) ((DataSnapshot)i.next()).getValue(); 

     chat_conversation.append(chat_user_name + " : " + chat_msg + "\n"); 
    } 
} 

基本上我想能夠發送消息使用當前發生的按鈕,但我想要的文本已經被鍵入並且在按鈕被點擊時發送消失。我想清除純文本框中的文本時,我按下按鈕,但無法找到任何東西

這是一個聊天室,所以這是必須停止用戶變得惱火。

謝謝!

+1

心不是設置view.setText(「」)它'textview.setText(「」);'? –

+0

將文本框的內容設置爲「」 – Prabu

+1

可能的重複[如何清除TextView的值?](http://stackoverflow.com/questions/9037605/how-i-can-clear-the-value-的-的TextView) –

回答

0

您需要TextView的文本設置爲空字符串:

textview.setText(""); 
0

你必須點擊監聽器上月底

@Override 
public void onClick(View view) { 

    Map<String, Object> map = new HashMap<String, Object>(); 
    temp_key = root.push().getKey(); 
    root.updateChildren(map); 

    DatabaseReference message_root = root.child(temp_key); 
    Map<String, Object> map2 = new HashMap<String, Object>(); 
    map2.put("name", user_name); 
    map2.put("msg", input_msg.getText().toString()); 

    message_root.updateChildren(map2); 
    input_msg.setText(""); 
} 
相關問題