【问题标题】:Android: KeyboardView doesn't appear above EditTextAndroid:KeyboardView 没有出现在 EditText 上方
【发布时间】:2011-05-01 19:59:14
【问题描述】:

我正在尝试使用EditText 以及当用户点击EditText 时在屏幕底部显示键盘的可能性。我知道 InputMethodService 和 SoftKeyboard 示例,但我不能以这种方式使用它,因为我的键盘应该只对 EditText 可用。

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

我已经阅读了大量代码片段,但在许多情况下,它们包含不再可用的方法(即getViewInflate()),或者是在我不理解或无法翻译成的上下文中编写的我的代码(请注意,我是 Android 新手)。

在大多数尝试中,当我点击 EditText 时,我都会因为这个异常而失败:

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

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

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

下面没有尝试在一个布局 XML 中使用 RelativeLayoutEditTextKeyboardView。在创建布局时出现了一个不同的异常,例如“无效类型 0x12”或其他异常。

它只是不起作用,或者我只是不知道该怎么做。谁能指导我完成这个?如有遗漏,请告诉我。

main.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>

keyboard.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"
    />

LatinKeyboardView.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) {
        :
    }
}

【问题讨论】:

    标签: android popupwindow


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

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

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

      【讨论】:

      • 由于这个项目不再是我的项目,我无法对此进行测试或对您的方法发表任何评论。我从您的回答中猜测它有效,所以感谢您的更新:)
      猜你喜欢
      • 1970-01-01
      • 2019-04-18
      • 1970-01-01
      • 2012-07-30
      • 2014-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多