在我的代码中,除了完成之外,所有按键都能正常工作,因此请告诉我有关 DONE 键的建议。 我的代码如下:
我的mainActivity代码低于
package nl.fampennings.keyboard;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;
public class MainActivity extends Activity {
CustomKeyboard mCustomKeyboard;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mCustomKeyboard= new CustomKeyboard(this, R.id.keyboardview, R.xml.hexkbd);
mCustomKeyboard.registerEditText(R.id.edittext0);
mCustomKeyboard.registerEditText(R.id.edittext1);
mCustomKeyboard.registerEditText(R.id.edittext2);
mCustomKeyboard.registerEditText(R.id.edittext3);
mCustomKeyboard.registerEditText(R.id.edittext4);
}
@Override public void onBackPressed() {
// NOTE Trap the back key: when the CustomKeyboard is still visible hide it, only when it is invisible, finish activity
if( mCustomKeyboard.isCustomKeyboardVisible() ) mCustomKeyboard.hideCustomKeyboard(); else this.finish();
}
boolean displayMsg(){
boolean flag=true;
if(flag)
{
Toast.makeText(this, "adfdf", 100).show();
}
return true;
}
}
我的Customerkeyboard.java文件位于
之下package nl.fampennings.keyboard;
import android.app.Activity;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.inputmethodservice.KeyboardView.OnKeyboardActionListener;
import android.text.Editable;
import android.text.InputType;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
class CustomKeyboard {
private OnKeyboardActionListener mOnKeyboardActionListener = new OnKeyboardActionListener() {
public final static int enter = 13;
public final static int CodeDelete = -5; // Keyboard.KEYCODE_DELETE
public final static int CodeCancel = -3; // Keyboard.KEYCODE_CANCEL
public final static int CodePrev = 55000;
public final static int CodeAllLeft = 55001;
public final static int CodeLeft = 55002;
public final static int CodeRight = 55003;
public final static int CodeAllRight = 55004;
public final static int CodeNext = 55005;
public final static int CodeClear = 55006;
@Override public void onKey(int primaryCode, int[] keyCodes) {
View focusCurrent = mHostActivity.getWindow().getCurrentFocus();
if( focusCurrent==null || focusCurrent.getClass()!=EditText.class ) return;
EditText edittext = (EditText) focusCurrent;
Editable editable = edittext.getText();
int start = edittext.getSelectionStart();
main=new MainActivity();
// Apply the key to the edittext
if( primaryCode==CodeCancel ) {
hideCustomKeyboard();
}
else if(primaryCode==enter)
{
edittext.setText("");
main.displayMsg();
}
else if( primaryCode==CodeDelete ) {
if( editable!=null && start>0 ) editable.delete(start - 1, start);
// main.displayMsg();
} else if( primaryCode==CodeClear ) {
if( editable!=null ) editable.clear();
} else if( primaryCode==CodeLeft ) {
if( start>0 ) edittext.setSelection(start - 1);
} else if( primaryCode==CodeRight ) {
if (start < edittext.length()) edittext.setSelection(start + 1);
} else if( primaryCode==CodeAllLeft ) {
edittext.setSelection(0);
} else if( primaryCode==CodeAllRight ) {
edittext.setSelection(edittext.length());
} else if( primaryCode==CodePrev ) {
View focusNew= edittext.focusSearch(View.FOCUS_BACKWARD);
if( focusNew!=null ) focusNew.requestFocus();
} else if( primaryCode==CodeNext ) {
//main.displayMsg();
View focusNew= edittext.focusSearch(View.FOCUS_FORWARD);
if( focusNew!=null ) focusNew.requestFocus();
} else { // insert character
editable.insert(start, Character.toString((char) primaryCode));
}
}
@Override public void onPress(int arg0) {
}
@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() {
}
};
public CustomKeyboard(Activity host, int viewid, int layoutid) {
mHostActivity= host;
mKeyboardView= (KeyboardView)mHostActivity.findViewById(viewid);
mKeyboardView.setKeyboard(new Keyboard(mHostActivity, layoutid));
mKeyboardView.setPreviewEnabled(false); // NOTE Do not show the preview balloons
mKeyboardView.setOnKeyboardActionListener(mOnKeyboardActionListener);
// Hide the standard keyboard initially
mHostActivity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}
/** Returns whether the CustomKeyboard is visible. */
public boolean isCustomKeyboardVisible() {
return mKeyboardView.getVisibility() == View.VISIBLE;
}
/** Make the CustomKeyboard visible, and hide the system keyboard for view v. */
public void showCustomKeyboard( View v ) {
mKeyboardView.setVisibility(View.VISIBLE);
mKeyboardView.setEnabled(true);
if( v!=null ) ((InputMethodManager)mHostActivity.getSystemService(Activity.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(v.getWindowToken(), 0);
}
/** Make the CustomKeyboard invisible. */
public void hideCustomKeyboard() {
mKeyboardView.setVisibility(View.GONE);
mKeyboardView.setEnabled(false);
}
/**
* Register <var>EditText<var> with resource id <var>resid</var> (on the hosting activity) for using this custom keyboard.
*
* @param resid The resource id of the EditText that registers to the custom keyboard.
*/
public void registerEditText(int resid) {
// Find the EditText 'resid'
EditText edittext= (EditText)mHostActivity.findViewById(resid);
// Make the custom keyboard appear
edittext.setOnFocusChangeListener(new OnFocusChangeListener() {
// NOTE By setting the on focus listener, we can show the custom keyboard when the edit box gets focus, but also hide it when the edit box loses focus
@Override public void onFocusChange(View v, boolean hasFocus) {
if( hasFocus ) showCustomKeyboard(v); else hideCustomKeyboard();
}
});
edittext.setOnClickListener(new OnClickListener() {
// NOTE By setting the on click listener, we can show the custom keyboard again, by tapping on an edit box that already had focus (but that had the keyboard hidden).
@Override public void onClick(View v) {
showCustomKeyboard(v);
}
});
// Disable standard keyboard hard way
// NOTE There is also an easy way: 'edittext.setInputType(InputType.TYPE_NULL)' (but you will not have a cursor, and no 'edittext.setCursorVisible(true)' doesn't work )
edittext.setOnTouchListener(new OnTouchListener() {
@Override public boolean onTouch(View v, MotionEvent event) {
EditText edittext = (EditText) v;
int inType = edittext.getInputType(); // Backup the input type
edittext.setInputType(InputType.TYPE_NULL); // Disable standard keyboard
edittext.onTouchEvent(event); // Call native handler
edittext.setInputType(inType); // Restore input type
return true; // Consume touch event
}
});
edittext.setInputType(edittext.getInputType() | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
}
}
我的Xml文件位于
之下<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
android:keyWidth="12.50%p"
android:keyHeight="10%p">
<!-- android:horizontalGap="0.50%p"
android:verticalGap="0.50%p"
NOTE When we add a horizontalGap in pixels, this interferes with keyWidth in percentages adding up to 100%
NOTE When we have a horizontalGap (on Keyboard level) of 0, this make the horizontalGap (on Key level) to move from after the key to before the key... (I consider this a bug)
-->
<Row>
<Key android:codes="55" android:keyLabel="7" android:keyEdgeFlags="left" />
<Key android:codes="56" android:keyLabel="8" />
<Key android:codes="57" android:keyLabel="9" />
<Key android:codes="65" android:keyLabel="A" android:popupKeyboard="@xml/popup" android:popupCharacters="aA" android:horizontalGap="6.25%p" />
<Key android:codes="66" android:keyLabel="B" android:popupKeyboard="@xml/popup" android:popupCharacters="bB" />
<Key android:codes="13" android:keyIcon="@drawable/sym_keyboard_delete" android:isRepeatable="true" android:horizontalGap="6.25%p" />
<Key android:codes="55006" android:keyLabel="CLR" android:keyEdgeFlags="right"/>
</Row>
<Row>
<Key android:codes="52" android:keyLabel="4" android:keyEdgeFlags="left" />
<Key android:codes="53" android:keyLabel="5" />
<Key android:codes="54" android:keyLabel="6" />
<Key android:codes="67" android:keyLabel="C" android:popupKeyboard="@xml/popup" android:popupCharacters="cC" android:horizontalGap="6.25%p" />
<Key android:codes="68" android:keyLabel="D" android:popupKeyboard="@xml/popup" android:popupCharacters="dD" />
<Key android:codes="55002" android:keyIcon="@drawable/sym_keyboard_left" android:isRepeatable="true" android:horizontalGap="6.25%p" />
<Key android:codes="55003" android:keyIcon="@drawable/sym_keyboard_right" android:isRepeatable="true" android:keyEdgeFlags="right" />
</Row>
<Row>
<Key android:codes="49" android:keyLabel="1" android:keyEdgeFlags="left" />
<Key android:codes="50" android:keyLabel="2" />
<Key android:codes="51" android:keyLabel="3" />
<Key android:codes="69" android:keyLabel="E" android:popupKeyboard="@xml/popup" android:popupCharacters="eE" android:horizontalGap="6.25%p" />
NOTE We could add 'android:keyHeight="20%p"' to the 'E' key, so that it becomes twice as tall. We would then typically make the the 'done' key half the width (12.5%) but give it a double gap (25%)
<Key android:codes="70" android:keyLabel="F" android:popupKeyboard="@xml/popup" android:popupCharacters="fF" />
<Key android:codes="55001" android:keyIcon="@drawable/sym_keyboard_allleft" android:horizontalGap="6.25%p" />
<Key android:codes="55004" android:keyIcon="@drawable/sym_keyboard_allright" android:keyEdgeFlags="right" />
</Row>
<Row>
<Key android:codes="48" android:keyLabel="0" android:keyWidth="25%p" android:horizontalGap="6.25%p" android:keyEdgeFlags="left" />
<Key android:codes="-3" android:keyIcon="@drawable/sym_keyboard_done" android:keyWidth="25%p" android:horizontalGap="12.50%p" />
<Key android:codes="-5" android:keyIcon="@drawable/sym_keyboard_done" android:keyWidth="25%p" android:horizontalGap="12.50%p" />
<Key android:codes="13" android:keyLabel="DONE" android:keyWidth="25%p" android:horizontalGap="6.25%p" android:keyEdgeFlags="left" />
<Key android:codes="55000" android:keyLabel="PREV" android:horizontalGap="6.25%p" />
<Key android:codes="55005" android:keyLabel="NEXT" android:keyEdgeFlags="right" />
</Row>
</Keyboard>
我的布局XMl文件位于
之下<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText
android:id="@+id/edittext0"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:drawableRight="@drawable/hex"
android:inputType="text" />
<EditText
android:id="@+id/edittext1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/edittext0"
android:layout_centerHorizontal="true"
android:drawableRight="@drawable/txt"
android:inputType="text" />
<EditText
android:id="@+id/edittext2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/edittext1"
android:layout_centerHorizontal="true"
android:drawableRight="@drawable/txt"
android:inputType="text" />
<EditText
android:id="@+id/edittext3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/edittext2"
android:layout_centerHorizontal="true"
android:drawableRight="@drawable/hex"
android:inputType="text" />
<EditText
android:id="@+id/edittext4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/edittext3"
android:layout_centerHorizontal="true"
android:drawableRight="@drawable/hex"
android:inputType="text" />
<android.inputmethodservice.KeyboardView
android:id="@+id/keyboardview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:visibility="gone" />
</RelativeLayout>
答案 0 :(得分:1)
因为你做错了(int enter = 13)。不是这样,您可以尝试创建自己的代码,并在按下时执行任何操作。首先,在键盘xml处为“DONE”按钮创建自定义代码。我为此选择了数字-10:
<Key android:codes="-10" android:keyLabel="DONE" android:keyWidth="25%p" android:horizontalGap="6.25%p" android:keyEdgeFlags="left" />
在CustomerKeyboard类中为“enter”变量设置相同的代码:
public final static int enter = -10;
现在设置按下DONE时会发生什么:
else if(primaryCode==enter)
{
edittext.setText("");
main.displayMsg();
}
对不起我的英语,我希望你能理解。
答案 1 :(得分:0)
完成关键操作可能是
ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER));
如下所示: http://code.tutsplus.com/tutorials/create-a-custom-keyboard-on-android--cms-22615
答案 2 :(得分:0)
ACTION_UP
对我来说还不够。您还需要ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER));
ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_ENTER));
之后立即模仿实际按键:
Environment.CurrentDirectory