我正在創建一個即時聊天應用程序。我正在使用庫編譯com.rockerhieu.emojicon:library:1.3.3
在我的應用程序中添加表情符號。EmojiconEditText從一開始就添加表情符號
在截圖中,我們有一個EmojiEditText
其左可繪是表情符號圖標,然後右鍵可繪製是發送icon.ON點擊表情符號圖標表情的面板顯示爲我們在選擇的screenshot.The的表情符號看通過表情符號面板顯示在edittext中。在cliking 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);
}
showKeyboard
//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你檢查了我的代碼嗎?請幫助我,因爲我無法找到解決方案。 –