【问题标题】:How do you open up a context menu in a soft keyboard by pressin onto a soft key?如何通过按下软键在软键盘中打开上下文菜单?
【发布时间】:2014-08-28 20:50:58
【问题描述】:

例如,当我在虚拟键盘中选择“完成”软键时,它应该触发上下文菜单以显示诸如
“选项 1”、
“选项 2”、
等选项“选项 3”
标题为“选项”。

以下是我的 xml 文件的简化版本。

<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
android:keyWidth="10%p"
android:horizontalGap="0px"
android:verticalGap="0px"
android:keyHeight="@dimen/key_height"
>

<Row>
    <Key android:codes="1" android:keyLabel="Encrypt" android:keyEdgeFlags="left"/>
    <Key android:codes="2" android:keyLabel="Translate"/>
    <Key android:codes="3" android:keyLabel="Done" android:keyEdgeFlags="right"/>
</Row>
</Keyboard>

【问题讨论】:

    标签: java android keyboard android-softkeyboard


    【解决方案1】:

    要显示上下文菜单,请参见以下代码:

    在Activity的onCreate方法中:

      key.setOnClickListener(new OnClickListener() {  
    
           @Override  
           public void onClick(View v) {  
            //Creating the instance of PopupMenu  
            PopupMenu popup = new PopupMenu(MainActivity.this, button1);  
            //Inflating the Popup using xml file  
            popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());  
    
            //registering popup with OnMenuItemClickListener  
            popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {  
             public boolean onMenuItemClick(MenuItem item) {  
              Toast.makeText(MainActivity.this,"You Clicked : " + item.getTitle(),Toast.LENGTH_SHORT).show();  
              return true;  
             }  
            });  
    
            popup.show();//showing popup menu
           }  
          });//closing the setOnClickListener method
    

    文件:poupup_menu.xml

    <menu xmlns:androclass="http://schemas.android.com/apk/res/android" >  
    
    <item  
        android:id="@+id/one"  
        android:title="One"/>  
    
    <item  
        android:id="@+id/two"  
        android:title="Two"/>  
    
    <item  
        android:id="@+id/three"  
        android:title="Three"/>  
    
    </menu>
    

    【讨论】:

    • 但是,我正在处理的类已经扩展了 InputMethodService。因为我正在研究 Eclipse ADT Budle 提供的示例代码。我如何扩展 Activity?
    • 你能显示代码吗?我们不能一次扩展 2 个类。
    • 对不起,代码太长了。如果我们不能同时扩展 2 个类,我该如何解决需要扩展 2 个类的问题?
    【解决方案2】:

    您的软键盘按键在扩展 InputMethodService 并实现 onKeyboardActionListener 的服务中处理。

    在服务中,您正在处理软键盘上的按键,添加弹出菜单代码,否则您将在应用程序中使用屏幕按钮。 像这样:

    public class MyKeyboardService extends InputMethodService implements KeyboardView.OnKeyboardActionListener
    {
        private KeyboardView viewOfKeyboard;
        private Keyboard theKeyboardLayout;
    
        @Override
        public View onCreateInputView()
        {
            viewOfKeyboard = (KeyboardView)getLayoutInflater().inflate(R.layout.keyboard1, null);
            theKeyboardLayout = new Keyboard(this, R.xml.keyboardlayout);
            viewOfKeyboard.setKeyboard(theKeyboardLayout);
            viewOfKeyboard.setOnKeyboardActionListener(this);
            return viewOfKeyboard;
        }
    
    
        .................//InputMethodService methods goes here
    
        @Override
        public void onKey(int primaryCode, int[] keyCodes)
        {
            InputConnection ic = getCurrentInputConnection();
    
            switch(primaryCode){ 
            case -222: //-222 is just a random ID number I assigned my key on the softkeyboard.
                PopupMenu popup = new PopupMenu(MyKeyboardService.this, viewOfKeyboard);
                popup.getMenuInflater().inflate(R.menu.menu_main, popup.getMenu());
                //menu_main being the xml file for the popup menu
                popup.show();
                break;
        }
    }
    

    从那里,只需像往常一样添加代码来处理弹出菜单中的按钮点击。

    menu_main XML 文件供参考:

    < menu xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          xmlns:tools="http://schemas.android.com/tools"
          tools:context=".MainActivity">
          < item android:id="@+id/action_settings"
              android:title="@string/action_settings"
              android:orderInCategory="100"
              app:showAsAction="never"/>
          < item android:id="@+id/button1"
                  android:title="button1"/>
    < /menu>
    

    【讨论】:

    • 感谢分享!我认为他无法从 Marshmallow 内的弹出菜单中更新或获取当前输入连接中的任何文本。出了点问题,我打开了一个错误报告。你在 Marshmallow 中测试过吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-16
    相关资源
    最近更新 更多