Java 合并自定义键盘onClick和ToggleButton
现在我有一个Java 合并自定义键盘onClick和ToggleButton,java,Java,现在我有一个自定义键盘类(视图),其中有自定义汉字 我也有几个切换按钮。我想实现这样的功能,如果我要单击中文键盘的中文选项,它会将我的切换按钮切换到“开”。 但我不确定如何将它们联系在一起 //CustomKeyboard.java/class - This is the java file that is use for my CustomKeyboardView. This works when onClick on the Keyboard itself, it'll change the
自定义键盘类(视图)
,其中有自定义汉字
我也有几个切换按钮。我想实现这样的功能,如果我要单击中文键盘的中文选项,它会将我的切换按钮切换到“开”。
但我不确定如何将它们联系在一起
//CustomKeyboard.java/class - This is the java file that is use for my CustomKeyboardView. This works when onClick on the Keyboard itself, it'll change the keyboard view etc.
private OnKeyboardActionListener noncapitalKeyboard = new OnKeyboardActionListener() {
public final static int CodeNext = 55005;
public final static int CodeClear = 55006;
public final static int CodeCapital = 55007;
public final static int CodeSymbol = 55008;
public final static int CodeABC = 55009;
public final static int CodeChinese1 = 55010;
@Override public void onKey(int primaryCode, int[] keyCodes) {
// NOTE We can say '<Key android:codes="49,50" ... >' in the xml file; all codes come in keyCodes, the first in this list in primaryCode
// Get the EditText and its Editable
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();
// Apply the key to the edittext
if(primaryCode == CodeCapital){
mKeyboardView= (KeyboardView)mHostActivity.findViewById(R.id.keyboardview);
mKeyboardView.setKeyboard(new Keyboard(mHostActivity, R.xml.capitalkeyboard));
mKeyboardView.setPreviewEnabled(false); // NOTE Do not show the preview balloons
mKeyboardView.setOnKeyboardActionListener(capitalKeyboard);
}else if(primaryCode == CodeSymbol){
mKeyboardView= (KeyboardView)mHostActivity.findViewById(R.id.keyboardview);
mKeyboardView.setKeyboard(new Keyboard(mHostActivity, R.xml.symbolkeyboard));
mKeyboardView.setPreviewEnabled(false); // NOTE Do not show the preview balloons
mKeyboardView.setOnKeyboardActionListener(symbolKeyboard);
}else if(primaryCode == CodeChinese1){
mKeyboardView= (KeyboardView)mHostActivity.findViewById(R.id.keyboardview);
mKeyboardView.setKeyboard(new Keyboard(mHostActivity, R.xml.chinese1keyboard));
mKeyboardView.setPreviewEnabled(false); // NOTE Do not show the preview balloons
mKeyboardView.setOnKeyboardActionListener(chinese1Keyboard);
}
我试图在我的主java文件上实现OnKeyboardActionListener,但没有成功,因为它没有引用我的CustomKeyboard。我需要创建接口吗?这是java而不是javascript。@MichaelJosephAubry抱歉。我错误地点击了标签。不用担心,我只是想尽快指出它,加上我正在搜索回答javascript问题,这个弹出了lol。我参加了java课程,但我不是专家哈哈,祝你好运!谢谢@迈克尔约瑟福布里
if(primaryCode == CodeChinese1){
toggleButton1.setChecked(true);
}