0

EditText用户点击EditText. 我知道InputMethodService和 SoftKeyboard 示例,但我不能以这种方式使用它,因为我的键盘应该只适用于此EditText.

此外,应该有一个上下文菜单,但这不是这个问题的一部分(我认为)。

我已经阅读了大量代码片段,但在许多情况下,它们包含不再可用的方法(getViewInflate()即'我是关于 Android 的新手)。

在大多数尝试中,当我点击EditText

java.lang.IllegalArgumentException: width and height must be > 0

后面是一个不包含我的任何类的堆栈跟踪。正如您在下面的代码中看到的那样,所有尺寸都已设置。

您在下面看到的是代码的当前状态(我删除了一些代码,希望它仍然有意义)。我还尝试使用handler.post()主线程中的内容,使用注释的内容而不是handler.post()...

下面没有尝试将 aRelativeLayout与 the EditTextand the KeyboardViewin one layout-XML 一起使用。在创建布局时有一个不同的例外,例如“无效类型 0x12”或其他内容。

它只是不起作用,或者我只是不知道该怎么做。谁能指导我完成这个?如果缺少某些东西,请告诉我。

主.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
  <EditText
    android:id="@+id/field_input"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:inputType="textMultiLine|textImeMultiLine"
    android:typeface="monospace"
    android:gravity="top|left"
    android:maxLength="255"
    />
</LinearLayout>

键盘.xml:

<?xml version="1.0" encoding="utf-8"?>
<com.messenger.keyboard.LatinKeyboardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/keyboard"
    android:layout_alignParentBottom="true"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />

拉丁键盘视图.java:

import android.inputmethodservice.KeyboardView;

public class LatinKeyboardView extends KeyboardView {
    :
}

EditorActivity.java

import android.app.Activity;

public class EditorActivity extends Activity {
    private View keyboardLayout;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        final EditText inputField;

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        keyboardLayout = (View)getLayoutInflater().inflate(R.layout.keyboard, null, false);
        inputField = (EditText)findViewById(R.id.field_input);
        registerForContextMenu(inputField);

        inputField.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Handler handler = new Handler(Looper.getMainLooper());

                    handler.post(new Runnable() {
                            @Override
                            public void run() {
                                LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                                //PopupWindow pw = new PopupWindow(inflater.inflate(R.layout.input, null, false), 100, 100, true);
                                PopupWindow pw = new PopupWindow(keyboardLayout, 100, 100, true);

                                pw.showAtLocation(findViewById(R.id.field_input), Gravity.CENTER, 0, 0);
                            }
                        });

/*
                    if (keyboardLayout.getVisibility() == View.GONE) {
                        // Show Media Player
                        TranslateAnimation mAnimUp = 
                            new TranslateAnimation(
                                    Animation.RELATIVE_TO_SELF, 0,
                                    Animation.RELATIVE_TO_SELF, 0,
                                    Animation.RELATIVE_TO_SELF, -keyboardLayout.getHeight(),
                                    Animation.RELATIVE_TO_SELF, 0);

                        mAnimUp.setStartOffset(500);
                        mAnimUp.setDuration(500);

                        keyboardLayout.setVisibility(View.VISIBLE);
                        keyboardLayout.setAnimation(mAnimUp);
                    }
*/
                }
            });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        :
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        :
    }

    @Override
    public boolean onOptionsItemSelected(final MenuItem item) {
        :
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
        :
    }
}
4

2 回答 2

3

经过几个小时的“研究和尝试”,我终于明白了我的错误,这似乎是“sjngm”的错误。为了呈现虚拟键盘,您必须

  1. 通过扩展布局 xml 文件或在行中声明您的视图来声明视图KeyboardView(就像您为另一个所做的那样View)。
  2. 这里忘记了什么:检索您的 KeyboardViewfindViewById()并调用它: keyboardViewInstance.setKeyboard(new Keyboard(...) );

而已。您将能够在屏幕上看到您的键盘视图!当然,您需要创建自己的 Keyboard 类,或者将现有的类与定义键盘键的 xml 资源文件一起使用(res/xml/keyboard.xml)

于 2012-01-13T23:13:45.017 回答
0

我目前正在重新发明我的方法,因为我认为我没有打破InputMethodService足以让它在没有自己的情况下工作。换句话说,我扔掉了示例并从头开始让布局正常工作(现在是一个布局而不是两个),然后添加示例中的代码以正确处理输入。

经过进一步研究,我发现了一个关于App-specific soft-keyboard的非常有用的问题。如果你遇到我的情况,看看那里。

于 2010-11-03T07:18:27.367 回答