本文介绍了全屏Surface View中的Android软键盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!我目前正在扩展Android游戏框架,该游戏框架是我在Mario Zechner(也是Libgdx的开发者)所写的书中找到的.

HI!I am currently extending an Android Game Framework,which I found in a Book written by Mario Zechner (who also develops Libgdx).

到目前为止,它运行良好,并且我完成了该框架的Java桌面实现.

It is working great so far and I accomplished a Java Desktop Implementation of the Framework.

但是缺少一件事,就是正确使用SoftKeyboard.

But one tiny thing is missing, the correct usage of the SoftKeyboard.

您可以尝试并重现我的错误,整个项目都在github上,而android模块是"app"文件夹. Java版本(左箭头键=返回键)位于javalib模块中.

You can try out and reproduce my bug, the whole project is on github,and the android module is the "app"folder. The java version (left arrow key = key back) is in the javalib module.

https://github.com/Railwanderer/AndroidGameFramework

框架将Window FullScreenFlags和NoTitle标志与Surface View结合使用进行渲染.

The Framework uses Window FullScreenFlags and NoTitle Flags in combination with a Surface View for rendering.

AndroidGame类中的标志:

The Flags in AndroidGame Class:

// set FullScreen
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

表面视图:

public class AndroidFastRenderView extends SurfaceView implements Runnable {

AndroidGame game;
Bitmap framebuffer;
Thread renderThread = null;
SurfaceHolder holder;

// Fps Counting Stuff...
static long             lastTime;
static double           fps;
static String           FPS = "";


volatile boolean running = false;


public AndroidFastRenderView(AndroidGame game, Bitmap framebuffer) {
    super(game);
    this.game = game;
    this.framebuffer = framebuffer;
    this.holder = getHolder();
}

public void resume() {
    running = true;
    renderThread = new Thread(this);
    renderThread.start();
}

public void run() {
    Rect dstRect = new Rect();
    long startTime = System.nanoTime();
    while (running) {
        if (!holder.getSurface().isValid())
            continue;

        lastTime = System.nanoTime();
        float deltaTime = (System.nanoTime() - startTime) / 1000000000.0f;
        startTime = System.nanoTime();

        game.getCurrentScreen().update(deltaTime);
        Graphics g = game.getGraphics();
        g.clear(Color.BLACK);
        game.getCurrentScreen().present(deltaTime);
        g.drawString(FPS,framebuffer.getWidth()-100,framebuffer.getHeight()-30);

        Canvas canvas = holder.lockCanvas();
        canvas.getClipBounds(dstRect);
        canvas.drawBitmap(framebuffer, null, dstRect, null);
        holder.unlockCanvasAndPost(canvas);

        //one second(nano) divided by amount of time it takes for one frame to finish
        fps = 1000000000.0 / (System.nanoTime() - lastTime);
        lastTime = System.nanoTime();
        FPS = "FPS: " + (int) fps;
    }
}

public void pause() {
    running = false;
    while (true) {
        try {
            renderThread.join();
            break;
        } catch (InterruptedException e) {
            // retry
        }
    }
}

我的问题是,当我尝试拉起SoftKeyboard 时,它将显示用SHOW_FORCED调用.它还位于我的表面视图的顶部,但是触摸事件不会触发键盘,但会以某种方式直通并击中我的表面视图.

My Problem is when I try to pull up the SoftKeyboard, it would show whencalling with SHOW_FORCED. It also lays itself ontop of my surface view, but the touch events won't trigger the keyboard but somehow pass thru and hit my surface view.

public void showKeyboard(){
    inputManager.showSoftInput(view, InputMethodManager.SHOW_FORCED);
}

当我疯狂地敲击软键盘的最上边界(第一行键的上边缘)时,最终一些触摸事件实际上触发了这些键.但是它仅适用于第一行键,根本不能令人满意.我得到的印象是,由于记录的触摸事件以偏移量返回,整个事情发生了变化.

When I hit the most upper border of the soft keyboard (first line keys upper edges) like crazy, eventually some touch events actually trigger the keys. But it only works for the first Line of keys and not satisfying at all.I get the Impression that the whole thing is shifted since the recorded touch events come back with an offset.

我当前正在使用任何keyEvent(键侦听器的第一行onKey()方法)调用软键盘.所以返回键将触发键盘.由于此键是我设备上的硬键,因此始终可以识别.

I am currently calling the soft keyboard with any keyEvent ( first line onKey() method of key listener). so the back key will trigger the keyboard. This key is always recognized since it is a hard key on my device.

-是否可以使用可调整大小的布局归档全屏?我猜这是阻止键盘正常工作的标志.-还有其他将两者结合起来的想法吗?

-Is there a possibility to archive fullscreen with a resizable layout? I guess its the flags preventing the keyboard to work correctly.-any other ideas to combine the both?

此外,这是我的android清单,此应用程序使用的唯一xml文件:

Additionally here my android manifest, the only xml file this app uses:

<application android:icon="@mipmap/ic_launcher" android:label="TestGame">

    <activity android:name="railwanderer.androidgameframework.ExampleAndroidGame.StartClass"
        android:label="StartClass"
        android:screenOrientation="landscape"
        android:configChanges="keyboard|keyboardHidden|orientation">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

    </activity>
</application>

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-sdk android:minSdkVersion="3" android:targetSdkVersion="9"/>

推荐答案

我找到了解决方法:D https://stackoverflow.com/questions/33561137/soft-keyboard-not-接收表面上的触摸

I found a solve :Dhttps://stackoverflow.com/questions/33561137/soft-keyboard-not-receiving-touches-over-surfaceview

我确实在CustomSurfaceView类上重写了onCreateInputConnection方法,就像在发布的链接中一样.

I did override onCreateInputConnection method on the CustomSurfaceView class, like in the posted link.

@Override
  public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
   InputConnection conn = super.onCreateInputConnection(outAttrs);
   outAttrs.imeOptions = EditorInfo.IME_FLAG_NO_FULLSCREEN;
   return conn;
}

现在可以使用,请尝试一下,我将立即上传以查看它是否也可以在您的设备上使用.

it works now, pls try it out, I will upload right away to see if it works on your device too.

这篇关于全屏Surface View中的Android软键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 20:10