草庐IT

android - 自定义 Android 键盘焦点问题

coder 2023-12-05 原文

我目前正在开发一个自定义键盘应用程序,该应用程序将针对使用 DPad 作为主要输入设备的设备进行优化。

我的问题是,当光标位于 EditText 字段中并且您按下(例如 KEYCODE_DPAD_DOWN)时,键盘 View 未接收到焦点和 KeyEvents。要么没有任何反应,要么相关 EditText 下方的元素获得焦点。

相关代码如下。

任何帮助将不胜感激。我已尝试剖析 SoftKeyboard 示例以及 KeyboardView.java 以获取提示,但均未成功。

谢谢, 布莱恩

我的键盘.java

public class MyKeyboard extends InputMethodService {

    private static final String TAG = "MyKeyboard";
    private MyKeyboardView mInputView = null;

    @Override public void onCreate() {
        super.onCreate();
    }

    @Override public View onCreateInputView() {
        mInputView = (MyKeyboardView) getLayoutInflater().inflate(R.layout.input, null);

        // attempts to make this focusable
        mInputView.setClickable(true);
        mInputView.setFocusableInTouchMode(true);
        mInputView.setFocusable(true);
        mInputView.setEnabled(true);

        return mInputView;
    }

    @Override public View onCreateCandidatesView() {
        super.onCreateCandidatesView();
        return null;
    }

    @Override public void onStartInputView(EditorInfo info, boolean restarting) {
        super.onStartCandidatesView(info, restarting);
    }

    @Override public void onFinishInput() {
        super.onFinishInput();
    }

    @Override public void onDestroy() {
        super.onDestroy();
    }
}

MyKeyboardView.java

public class MyKeyboardView extends TableLayout implements View.OnClickListener, View.OnFocusChangeListener {

    private static final String TAG = "MyKeyboardView";
    private ArrayList<Character> charList = new ArrayList<Character>();

    public MyKeyboardView(Context context) {
        super(context);

        populateKeyboard();
        this.setOnFocusChangeListener(this);
        this.setOnClickListener(this);
    }

    public MyKeyboardView(Context context, AttributeSet attrs) {
        super(context, attrs);

        populateKeyboard();
        this.setOnFocusChangeListener(this);
        this.setOnClickListener(this);
    }

    @Override
    public void onClick(View arg0) {
        Log.d(TAG, "onClick");
    }

    private void populateKeyboard() {
        charList.add(new Character(','));
        charList.add(new Character('.'));
        charList.add(new Character('?'));
        charList.add(new Character('<'));
        charList.add(new Character('>'));
        charList.add(new Character((char) 0x2798)); // arrow
        charList.add(new Character((char) 0x2798)); // arrow
        charList.add(new Character((char) 0x2798)); // arrow
        charList.add(new Character((char) 0x005F)); // underscore
        for(char c = '@'; c < 'Z'; c++) {
            charList.add(new Character(c));
            Log.d(TAG, "char: " + c);
        }


        TableRow tr = null;
        for(int i=0; i<charList.size(); i++) {
            if(i % 7 == 0) {
                if(tr != null)
                    this.addView(tr);
                tr = new TableRow(this.getContext());
                tr.setGravity(Gravity.CENTER_HORIZONTAL);
            }
            TextView tv = new TextView(this.getContext());
            tv.setPadding(21, 2, 21, 2);
            tv.setText(charList.get(i).toString());
            tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 22);
            tv.setTextColor(Color.WHITE);
            tv.setGravity(Gravity.CENTER);
            tv.setFocusable(true);
            tv.setEnabled(true);

            tr.addView(tv);
        }
        if(tr.getChildCount() > 0)
            this.addView(tr);
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        Log.d(TAG, "mInputView onFocusChange " + (hasFocus ? "true" : "false"));
    }
}

输入.xml

<?xml version="1.0" encoding="utf-8"?>
<com.weirdtuesday.mykeyboard.MyKeyboardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/input"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:background="#FF000000"
    android:focusable="true" />

最佳答案

按键事件必须在onKey方法中手动处理。要移动光标,我使用这个:

if (primaryCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
        int position = connection.getTextBeforeCursor(Integer.MAX_VALUE, 0)
                .length();
        final CharSequence selected = connection.getSelectedText(0);
        if (selected != null)
            connection.commitText(
                    mComposing.substring(0,
                            mComposing.length() - selected.length()), 1);
        else
            connection.commitText(mComposing, 1);
        connection.setSelection(position + 1, position + 1);
    }

