使用预定义按钮发送消息的应用程序

时间:2017-05-11 00:06:02

标签: android button firebase android-notifications firebase-cloud-messaging

我的问题是关于我的消息发送应用程序。我试图按下按钮发送消息,但我被困在某事上。我查看了一个教程并按照它,并设法将代码更改为我自己的功能,但我无法弄清楚如何通过单击按钮发送消息(例如,当我按下红色按钮时,我必须按"发送"按钮才能实际发送信息。我想在按下红色按钮后发送信息。)

理想情况下,我想在我的程序中实现两者,但它花费的时间比我想象的要长,所以我只是坚持原来的目标。任何人都可以看到我应该在哪里更改我的代码吗?

package app.real_time_chat;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.view.View.OnClickListener;


import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * Created by David on 5/01/2017
 */
public class Chat_Room  extends AppCompatActivity{

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

private String user_name,room_name;
private DatabaseReference root ;
private String temp_key;
private String greeting;                                                            
//for displaying button messages

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


    button_msg = (Button)findViewById(R.id.button_red);
    button_msg.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View arg0) {
            greeting = "It's Red" ;                                                 
//for displaying red color

        }});

    button_msg = (Button)findViewById(R.id.button_blue);
    button_msg.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View arg0) {
            greeting = "It's Blue" ;                                                  //for displaying blue color

        }});

    button_msg = (Button)findViewById(R.id.button_green);
    button_msg.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View arg0) {
            greeting = "It's Green" ;                                                  
//for displaying green color

        }});

    button_msg = (Button)findViewById(R.id.button_yellow);
    button_msg.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View arg0) {
            greeting = "It's Yellow" ;                                                  //for displaying yellow color

        }});

    btn_send_msg = (Button) findViewById(R.id.btn_send);

//      btn_send_msg = (Button) findViewById(R.id.btn_send);                               


    input_msg = (EditText) findViewById(R.id.msg_input);
    chat_conversation = (TextView) findViewById(R.id.textView);

    user_name = getIntent().getExtras().get("user_name").toString();                //converts username to string to display to screen
    room_name = getIntent().getExtras().get("room_name").toString();                //converts room name to string to display to screen
    setTitle(" Room - "+room_name);                                                 //displays pre-set room name to top of app page

    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",greeting);
 //             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 个答案:

答案 0 :(得分:1)

你可以点击添加电话到

等方法
button_msg.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View arg0) {
            greeting = "It's Yellow" ;                                                  //for displaying yellow color
           btn_send_msg.callOnClick();
        }});

如果你想同时做两件事你应该做这样的事情并制作一个新的方法

添加

private void sendMessage(String message)
{
     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",message);
 //             map2.put("msg",input_msg.getText().toString());                           



            message_root.updateChildren(map2);
}

那么按钮应该是这样的     button_msg =(Button)findViewById(R.id.button_red);     button_msg.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View arg0) {
            sendMessage("It's Red");                                                 
//for displaying red color

        }});

并且发送输入的发送按钮将是

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

            sendMessage(input_msg.getText().toString());                           
        }
    });