gpt4 book ai didi

android - 带有图像的 Android 中的自定义表情符号键盘

转载 作者:行者123 更新时间:2023-11-29 00:05:38 24 4
gpt4 key购买 nike

我想在 android 中创建自己的表情符号键盘。用户应该能够选择此键盘作为其 Android 手机的输入法。

我尝试创建它,并且能够在我的应用程序中使用它,但我不知道如何将其作为输入法,以便该键盘可供我手机中的所有其他应用程序使用。我在某处读到,我必须为此创建一个服务,以便它与输入服务绑定(bind)。除此之外,我无法理解其他内容。

这是我做的。虽然和我想做的不一样,但是刚开始,不知道如何进行。

public class MainActivity extends FragmentActivity implements EmoticonsGridAdapter.KeyClickListener {

private static final int NO_OF_EMOTICONS = 100;

private ListView chatList;
private View popUpView;
private ArrayList<Spanned> chats;
private ChatListAdapter mAdapter;

private LinearLayout emoticonsCover;
private PopupWindow popupWindow;

private int keyboardHeight;
private EditText content;

private LinearLayout parentLayout;

private boolean isKeyBoardVisible;

private Bitmap[] emoticons;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

chatList = (ListView) findViewById(R.id.chat_list);

parentLayout = (LinearLayout) findViewById(R.id.list_parent);

emoticonsCover = (LinearLayout) findViewById(R.id.footer_for_emoticons);

popUpView = getLayoutInflater().inflate(R.layout.emoticons_popup, null);

// Setting adapter for chat list
chats = new ArrayList<Spanned>();
mAdapter = new ChatListAdapter(getApplicationContext(), chats);
chatList.setAdapter(mAdapter);
chatList.setOnTouchListener(new OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
if (popupWindow.isShowing())
popupWindow.dismiss();
return false;
}
});

// Defining default height of keyboard which is equal to 230 dip
final float popUpheight = getResources().getDimension(
R.dimen.keyboard_height);
changeKeyboardHeight((int) popUpheight);

// Showing and Dismissing pop up on clicking emoticons button
ImageView emoticonsButton = (ImageView) findViewById(R.id.emoticons_button);
emoticonsButton.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

if (!popupWindow.isShowing()) {

popupWindow.setHeight((int) (keyboardHeight));

if (isKeyBoardVisible) {
emoticonsCover.setVisibility(LinearLayout.GONE);
} else {
emoticonsCover.setVisibility(LinearLayout.VISIBLE);
}
popupWindow.showAtLocation(parentLayout, Gravity.BOTTOM, 0, 0);

} else {
popupWindow.dismiss();
}

}
});

readEmoticons();
enablePopUpView();
checkKeyboardHeight(parentLayout);
enableFooterView();

}

/**
* Reading all emoticons in local cache
*/
private void readEmoticons () {

emoticons = new Bitmap[NO_OF_EMOTICONS];
for (short i = 0; i < NO_OF_EMOTICONS; i++) {
emoticons[i] = getImage((i+1) + ".png");
}

}

/**
* Enabling all content in footer i.e. post window
*/
private void enableFooterView() {

content = (EditText) findViewById(R.id.chat_content);
content.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

if (popupWindow.isShowing()) {

popupWindow.dismiss();

}

}
});
final Button postButton = (Button) findViewById(R.id.post_button);

postButton.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

if (content.getText().toString().length() > 0) {

Spanned sp = content.getText();
chats.add(sp);
content.setText("");
mAdapter.notifyDataSetChanged();

}

}
});
}

/**
* Overriding onKeyDown for dismissing keyboard on key down
*/
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (popupWindow.isShowing()) {
popupWindow.dismiss();
return false;
} else {
return super.onKeyDown(keyCode, event);
}
}

/**
* Checking keyboard height and keyboard visibility
*/
int previousHeightDiffrence = 0;
private void checkKeyboardHeight(final View parentLayout) {

parentLayout.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {

@Override
public void onGlobalLayout() {

Rect r = new Rect();
parentLayout.getWindowVisibleDisplayFrame(r);

int screenHeight = parentLayout.getRootView()
.getHeight();
int heightDifference = screenHeight - (r.bottom);

if (previousHeightDiffrence - heightDifference > 50) {
popupWindow.dismiss();
}

previousHeightDiffrence = heightDifference;
if (heightDifference > 100) {

isKeyBoardVisible = true;
changeKeyboardHeight(heightDifference);

} else {

isKeyBoardVisible = false;

}

}
});

}

/**
* change height of emoticons keyboard according to height of actual
* keyboard
*
* @param height
* minimum height by which we can make sure actual keyboard is
* open or not
*/
private void changeKeyboardHeight(int height) {

if (height > 100) {
keyboardHeight = height;
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, keyboardHeight);
emoticonsCover.setLayoutParams(params);
}

}