关于android - 自定义 Android 键盘焦点问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9779529/

有关android - 自定义 Android 键盘焦点问题的更多相关文章

  1. ruby - Facter::Util::Uptime:Module 的未定义方法 get_uptime (NoMethodError) - 2

    我正在尝试设置一个puppet节点,但ruby​​gems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由ruby​​gems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby

  2. ruby - 在 64 位 Snow Leopard 上使用 rvm、postgres 9.0、ruby 1.9.2-p136 安装 pg gem 时出现问题 - 2

    我想为Heroku构建一个Rails3应用程序。他们使用Postgres作为他们的数据库,所以我通过MacPorts安装了postgres9.0。现在我需要一个postgresgem并且共识是出于性能原因你想要pggem。但是我对我得到的错误感到非常困惑当我尝试在rvm下通过geminstall安装pg时。我已经非常明确地指定了所有postgres目录的位置可以找到但仍然无法完成安装:$envARCHFLAGS='-archx86_64'geminstallpg--\--with-pg-config=/opt/local/var/db/postgresql90/defaultdb/po

  3. ruby - 通过 rvm 升级 ruby​​gems 的问题 - 2

    尝试通过RVM将RubyGems升级到版本1.8.10并出现此错误:$rvmrubygemslatestRemovingoldRubygemsfiles...Installingrubygems-1.8.10forruby-1.9.2-p180...ERROR:Errorrunning'GEM_PATH="/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/ruby-1.9.2-p180@global:/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/rub

  4. ruby-on-rails - Rails 3.2.1 中 ActionMailer 中的未定义方法 'default_content_type=' - 2

    我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer

  5. ruby-on-rails - form_for 中不在模型中的自定义字段 - 2

    我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢

  6. ruby - 主要 :Object when running build from sublime 的未定义方法 `require_relative' - 2

    我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby​​1.9+ 关于ruby-主要:Objectwhenrun

  7. ruby - 通过 RVM (OSX Mountain Lion) 安装 Ruby 2.0.0-p247 时遇到问题 - 2

    我的最终目标是安装当前版本的RubyonRails。我在OSXMountainLion上运行。到目前为止,这是我的过程:已安装的RVM$\curl-Lhttps://get.rvm.io|bash-sstable检查已知(我假设已批准)安装$rvmlistknown我看到当前的稳定版本可用[ruby-]2.0.0[-p247]输入命令安装$rvminstall2.0.0-p247注意:我也试过这些安装命令$rvminstallruby-2.0.0-p247$rvminstallruby=2.0.0-p247我很快就无处可去了。结果:$rvminstall2.0.0-p247Search

  8. ruby - Fast-stemmer 安装问题 - 2

    由于fast-stemmer的问题,我很难安装我想要的任何ruby​​gem。我把我得到的错误放在下面。Buildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingfast-stemmer:ERROR:Failedtobuildgemnativeextension./System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/rubyextconf.rbcreatingMakefilemake"DESTDIR="cleanmake"DESTDIR=

  9. ruby - 在 Ruby 中用键盘诅咒数组浏览 - 2

    我正在尝试在Ruby中制作一个cli应用程序,它接受一个给定的数组,然后将其显示为一个列表,我可以使用箭头键浏览它。我觉得我已经在Ruby中看到一个库已经这样做了,但我记不起它的名字了。我正在尝试对soundcloud2000中的代码进行逆向工程做类似的事情,但他的代码与SoundcloudAPI的使用紧密耦合。我知道cursesgem,我正在考虑更抽象的东西。广告有没有人见过可以做到这一点的库或一些概念证明的Ruby代码可以做到这一点? 最佳答案 我不知道这是否是您正在寻找的,但也许您可以使用我的想法。由于我没有关于您要完成的工作

  10. ruby - 在 Ruby 中有条件地定义函数 - 2

    我有一些代码在几个不同的位置之一运行:作为具有调试输出的命令行工具,作为不接受任何输出的更大程序的一部分,以及在Rails环境中。有时我需要根据代码的位置对代码进行细微的更改,我意识到以下样式似乎可行:print"Testingnestedfunctionsdefined\n"CLI=trueifCLIdeftest_printprint"CommandLineVersion\n"endelsedeftest_printprint"ReleaseVersion\n"endendtest_print()这导致:TestingnestedfunctionsdefinedCommandLin

随机推荐