SORU
27 Ocak 2010, ÇARŞAMBA


Nasıl Android yazılım klavye görünürlüğünü kontrol etmek için?

Eğer yazılım klavye gösterilirse çok basit bir şey öğrenmem gerekiyor. Bu olası Android mi?

CEVAP
19 Ocak 2011, ÇARŞAMBA


YENİ CEVAPekledi Oca 2012 25

Aşağıda cevap yazma beri, biri bana Sürüm 1 beri SDK içinde gizlenmiş olan ViewTreeObserver ve arkadaşları, API varlığı konusunda toplanan.

Yerine gerektiren, özel bir Düzen türü, çok daha basit bir çözüm vermek aktivitesi kök görünümü bilinen bir KİMLİĞİ, demek '@ id/activityRoot', kanca bir GlobalLayoutListener içine ViewTreeObserver ve oradan hesaplayın boyutu arasındaki fark etkinliği görüşüne kök ve pencerenin boyutu:

final View activityRootView = findViewById(R.id.activityRoot);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        int heightDiff = activityRootView.getRootView().getHeight() - activityRootView.getHeight();
        if (heightDiff > 100) { // if more than 100 pixels, its probably a keyboard...
            ... do something here
        }
     }
});

Kolay!

Not: Uygulamanız çözüm çalışmaz yukarıda Android android:windowSoftInputMode="adjustResize" Apaçık bu bayrak yoksa ayarlamak gerekir.

ORİJİNAL CEVAP

Evet bu mümkün ama olması gereken daha zordur.

Klavye görünür ve kaybolur ilgilenmeye ihtiyacım var ki bu çok sık olur) sonra ben ne onMeasure() geçersiz kılan bir üst düzey düzeni benim sınıf özelleştirin. Temel mantığı ise düzeni önemli ölçüde daha az penceresi toplam alanı daha kendini doldurma eğer bulur, sonra yumuşak bir klavye muhtemelen gösteriyor olmasıdır.

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.LinearLayout;

/*
 * LinearLayoutThatDetectsSoftKeyboard - a variant of LinearLayout that can detect when 
 * the soft keyboard is shown and hidden (something Android can't tell you, weirdly). 
 */

public class LinearLayoutThatDetectsSoftKeyboard extends LinearLayout {

    public LinearLayoutThatDetectsSoftKeyboard(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public interface Listener {
        public void onSoftKeyboardShown(boolean isShowing);
    }
    private Listener listener;
    public void setListener(Listener listener) {
        this.listener = listener;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int height = MeasureSpec.getSize(heightMeasureSpec);
        Activity activity = (Activity)getContext();
        Rect rect = new Rect();
        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
        int statusBarHeight = rect.top;
        int screenHeight = activity.getWindowManager().getDefaultDisplay().getHeight();
        int diff = (screenHeight - statusBarHeight) - height;
        if (listener != null) {
            listener.onSoftKeyboardShown(diff>128); // assume all soft keyboards are at least 128 pixels high
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);       
    }

    }

Etkinlik sınıfta

public class MyActivity extends Activity implements LinearLayoutThatDetectsSoftKeyboard.Listener {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ...
        LinearLayoutThatDetectsSoftKeyboard mainLayout = (LinearLayoutThatDetectsSoftKeyboard)findViewById(R.id.main);
        mainLayout.setListener(this);
        ...
    }


    @Override
    public void onSoftKeyboardShown(boolean isShowing) {
        // do whatever you need to do here
    }

    ...
}

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Garrett Müller

    Garrett Mül

    26 HAZİRAN 2009
  • Tips On Linux

    Tips On Linu

    26 Temmuz 2008
  • YouChewBu

    YouChewBu

    26 Ocak 2009