我正在android.Keyboard中创建一个自定义键盘,其中也会有自定义表情符号。我为每个页面创建了我想要的所有表情符号的单独xmls,现在我想在用户点击笑脸按钮时在弹出窗口中为它们充气。 现在我正在将xml直接膨胀到弹出窗口中。它包含的图像都是png格式。现在如果用户点击任何图像,它应该输入用户输入的编辑文本。 我如何能够在编辑文本中设置点击的图像?
这是我的键盘服务类代码
public class SimpleIME extends InputMethodService
implements KeyboardView.OnKeyboardActionListener,EmoticonsGridAdapter.KeyClickListener {
private KeyboardView kv;
private Keyboard keyboard;
private View popUpView;
private boolean caps = false;
LayoutInflater inflater;
private PopupWindow popupWindow;
private int keyboardHeight=500;
private static final int NO_OF_EMOTICONS = 100;
private Bitmap[] emoticons;
Button abc;
Context context;
@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();
context=this;
inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
popUpView = inflater.inflate(R.layout.emoticons_popup, null);
abc= (Button) popUpView.findViewById(R.id.btn_abc);
abc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popupWindow.dismiss();
}
});
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 -10 :
//View view = inflater.inflate( R.layout.emoticons_popup, null );
enablePopUpView();
Log.d("smiley", "smiley pressed");
Toast.makeText(this, "Smiley", Toast.LENGTH_LONG).show();
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() {
}
private void enablePopUpView() {
ViewPager pager = (ViewPager) popUpView.findViewById(R.id.map_pager);
pager.setOffscreenPageLimit(3);
pager.setBackgroundColor(Color.WHITE);
ArrayList<String> paths = new ArrayList<String>();
ArrayList<String>country=new ArrayList<>();
String[] countr=context.getResources().getStringArray(R.array.countries);
for(int i=0;i<countr.length;i++)
{
country.add(countr[i]);
}
for (short i = 1; i < NO_OF_EMOTICONS; i++) {
paths.add(i + ".png");
}
// Creating a pop window for emoticons keyboard
popupWindow = new PopupWindow(popUpView, ViewGroup.LayoutParams.MATCH_PARENT,
(int) keyboardHeight, false);
popupWindow.showAtLocation(kv, Gravity.VERTICAL_GRAVITY_MASK, 0, 0);
/*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 PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
// emoticonsCover.setVisibility(LinearLayout.GONE);
}
});
}
@Override
public void keyClickedIndex(String index) {
}
/**
* Reading all emoticons in local cache
*/
}