【发布时间】:2016-05-29 04:50:54
【问题描述】:
我正在创建一个即时聊天应用程序。我正在使用库编译com.rockerhieu.emojicon:library:1.3.3 在我的应用程序中添加表情符号。
在屏幕截图中,我们有一个EmojiEditText,其左侧可绘制为表情符号图标,右侧可绘制为发送图标。单击表情符号图标时,将显示表情符号面板,如截图所示。通过表情符号面板选择的表情符号是显示在edittext中。单击emojiedittext软键盘时显示。我的问题是当我单击表情符号图标时,总是从头开始输入新的表情符号。它没有插入其实际位置。这意味着假设我正在选择一些表情符号,然后用softkeyboard写一些文字。之后,如果我选择表情符号,它会被添加到editext的最左边。请帮我解决这个问题。
activity_single_chat.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_single_chat"
android:orientation="vertical"
>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbarSingleChat"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#09436B"
android:elevation="6dp"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
<ListView
android:id="@+id/lv_message"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".90"
android:divider="@null"
android:stackFromBottom="true"
android:transcriptMode="alwaysScroll"></ListView>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.rockerhieu.emojicon.EmojiconEditText
android:id="@+id/edtMessage"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="bottom"
android:layout_weight=".10"
android:background="#FFFFFF"
android:drawableLeft="@drawable/emoticons"
android:drawablePadding="@dimen/padding10"
android:drawableRight="@drawable/send"
android:hint="Type your message ..."
android:paddingBottom="@dimen/padding10"
android:paddingLeft="@dimen/padding10"
android:paddingRight="@dimen/padding10"
android:paddingTop="@dimen/padding10" />
<FrameLayout
android:id="@+id/emojicons"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".00"
android:visibility="gone" />
</LinearLayout>
</ScrollView>
</LinearLayout>
初始化 EmojiconEditText
EmojiconEditText edMessage = (EmojiconEditText) findViewById(R.id.edtMessage);
点击 EmojiconEditText
// On clicking the edit text Emoji panel will be hidden
edMessage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
hideEmoji = true;
hideEmojiPopUp(hideEmoji);
showKeyboard(edMessage);
}
});
hideEmojiPopup
// Hiding the FrameLayout containing the list of Emoticons
public void hideEmojiPopUp(boolean hideEmoji) {
FrameLayout frameLayout = (FrameLayout) findViewById(R.id.emojicons);
frameLayout.setVisibility(View.GONE);
}
显示键盘
//Show the soft keyboard
public void showKeyboard(EditText editText) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
hideEmojiPopUp(true);
//setHeightOfEmojiEditText();
}
触摸 EmojiconEditText
//Handling click event on drawableLeft and drawableRight inside EmojiconEditText
edMessage.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
final int DRAWABLE_LEFT = 0;
final int DRAWABLE_TOP = 1;
final int DRAWABLE_RIGHT = 2;
final int DRAWABLE_BOTTOM = 3;
if (event.getAction() == MotionEvent.ACTION_UP) {
//Handling drawableRight
int leftEdgeOfRightDrawable = edMessage.getRight() - edMessage.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width();
//EditText has padding here.So adjust left edge
leftEdgeOfRightDrawable -= getResources().getDimension(R.dimen.padding10); //Here we have set the padding of 10 from left and right in edittext
if (event.getRawX() >= leftEdgeOfRightDrawable) {
// your action here
Toast.makeText(getApplicationContext(), "Clicked", Toast.LENGTH_SHORT).show();
message = edMessage.getText().toString().trim();
// Spannable s = getSmiledText(getApplicationContext(),message);
// Log.e("Spannable", String.valueOf(s));
// Encoding emoji into unicode characters
String toServer = StringEscapeUtils.escapeJava(message);
//To find the current time in timestamp format
Date d = new Date();
final long time = d.getTime();
Log.e("Time", String.valueOf(time));
Log.e("To Server", toServer);
Log.e("Sending", "Sending data-----" + toServer);
if (!message.equals("")) {
edMessage.setText(" ");
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("room_id", md5StringRoomID);
jsonObject.put("user", loggedInUpper);
jsonObject.put("id", friendID);
jsonObject.put("message", toServer);
jsonObject.put("date", time);
jsonObject.put("status", "sent");
} catch (JSONException e) {
e.printStackTrace();
}
// isSelf = true; // Boolean isSelf is set to be true as sender of the message is logged in user i.e. you
//attemptToSend(loggedInUpper, message, isSelf);
mSocket.emit("send", jsonObject); // owner i.e LoggedIn user is sending the message
/* msg = new Bean_Message();
msg.setMessageStatus(Status.SENT);
listBeanMessages.add(msg);
adapter.notifyDataSetChanged();*/
} else {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "Please enter some text", Toast.LENGTH_LONG).show();
}
});
}
return true;
}
//Handling drawable Left
if (event.getRawX() <= (edMessage.getCompoundDrawables()[DRAWABLE_LEFT].getBounds().width())) {
// your action here
Toast.makeText(getApplicationContext(), "Clicked", Toast.LENGTH_SHORT).show();
hideKeyboard(); // hiding the keyboard
showEmojiPopUp(!showEmoji);
return true;
}
}
return false;
}
}
隐藏键盘:
// Hiding the keyboard
public void hideKeyboard() {
InputMethodManager inputManager = (InputMethodManager) getSystemService(this.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
显示表情符号弹出窗口
public void showEmojiPopUp(boolean showEmoji) {
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
deviceHeight = size.y;
Log.e("Device Height", String.valueOf(deviceHeight));
frameLayout = (FrameLayout) findViewById(R.id.emojicons);
frameLayout.getLayoutParams().height = (int) (deviceHeight / 2.5); // Setting the height of FrameLayout
frameLayout.requestLayout();
frameLayout.setVisibility(View.VISIBLE);
hideKeyboard();
}
用表情符号片段替换框架
// This method will set a panel of emoticons in the fragment
private void setEmojiconFragment(boolean useSystemDefault) {
// Replacing the existing frame having id emojicons with the fragment of emoticons library containing emoticons
getSupportFragmentManager().beginTransaction().replace(R.id.emojicons, EmojiconsFragment.newInstance(useSystemDefault)).commit();
}
点击退格键
@Override
public void onEmojiconBackspaceClicked(View view) {
EmojiconsFragment.backspace(edMessage);
}
点击表情符号时
@Override
public void onEmojiconClicked(Emojicon emojicon) {
EmojiconsFragment.input(edMessage, emojicon);
}
【问题讨论】:
-
我相信它在编码中,提供一段你的代码
-
@MinaFawzy :请检查我编辑的代码。
-
@MinaFawzy 你检查了我的代码吗?请帮助我,因为我找不到解决方案。