gpt4 book ai didi

android - 带有预定义按钮的消息发送应用程序

转载 作者:行者123 更新时间:2023-11-30 00:33:14 25 4
gpt4 key购买 nike

我的问题是关于我的消息发送应用程序。我试图通过按下按钮发送消息,但我被卡住了。我查看了一个教程并按照它进行了操作,并设法将代码更改为我自己的函数,但我无法弄清楚如何在单击按钮时发送消息(例如,当我按下红色按钮时,我有按下“发送”按钮实际发送消息。我想在按下红色按钮后发送消息)。

理想情况下,我想将两者都实现到我的程序中,但它花费的时间比我想象的要长,所以我只是坚持我最初的目标。谁能看到我应该在哪里更改我的代码?

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


}
}

最佳答案

您可以将点击调用添加到方法中,例如

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

关于android - 带有预定义按钮的消息发送应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43904468/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com