Android:KeyboardView没有';不要出现在编辑文本上方

Android:KeyboardView没有';不要出现在编辑文本上方,android,popupwindow,Android,Popupwindow,我正在尝试一个EditText,并且当用户点击EditText时,可以在屏幕底部显示一个键盘。我知道InputMethodService和软键盘示例,但我不能以那种方式使用它,因为我的键盘应该只适用于这个EditText 此外,应该有一个上下文菜单,但这不是这个问题的一部分(我认为) 我读过很多代码片段,但在很多情况下,它们包含不再可用的方法(例如,getViewInflate()),或者是在我不理解或无法翻译为代码的上下文中编写的(请注意,我是Android新手) 在大多数尝试中,当我点击Ed

我正在尝试一个
EditText
,并且当用户点击
EditText
时,可以在屏幕底部显示一个键盘。我知道
InputMethodService
和软键盘示例,但我不能以那种方式使用它,因为我的键盘应该只适用于这个
EditText

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

我读过很多代码片段,但在很多情况下,它们包含不再可用的方法(例如,
getViewInflate()
),或者是在我不理解或无法翻译为代码的上下文中编写的(请注意,我是Android新手)

在大多数尝试中,当我点击
EditText
时,我都会失败,出现此异常:

java.lang.IllegalArgumentException: width and height must be > 0
后跟不包含任何我的类的堆栈跟踪。正如您在下面的代码中看到的,所有尺寸都已设置

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

下面没有介绍的是在一个布局XML中尝试将
RelativeLayout
编辑文本
键盘视图
一起使用。有一个不同的异常,类似于“无效类型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>
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) {
        :
    }
}

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


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

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

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

经过几个小时的“研究和尝试”之后,我终于明白了我的错误,这似乎是“sjngm”的错误。要渲染虚拟键盘,必须

  • 通过膨胀布局xml文件或在行中声明您的
    键盘视图
    (就像您对其他
    视图
    )来声明视图
  • 这里忘记了什么:使用
    findViewById()
    检索键盘视图并调用它:
    keyboardViewInstance.setKeyboard(新键盘(…)
  • 就这样。您将能够在屏幕上看到您的键盘视图!当然,您需要创建自己的键盘类,或者使用现有的类和定义键盘键的xml资源文件
    (res/xml/Keyboard.xml)

    经过几个小时的“研究和尝试”之后,我终于明白了我的错误,这似乎是“sjngm”的错误。要渲染虚拟键盘,必须

  • 通过膨胀布局xml文件或在行中声明您的
    键盘视图
    (就像您对其他
    视图
    )来声明视图
  • 这里忘记了什么:使用
    findViewById()
    检索键盘视图并调用它:
    keyboardViewInstance.setKeyboard(新键盘(…)

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

    ,因为这个项目不再是我这边的项目了,所以我无法测试这个项目,也不能对您的方法说任何话。从你的回答我猜它是有效的,所以谢谢你的更新:)因为这个项目不再是我这边的项目了,所以我不能测试这个,也不能对你的方法说任何话。我从你的答案猜它是有效的,所以谢谢你的更新:)
    import android.inputmethodservice.KeyboardView;
    
    public class LatinKeyboardView extends KeyboardView {
        :
    }
    
    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) {
            :
        }
    }