方法はいろいろあるのですが、
どれが正しく動くかは端末でやってみないと分かりません。
InputMethodManagerでやろうと思いましたが、
うまく動きませんでしたので、
別の方法で実装していきます。
まずはLinearLayoutをextendsして、
Layoutが変更した事を検出できるLayoutクラスを作成していきます。
package com.shonanshachu.layout;
import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.LinearLayout;
public class LinearLayoutDetectsSoftKeyboard extends LinearLayout{
public LinearLayoutDetectsSoftKeyboard(Context context, AttributeSet attrs){
super(context, attrs);
}
public interface Listener {
public void onSoftKeyboardChanged(boolean isShowing);
}
private Listener listener;
public void setListener(Listener listener){
this.listener = listener;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
if(listener != null){
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;
listener.onSoftKeyboardChanged(diff>128);
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
このLayoutを使用して、XMLを書きます。そして、Activityには、Listenerをimplementsします。
public class ActivityMain extends Activity implements LinearLayoutDetectsSoftKeyboard.Listener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
public void onSoftKeyboardChanged(boolean isShowing) {
// do something here
}
}
これで、onSoftKeyboardChangedでソフトキーボードが表示されているかどうかを知る事が出来ます。では、引き続きプログラムを楽しんでくださいませ。
0 件のコメント:
コメントを投稿