2013-08-26 18 views
5

So che posso usare "android: windowSoftInputMode =". AdjustPan" per fare un po 'compensato automaticamente ma voglio davvero ottenere altezza della tastiera software per qualche scopo specialeCome ottenere l'altezza della tastiera virtuale Android?

ho avuto modo di scoprire che c'è un argomento simile qui.: Getting the dimensions of the soft keyboard. Ovviamente, non è una soluzione universale

C'è un modo comune o un metodo incorporato per ottenere l'altezza della tastiera software? (O come posso ottenere il valore di offset tra la posizione corrente del cursore e la posizione superiore della tastiera software?)

Grazie mille

+0

Hi Liangwang, è il vostro problema risolto? Anch'io ho bisogno dello stesso. – jrhamza

+0

Non esiste alcun metodo incorporato per ottenere l'altezza della tastiera. Devi usare ViewTreeObserver.OnGlobalLayoutListener() – aravindkanna

risposta

0

Qui la mia soluzione, è anche hacky ma risolve il problema.

  1. Ho posti sulla vista temporanea con sfondo trasparente nella parte inferiore dello schermo.
  2. Ho aggiunto il flag android:windowSoftInputMode="adjustResize" nel tag attività in manifest come @bill suggerisce.
  3. Ora la storia principale è onGlobalLayout(). Ci ho calcolato la differenza tra il y axis di vista temp e un'altezza di root view

    final View view = findViewById(R.id.base); 
    view.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
    
    @Override 
    public void onGlobalLayout() { 
    
        int rootViewHeight = view.getRootView().getHeight(); 
        View tv = findViewById(R.id.temp_view); 
        int location[] = new int[2]; 
        tv.getLocationOnScreen(location); 
        int height = (int) (location[1] + tv.getMeasuredHeight()); 
        deff = rootViewHeight - height; 
        // deff is the height of soft keyboard 
    } 
    }); 
    
0

Get tastiera software Altezza in Xamarin.Android, uso ViewTreeObserver.IOnGlobalLayoutListener per ascoltare GlobalLayout evento Change e calcolare la differenza di cambio di root view prima e dopo per ottenere l'altezza della tastiera. Puoi fare simili nel codice Android nativo.

ecco il codice:

public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity 
{ 
     public static Android.Views.View RootView = null; 
     public void DetectSoftKeyboardHeight() 
     { 
      RootView = this.FindViewById(Android.Resource.Id.Content); 
      if(RootView!=null) 
       RootView.ViewTreeObserver.AddOnGlobalLayoutListener(new MyLayoutListener()); 
     } 
} 
/// <summary> 
/// My layout listener. 
/// Detect Android Soft keyboard height 
/// </summary> 
public class MyLayoutListener : Java.Lang.Object, ViewTreeObserver.IOnGlobalLayoutListener 
{ 
    public void OnGlobalLayout() 
    { 
     // do stuff here 
     Android.Graphics.Rect r = new Android.Graphics.Rect(); 
     if (Mobibranch.Droid.MainActivity.RootView != null) 
     { 
      Mobibranch.Droid.MainActivity.RootView.GetWindowVisibleDisplayFrame(r); 

      int screenHeight = Mobibranch.Droid.MainActivity.RootView.RootView.Height; 
      int keyboardHeight = screenHeight - (r.Bottom); 

      if (keyboardHeight > 0) 
      { 
       //Keyboard is up on screen 
       Android.Util.Log.Verbose("[[[[MyLayoutListener]]]]", "Keyboard is up on screen, Height: "+keyboardHeight); 
      } 
      else 
      { 
       //Keyboard is hidden 
      } 
     } 
    } 
}   
Problemi correlati