0

好吧,您可能會看到像詢問這樣的問題。 但是,閱讀所有這些問題/答案以及Google中幾乎所有的android inputmethod網頁,我仍然遇到麻煩。爲android創建自定義鍵盤

我最終的目標是創建一個自定義鍵盤。但是,當然,我的某些語言會有特殊的輸入方法。

但是這一次,我想要的只是在彈出鍵盤時顯示我的自定義視圖。我已經設法基於qwerty.xml文件彈出默認佈局,這是這樣的。

XML/qwerty.xml

<Keyboard xmlns:android="http://schemas.android.com/apk/res/android" 
android:keyWidth="10%p" 
android:horizontalGap="0px" 
android:verticalGap="0px" 
android:keyHeight="60dp" 
> 
<Row android:rowEdgeFlags="bottom"> 
    <Key android:codes="999" android:keyLabel="Settings" android:keyWidth="20%" android:keyEdgeFlags="left"/> 
    <Key android:codes="44" android:keyLabel="," android:keyWidth="7%p" /> 
    <Key android:codes="47" android:keyLabel="/" android:keyWidth="7%p" /> 
    <Key android:codes="32" android:keyLabel="SPACE" android:keyWidth="30%p" android:isRepeatable="true"/> 
    <Key android:codes="-5" android:keyLabel="DEL" android:keyWidth="18%p" android:isRepeatable="true"/> 
    <Key android:codes="-4" android:keyLabel="DONE" android:keyWidth="18%p" android:keyEdgeFlags="right"/> 
</Row> 

而在我的類,其延伸InputMethodService,我有這個代碼它創建輸入圖。

private KeyboardView myKeyView; 
private Keyboard keyboard; 

@Override 
public View onCreateInputView() { 
    myKeyView = (KeyboardView)getLayoutInflater().inflate(R.layout.keyboard, null); 
    keyboard = new Keyboard(this, R.xml.qwerty); 
    myKeyView.setKeyboard(keyboard); 
    myKeyView.setOnKeyboardActionListener(this); 
    return myKeyView; 
} 

當然,因爲我在將我的自定義視圖應用到源代碼時遇到問題。 我只是在另一邊做了一些努力。當XML設置鍵,

<Key android:codes="999" android:keyLabel="Settings" android:keyWidth="20%" android:keyEdgeFlags="left"/> 

它會調用SettingsActivity

@Override 
public void onKey(int primaryCode, int[] keyCodes) { 
    if(primaryCode == 999) { 
     openSettings(); 
    } 
} 

public void openSettings() 
{ 
    Intent intent = new Intent(this, WRKeySettings.class); 
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    startActivity(intent); 
} 

它可以很好地直到這裏,但我在這個觀點的問題。

我認爲從閱讀android develpers和一些文章是,我可以擴展KeyboardView使我的自定義視圖,並在此方法中,我可能能夠用onDraw()以某種方式繪製鍵。但是我這麼做很麻煩。

任何建議都會很高興。謝謝。

+0

爲了清楚地理解問題,您希望以編程方式創建另一個佈局?你想改變現有的嗎?你想在不同的應用程序上重複使用相同的「鍵盤」嗎?你可以「重新繪製」按鍵,但是它們可以具有相同的功能,或者可以將視圖添加到佈局中,並「重做」窗口以及...嘗試儘可能詳細地描述你想要完成的任務 – Bonatti

回答

1

要定製鍵盤,您需要在xml文件夾和佈局文件夾中的鍵盤上修改「qwerty」。我將展示一些例子:

這正好佈局文件夾

<?xml version="1.0" encoding="UTF-8"?> 
<com.example.keyboard.MyKeyboardView 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/keyboard" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:layout_alignParentBottom="true" 
android:keyPreviewLayout="@layout/preview" 
android:keyBackground="@drawable/key_selector" 
android:shadowRadius="0.0" 
android:keyTextColor="#000000" 
/> 

這那張XML文件夾:

<?xml version="1.0" encoding="utf-8"?> 
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android" 
android:keyWidth="10%p" 
android:keyHeight="8%p"> 

<Row android:verticalGap="1%p" android:horizontalGap="0.5%p" android:keyWidth="9.6%p"> 
    <Key android:codes="113" android:keyLabel="q" /> 
    <Key android:codes="119" android:keyLabel="w" /> 
    <Key android:codes="101" android:keyLabel="e" /> 
    <Key android:codes="114" android:keyLabel="r" /> 
    <Key android:codes="116" android:keyLabel="t" /> 
    <Key android:codes="121" android:keyLabel="y" /> 
    <Key android:codes="117" android:keyLabel="u" /> 
    <Key android:codes="105" android:keyLabel="i" /> 
    <Key android:codes="111" android:keyLabel="o" /> 
    <Key android:codes="112" android:keyLabel="p" /> 
</Row> 
<Row android:verticalGap="1%p" android:horizontalGap="0.5%p" android:keyWidth="9.6%p"> 
    <Key android:codes="97" android:keyLabel="a" android:keyEdgeFlags="left" android:horizontalGap="5%p" /> 
    <Key android:codes="115" android:keyLabel="s" /> 
    <Key android:codes="100" android:keyLabel="d" /> 
    <Key android:codes="102" android:keyLabel="f" /> 
    <Key android:codes="103" android:keyLabel="g" /> 
    <Key android:codes="104" android:keyLabel="h" /> 
    <Key android:codes="106" android:keyLabel="j" /> 
    <Key android:codes="107" android:keyLabel="k" /> 
    <Key android:codes="108" android:keyLabel="l" /> 
</Row> 
<Row android:verticalGap="1%p" android:horizontalGap="0.5%p" android:keyWidth="9.6%p"> 
    <Key android:codes="3"  android:keyIcon="@drawable/keyboard_shift_off_normal" 
     android:keyWidth="13.7%p"/> 
    <Key android:codes="122" android:keyLabel="z" android:horizontalGap="1%p"/> 
    <Key android:codes="120" android:keyLabel="x" /> 
    <Key android:codes="99"  android:keyLabel="c" /> 
    <Key android:codes="118" android:keyLabel="v" /> 
    <Key android:codes="98"  android:keyLabel="b" /> 
    <Key android:codes="110" android:keyLabel="n" /> 
    <Key android:codes="109" android:keyLabel="m" /> 
    <Key android:codes="-5"  android:keyIcon="@drawable/sym_keyboard_delete_dim" 
     android:keyWidth="13.7%p" 
     android:horizontalGap="1%p"/> 
</Row> 
<Row android:verticalGap="1%p" android:horizontalGap="0.5%p" android:keyWidth="9.6%p"> 
    <Key android:codes="-16" android:keyIcon="@drawable/keyboard_symbol" 
     android:keyWidth="18.7%p"/> 
    <Key android:codes="44"  android:keyLabel="," android:horizontalGap="1%p"/> 
    <Key android:codes="32"  android:keyIcon="@drawable/sym_keyboard_feedback_space" android:keyWidth="40%p"/> 
    <Key android:codes="46"  android:keyLabel="."/> 
    <Key android:codes="-3"  android:keyIcon="@drawable/keyboard_go" 
     android:keyWidth="18.5%p" android:horizontalGap="1%p"/> 
</Row> 
</Keyboard> 

就像你說的,可以延長從鍵盤類佈局,但更多的是自定義鍵盤事件,如onTouch,onLongKeyPress,

+0

不幸的是,這不是我想要的具體答案。我會試着找到一種方法來讓這件事情起作用,並寫出一個具體的指導,因爲目前沒有這方面的內容。 – luopleming