2012-03-07 67 views
7

在Textfield中編寫時,我需要我的文本字段向上移動以便在彈出鍵盤時顯示文本字段。libgdx如何檢測鍵盤存在

libgdx是否有某種方法,如果鍵盤可見,則返回true,當它關閉時返回false?

+0

的可能重複[檢測,如果軟鍵盤顯示在屏幕上](http://stackoverflow.com/questions/4745988/檢測是否軟鍵盤在屏幕上可見) – 2012-03-07 16:13:05

+0

這與libgdx無關,您希望監聽本機Android事件。這個問題已經被問到並回答了 – 2012-03-07 16:13:36

+3

有點兒,但是如果它不是由LibGDX提供的,那麼聽取這個事件的實現就會更大;)。 – dom 2012-03-07 16:23:05

回答

1

嘗試

Gdx.input.isPeripheralAvailable(Input.Peripheral.OnscreenKeyboard); 

我只是在文檔看這件事,不知道它實際上做的伎倆。但

Gdx.input.setOnscreenKeyboardVisible(boolean visible); 

方法也可以使用(就像你定義何時鍵盤可見和什麼時候不可見)。

+0

已嘗試Input.Peripheral.OnscreenKeyboard,但在Android上運行應用程序時始終爲true。但我會嘗試手動做到這一點,感謝提示:) – 2012-03-07 16:40:46

+1

否則,正如Andrei Bodnarescu建議的那樣,您可以隨時聽取http://stackoverflow.com/questions/4745988/detect-if-soft中描述的事件-keyboard-is-visible-on-screen,使用與我在http://stackoverflow.com/questions/9584959/using-sqlite-from-libgdx-on-android/9590715#9590715中描述的相同的結構。乾杯! – dom 2012-03-08 00:15:27

+0

它似乎總是回覆不? – JohnyTex 2014-04-10 21:59:19

12

下面的代碼將檢測當您按下文本字段時,防止它顯示鍵盤,然後打開一個本機對話框,使用鍵盤上下移動。它將從本地對話框中輸入並最終將其放回到textField中:

textField.setOnscreenKeyboard(new TextField.OnscreenKeyboard() { 
     @Override 
     public void show(boolean visible) { 
      //Gdx.input.setOnscreenKeyboardVisible(true); 
      Gdx.input.getTextInput(new Input.TextInputListener() { 
       @Override 
       public void input(String text) { 
        textField.setText(text); 
       } 

       @Override 
       public void canceled() { 
        System.out.println("Cancelled."); 
       } 
      }, "Title", "Default text..."); 
     } 
    }); 

祝您好運!

+1

任何想法我們如何改變這個本地對話框的風格? – 2015-05-19 17:01:12

+1

不,我猜你不能這樣做,因爲它在Android和iPhone和Java桌面等方面會有所不同。簡而言之:它的本地=>它的工作原生! – JohnyTex 2015-05-19 20:07:15

+0

太好了,謝謝你的回答! – middlehut 2015-08-05 14:27:01

7

我知道我回答的是一箇舊的線程,但我用谷歌搜索來找到這個問題的答案,但無法在任何地方找到它。現在我自己創建了一個解決方案。以下是如何在Android上以優雅的方式完成它:) 我創建了一個ApplicationBundle來捆綁接口以添加特定於平臺的事物。如果您想使用RoboVM,您也可以在iOS上執行此操作。

我的解決辦法:

創建核心項目SizeChangeListener接口:

public interface SizeChangeListener { 
    public void onSizeChange(float width, float height); 
} 

創建核心項目視圖界面:

public interface View { 
    public void onSizeChange(float width, float height); 
    public void addListener(SizeChangeListener sizeChangeListener); 
    public float getWidth(); 
    public float getHeight(); 
} 

創建AndroidView實現View接口:

public class AndroidView implements View { 

    private ArrayList<SizeChangeListener> listeners = new ArrayList<SizeChangeListener>(); 
    private float width, height; 
    public AndroidView(int width, int height) { 
     this.width = width; 
     this.height = height; 
    } 

    public void addListener(SizeChangeListener listener) { 
     listeners.add(listener); 
    } 

    public void onSizeChange(float width, float height) { 
     this.width = width; 
     this.height = height; 
     for(SizeChangeListener listener : listeners) 
      listener.onSizeChange(width, height); 
    } 

    public float getWidth() { 
     return width; 
    } 

