我想在字符串fakeMessage中阻止此消息,但它不起作用,我不知道为什么..............任何人都有任何想法这样做
sendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String messageText = messageArea.getText().toString();
String fakeMessage = "hello you win 1000$ with us send your Email pls";
if(!messageText.equals("")){
Map<String, String> map = new HashMap<String, String>();
map.put("message", messageText);
map.put("user", UserDetails.username);
for (String key : map.keySet()) {
if (map.get(key) == fakeMessage) {
Toast.makeText(Chat.this, "This Is A Fake Message and We Can't Send It", Toast.LENGTH_SHORT).show();
Toast.makeText(Chat.this, "Please Don't Try To Send It Again ", Toast.LENGTH_SHORT).show();
} else {
reference1.push().setValue(map);
reference2.push().setValue(map);
messageArea.setText("");
}
}
}
}
});
答案 0 :(得分:0)
请更改
if (map.get(key) == fakeMessage)
为:
if (map.get(key).equals(fakeMessage))
==
比较对象引用,其中equals
比较String对象的内容。它的男生概念。
答案 1 :(得分:0)
如果在大写或小写的情况下消息不匹配,则使用equals ignore case进行比较
第二,你正在推动和设置文本两次在for循环你应该只有一次,如果有错误你可以返回
sendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String messageText = messageArea.getText().toString();
String fakeMessage = "hello you win 1000$ with us send your Email pls";
if(!messageText.equals("")){
Map<String, String> map = new HashMap<String, String>();
map.put("message", messageText);
map.put("user", UserDetails.username);
for (String key : map.keySet()) {
if (map.get(key).equalsIgnoreCase(fakeMessage)) {
Toast.makeText(Chat.this, "This Is A Fake Message and We Can't Send It", Toast.LENGTH_SHORT).show();
Toast.makeText(Chat.this, "Please Don't Try To Send It Again ", Toast.LENGTH_SHORT).show();
return;
}
}
reference1.push().setValue(map);
reference2.push().setValue(map);
messageArea.setText("");
}
}
});