我的主要活动
public class MainActivity extends AppCompatActivity implements NotificationCenter.NotificationCenterDelegate {
private ListView chatListView;
private EditText chatEditText1;
private ArrayList<ChatMessage> chatMessages;
private ImageView enterChatView1, emojiButton;
private EmojiView emojiView;
private ChatListAdapter listAdapter;
private SizeNotifierRelativeLayout sizeNotifierRelativeLayout;
private boolean showingEmoji;
private int keyboardHeight;
private boolean keyboardVisible;
private WindowManager.LayoutParams windowLayoutParams;
private EditText.OnKeyListener keyListener = new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// If the event is a key-down event on the "enter" button
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER)) {
// Perform action on key press
EditText editText = (EditText) v;
if(v==chatEditText1)
{
sendMessage(editText.getText().toString(), UserType.OTHER);
}
chatEditText1.setText("");
return true;
}
return false;
}
};
private ImageView.OnClickListener clickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
if(v==enterChatView1)
{
sendMessage(chatEditText1.getText().toString(), UserType.OTHER);
}
chatEditText1.setText("");
}
};
private final TextWatcher watcher1 = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
if (chatEditText1.getText().toString().equals("")) {
} else {
enterChatView1.setImageResource(R.drawable.input_send);
}
}
@Override
public void afterTextChanged(Editable editable) {
if(editable.length()==0){
enterChatView1.setImageResource(R.drawable.input_send);
}else{
enterChatView1.setImageResource(R.drawable.input_send);
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AndroidUtilities.statusBarHeight = getStatusBarHeight();
getWindow().setBackgroundDrawable(getResources().getDrawable(R.drawable.background));
chatMessages = new ArrayList<>();
chatListView = (ListView) findViewById(R.id.chat_list_view);
chatEditText1 = (EditText) findViewById(R.id.chat_edit_text1);
enterChatView1 = (ImageView) findViewById(R.id.enter_chat1);
// Hide the emoji on click of edit text
chatEditText1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (showingEmoji)
hideEmojiPopup();
}
});
emojiButton = (ImageView)findViewById(R.id.emojiButton);
emojiButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showEmojiPopup(!showingEmoji);
}
});
listAdapter = new ChatListAdapter(chatMessages, this);
chatListView.setAdapter(listAdapter);
chatEditText1.setOnKeyListener(keyListener);
enterChatView1.setOnClickListener(clickListener);
chatEditText1.addTextChangedListener(watcher1);
//sizeNotifierRelativeLayout = (SizeNotifierRelativeLayout) findViewById(R.id.chat_layout);
//sizeNotifierRelativeLayout.delegate = this;
NotificationCenter.getInstance().addObserver(this, NotificationCenter.emojiDidLoaded);
}
private void sendMessage(final String messageText, final UserType userType)
{
if(messageText.trim().length()==0)
return;
final ChatMessage message = new ChatMessage();
message.setMessageStatus(Status.SENT);
message.setMessageText(messageText);
message.setUserType(userType);
message.setMessageTime(new Date().getTime());
chatMessages.add(message);
if(listAdapter!=null)
listAdapter.notifyDataSetChanged();
// Mark message as delivered after one second
final ScheduledExecutorService exec = Executors.newScheduledThreadPool(1);
exec.schedule(new Runnable() {
@Override
public void run() {
message.setMessageStatus(Status.DELIVERED);
final ChatMessage message = new ChatMessage();
message.setMessageStatus(Status.SENT);
message.setMessageText(messageText); // 10 spaces;
message.setUserType(UserType.SELF);
message.setMessageTime(new Date().getTime());
chatMessages.add(message);
MainActivity.this.runOnUiThread(new Runnable() {
public void run() {
listAdapter.notifyDataSetChanged();
}
});
}
}, 1, TimeUnit.SECONDS);
}
private Activity getActivity()
{
return this;
}
//@Override
public void onSizeChanged(int height) {
Rect localRect = new Rect();
getActivity().getWindow().getDecorView().getWindowVisibleDisplayFrame(localRect);
WindowManager wm = (WindowManager) App.getInstance().getSystemService(Activity.WINDOW_SERVICE);
if (wm == null || wm.getDefaultDisplay() == null) {
return;
}
if (height > AndroidUtilities.dp(50) && keyboardVisible) {
keyboardHeight = height;
App.getInstance().getSharedPreferences("emoji", 0).edit().putInt("kbd_height", keyboardHeight).commit();
}
if (showingEmoji) {
int newHeight = 0;
newHeight = keyboardHeight;
if (windowLayoutParams.width != AndroidUtilities.displaySize.x || windowLayoutParams.height != newHeight) {
windowLayoutParams.width = AndroidUtilities.displaySize.x;
windowLayoutParams.height = newHeight;
wm.updateViewLayout(emojiView, windowLayoutParams);
if (!keyboardVisible) {
sizeNotifierRelativeLayout.post(new Runnable() {
@Override
public void run() {
if (sizeNotifierRelativeLayout != null) {
sizeNotifierRelativeLayout.setPadding(0, 0, 0, windowLayoutParams.height);
sizeNotifierRelativeLayout.requestLayout();
}
}
});
}
}
}
boolean oldValue = keyboardVisible;
keyboardVisible = height > 0;
if (keyboardVisible && sizeNotifierRelativeLayout.getPaddingBottom() > 0) {
showEmojiPopup(false);
} else if (!keyboardVisible && keyboardVisible != oldValue && showingEmoji) {
showEmojiPopup(false);
}
}
/**
* Show or hide the emoji popup
*
* @param show
*/
private void showEmojiPopup(boolean show) {
showingEmoji = show;
if (show) {
if (emojiView == null) {
if (getActivity() == null) {
return;
}
emojiView = new EmojiView(getActivity());
emojiView.setListener(new EmojiView.Listener() {
public void onBackspace() {
chatEditText1.dispatchKeyEvent(new KeyEvent(0, 67));
}
public void onEmojiSelected(String symbol) {
int i = chatEditText1.getSelectionEnd();
if (i < 0) {
i = 0;
}
try {
CharSequence localCharSequence = Emoji.replaceEmoji(symbol, chatEditText1.getPaint().getFontMetricsInt(), AndroidUtilities.dp(20));
chatEditText1.setText(chatEditText1.getText().insert(i, localCharSequence));
int j = i + localCharSequence.length();
chatEditText1.setSelection(j, j);
} catch (Exception e) {
Log.e(Constants.TAG, "Error showing emoji");
}
}
});
windowLayoutParams = new WindowManager.LayoutParams();
windowLayoutParams.gravity = Gravity.BOTTOM | Gravity.LEFT;
if (Build.VERSION.SDK_INT >= 21) {
windowLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
} else {
windowLayoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_PANEL;
windowLayoutParams.token = getActivity().getWindow().getDecorView().getWindowToken();
}
windowLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
}
final int currentHeight;
if (keyboardHeight <= 0)
keyboardHeight = App.getInstance().getSharedPreferences("emoji", 0).getInt("kbd_height", AndroidUtilities.dp(200));
currentHeight = keyboardHeight;
WindowManager wm = (WindowManager) App.getInstance().getSystemService(Activity.WINDOW_SERVICE);
windowLayoutParams.height = currentHeight;
windowLayoutParams.width = AndroidUtilities.displaySize.x;
try {
if (emojiView.getParent() != null) {
wm.removeViewImmediate(emojiView);
}
} catch (Exception e) {
Log.e(Constants.TAG, e.getMessage());
}
try {
wm.addView(emojiView, windowLayoutParams);
} catch (Exception e) {
Log.e(Constants.TAG, e.getMessage());
return;
}
if (!keyboardVisible) {
if (sizeNotifierRelativeLayout != null) {
sizeNotifierRelativeLayout.setPadding(0, 0, 0, currentHeight);
}
return;
}
}
else {
removeEmojiWindow();
if (sizeNotifierRelativeLayout != null) {
sizeNotifierRelativeLayout.post(new Runnable() {
public void run() {
if (sizeNotifierRelativeLayout != null) {
sizeNotifierRelativeLayout.setPadding(0, 0, 0, 0);
}
}
});
}
}
}
@Override
public void onDestroy() {
super.onDestroy();
NotificationCenter.getInstance().removeObserver(this, NotificationCenter.emojiDidLoaded);
}
/**
* Get the system status bar height
* @return
*/
public int getStatusBarHeight() {
int result = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = getResources().getDimensionPixelSize(resourceId);
}
return result;
}
@Override
protected void onPause() {
super.onPause();
hideEmojiPopup();
}
/**
* Remove emoji window
*/
private void removeEmojiWindow() {
if (emojiView == null) {
return;
}
try {
if (emojiView.getParent() != null) {
WindowManager wm = (WindowManager) App.getInstance().getSystemService(Context.WINDOW_SERVICE);
wm.removeViewImmediate(emojiView);
}
} catch (Exception e) {
Log.e(Constants.TAG, e.getMessage());
}
}
/**
* Hides the emoji popup
*/
public void hideEmojiPopup() {
if (showingEmoji) {
showEmojiPopup(false);
}
}
/**
* Check if the emoji popup is showing
*
* @return
*/
public boolean isEmojiPopupShowing() {
return showingEmoji;
}
/**
* Updates emoji views when they are complete loading
*
* @param id
* @param args
*/
@Override
public void didReceivedNotification(int id, Object... args) {
if (id == NotificationCenter.emojiDidLoaded) {
if (emojiView != null) {
emojiView.invalidateViews();
}
if (chatListView != null) {
chatListView.invalidateViews();
}
}
}
}
我正在尝试制作一个聊天应用程序。我在这个类和其他 3 个类中遇到资源文件错误。我的 res 目录中没有错误。当我清理项目时出现此错误
错误:在 x=14 的顶部边框上发现了意外的光学边界(红色像素)。错误:。错误:java.util.concurrent.ExecutionException:com.android.tools.aapt2.Aapt2Exception:AAPT2 错误:检查日志以获取详细信息错误:任务':app:mergeDebugResources'的执行失败。> 错误:java.util.concurrent.ExecutionException : com.android.tools.aapt2.Aapt2Exception: AAPT2 错误:查看日志了解详情