gpt4 book ai didi

android - 当有人没有在纯文本框中输入任何内容时,如何弹出错误消息?

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

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

input_msg.setText("");
}
});


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

所以这个功能是针对聊天室的,所以我希望能够做到这一点,这样用户就可以向聊天室发送空白消息。所以我希望在用户尝试发送空白消息时发生错误。

谢谢!

最佳答案

检查输入文本并显示 toast 或您想要的任何内容。

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

String inputMsg= usernameEditText.getText().toString();
if (inputMsg.matches("")) {
Toast.makeText(this, "You did not enter a value", Toast.LENGTH_SHORT).show();
return;
}
}
});

关于android - 当有人没有在纯文本框中输入任何内容时,如何弹出错误消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41550916/

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