/**
* Defining all components of emoticons keyboard
*/
private void enablePopUpView() {

ViewPager pager = (ViewPager) popUpView.findViewById(R.id.emoticons_pager);
pager.setOffscreenPageLimit(3);
pager.setBackgroundColor(Color.WHITE);
ArrayList<String> paths = new ArrayList<String>();

for (short i = 1; i <= NO_OF_EMOTICONS; i++) {
paths.add(i + ".png");
}

EmoticonsPagerAdapter adapter = new EmoticonsPagerAdapter(MainActivity.this, paths, this);
pager.setAdapter(adapter);

// Creating a pop window for emoticons keyboard
popupWindow = new PopupWindow(popUpView, LayoutParams.MATCH_PARENT,
(int) keyboardHeight, false);

/*TextView backSpace = (TextView) popUpView.findViewById(R.id.back);
backSpace.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
KeyEvent event = new KeyEvent(0, 0, 0, KeyEvent.KEYCODE_DEL, 0, 0, 0, 0, KeyEvent.KEYCODE_ENDCALL);
content.dispatchKeyEvent(event);
}
});*/

popupWindow.setOnDismissListener(new OnDismissListener() {

@Override
public void onDismiss() {
emoticonsCover.setVisibility(LinearLayout.GONE);
}
});
}

/**
* For loading smileys from assets
*/
private Bitmap getImage(String path) {
AssetManager mngr = getAssets();
InputStream in = null;
try {
in = mngr.open("emoticons/" + path);
} catch (Exception e) {
e.printStackTrace();
}

Bitmap temp = BitmapFactory.decodeStream(in, null, null);
return temp;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

return true;
}

@Override
public void keyClickedIndex(final String index) {

ImageGetter imageGetter = new ImageGetter() {
public Drawable getDrawable(String source) {
StringTokenizer st = new StringTokenizer(index, ".");
Drawable d = new BitmapDrawable(getResources(),emoticons[Integer.parseInt(st.nextToken()) - 1]);
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
return d;
}
};

Spanned cs = Html.fromHtml("<img src ='"+ index +"'/>", imageGetter, null);

int cursorPosition = content.getSelectionStart();
content.getText().insert(cursorPosition, cs);

}

编辑:这是我实现的自定义键盘的代码,但我无法找到如何向该键盘添加表情符号。

  public class SimpleIME extends InputMethodService
implements KeyboardView.OnKeyboardActionListener {

private KeyboardView kv;
private Keyboard keyboard;
private View popUpView;
private boolean caps = false;
@Override
public View onCreateInputView() {
kv = (KeyboardView)getLayoutInflater().inflate(R.layout.keyboard, null);
keyboard = new Keyboard(this, R.xml.qwerty);
kv.setKeyboard(keyboard);
kv.setOnKeyboardActionListener(this);
kv.invalidateAllKeys();
popUpView = getLayoutInflater().inflate(R.layout.emoticons_popup, null);
return kv;
}

@Override
public void onKey(int primaryCode, int[] keyCodes) {
InputConnection ic = getCurrentInputConnection();
playClick(primaryCode);
switch(primaryCode){
case Keyboard.KEYCODE_DELETE :
ic.deleteSurroundingText(1, 0);
break;
case Keyboard.KEYCODE_SHIFT:
caps = !caps;
keyboard.setShifted(caps);
kv.invalidateAllKeys();
break;
case Keyboard.KEYCODE_DONE:
ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER));
break;
case -80 :
Log.d("smiley", "smiley pressed");

break;
default:
char code = (char)primaryCode;
if(Character.isLetter(code) && caps){
code = Character.toUpperCase(code);
}
ic.commitText(String.valueOf(code),1);
}
}

private void playClick(int keyCode){
AudioManager am = (AudioManager)getSystemService(AUDIO_SERVICE);
switch(keyCode){
case 32:
am.playSoundEffect(AudioManager.FX_KEYPRESS_SPACEBAR);
break;
case Keyboard.KEYCODE_DONE:
case 10:
am.playSoundEffect(AudioManager.FX_KEYPRESS_RETURN);
break;
case Keyboard.KEYCODE_DELETE:
am.playSoundEffect(AudioManager.FX_KEYPRESS_DELETE);
break;
default: am.playSoundEffect(AudioManager.FX_KEYPRESS_STANDARD);
}
}


@Override
public void onPress(int primaryCode) {
}

@Override
public void onRelease(int primaryCode) {
}

@Override
public void onText(CharSequence text) {
}

@Override
public void swipeDown() {
}

@Override
public void swipeLeft() {
}

@Override
public void swipeRight() {
}

@Override
public void swipeUp() {
}

}

编辑:我们可以从图像列表中复制图像并将其粘贴到键盘打开的位置吗??

最佳答案

我发现表情符号键盘的最佳实现是 sliding emoji-Keyboard。这是一个非常好的实现,可能有一些冗余代码,但仍然非常有助于理解如何实现不适合普通“按钮到文本”键盘的键盘。

更新

好的,我现在已经能够成功整合 sliding emoji-keyboard 进入我自己的项目 8Vim在对这两个项目进行大量重构之后。

基本上,您为表情符号键盘所做的一切就是创建键盘大小的 View ,然后使用与表情符号对应的 PNG 文件填充该 View 。每个图像都像一个按钮,并将适当的表情符号传送到 inputConnection。

更新 2

我已经扩展了 sliding emoji-keyboard并创建了一个更清晰的版本,应该更容易理解。看看我的emoji-keyboard

关于android - 带有图像的 Android 中的自定义表情符号键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33998362/

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