    public float getHeight() { 
     return height; 
    } 

} 

核心項目

public class ApplicationBundle { 

    private final View view; 

    public ApplicationBundle(View view) { 
     this.view = view; 
    } 

    public View getView() { 
     return view; 
    } 
} 

建立從核心項目所需的進口創造ApplicationBundle。在AndroidLauncher在Android項目中添加以下內容:

public class AndroidLauncher extends AndroidApplication { 

    private View rootView; 
    private AndroidView androidView; 
    private int width, height; 

    @Override 
    protected void onCreate (Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     AndroidApplicationConfiguration config = new AndroidApplicationConfiguration(); 
     rootView = this.getWindow().getDecorView().getRootView(); 
     Rect rect = new Rect(); 
     rootView.getWindowVisibleDisplayFrame(rect); 
     width = rect.width(); 
     height = rect.height(); 
     androidView = new AndroidView(width, height); 

     rootView.addOnLayoutChangeListener(new OnLayoutChangeListener() { 

      @Override 
      public void onLayoutChange(View v, int left, int top, int right, 
        int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) { 

       Rect rect = new Rect(); 
       rootView.getWindowVisibleDisplayFrame(rect); 

       if(!(width == rect.width() && height == rect.height())) { 
        width = rect.width(); 
        height = rect.height(); 
        androidView.onSizeChange(width, height); 
       } 
      } 

     }); 

     initialize(new DigMeApp(new ApplicationBundle(androidView)), config); 
    } 
} 

在你的主MyApp的核心項目在create()方法添加SizeChangeListener實現與您從構造得到了看法。

public class MyApp extends Game { // or ApplicationAdapter 
    private View view; 
    private Stage stage; 
    // your own variables 

    public MyApp(ApplicationBundle applicationBundle) { 
     view = applicationBundle.getView(); 
    } 

    @Override 
    public void create() { 
     stage = new Stage(); 
     // add some textfields 
    final TextField tf1 = new TextField("", skin); 
    final TextField tf2 = new TextField("", skin); 

    tf1.setWidth((float)view.getWidth() * 0.6f); 
    tf2.setWidth((float)view.getWidth() * 0.6f); 
    tf1.setHeight((float)view.getHeight() * 0.05f); 
    tf2.setHeight((float)view.getHeight() * 0.05f); 
     view.addListener(new SizeChangeListener() {   
      @Override 
      public void onSizeChange(float width, float height) { 
       Gdx.app.log("INFO", "Visible area: " + width + " " + height); 
       Gdx.app.log("INFO", "Stage area: " + stage.getWidth() + " " + stage.getHeight()); 
       float keyboardHeight = getKeyboardHeight(); 

// MOVE THEM OUT OF THE WAY :) 

       tf1.addAction(Actions.moveTo(width/2 - tf1.getWidth()/2.0f, keyboardHeight + (6 * (height/8)), 1, Interpolation.sineOut)); 
       tf2.addAction(Actions.moveTo(width/2 - tf2.getWidth()/2.0f, keyboardHeight + (7 * (height/8)), 1, Interpolation.sineOut)); 


//    Gdx.gl20. 
//    tf.setPosition(width/2 - (tf.getWidth()/2.0f), 0); 
      } 
     }); 
} 

也許創建一個小鍵盤heigt方法,像我一樣:

private float getKeyboardHeight() { 
     return stage.getHeight() - view.getHeight(); 
    } 
+0

我喜歡這個答案,它在我測試過的所有設備上工作得非常好。我唯一要做的評論是'view.getHeight()'需要縮放以匹配舞臺或攝像機尺寸,如果你已經強制libgdx成爲特定的分辨率。 @Willempie你有沒有適用於iOS的工作解決方案? – hamham 2016-05-25 10:37:07

+0

這是一個很好的答案,效果很好,幸運的是我不必實現iOS的方式,所以這不是問題。這應該是選擇的答案,但我有一個警告。當我觸摸文本框時,隨着鍵盤出現,我的應用程序頂部的黑色橫幅也會顯示,其中包含我的應用程序名稱。這似乎是行'rootView = this.getWindow()。getDecorView()。getRootView();'任何想法如何避免這種情況? – 2017-02-16 00:50:06

+0

[已解決]'this.requestWindowFeature(Window.FEATURE_NO_TITLE);'我在rootView被設置之前添加了這一行 – 2017-02-16 01:05:21