編輯:關於Android編程
你有一個登錄界面是這樣設計的
你希望登錄時它是這樣的,<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+PGltZyBzcmM9"/uploadfile/Collfiles/20140508/2014050808314879.jpg" alt="\">
你不甘心,你將adjustResize修改成adjustPan,結果是這樣的
可以在onSizeChanged(int w, int h,int oldw,int oldh) 方法裡監聽界面大小的變化,然後在該方法裡定義回調函數,當監聽到鍵盤被調出時,將鍵盤向上推一段距離,這樣將登錄鍵顯示出來。
package com.example.keyboardlistener; import android.content.Context; import android.util.AttributeSet; import android.util.Log; import android.widget.RelativeLayout; public class KeyboardLayout extends RelativeLayout { private onSizeChangedListener mChangedListener; private static final String TAG ="KeyboardLayoutTAG"; private boolean mShowKeyboard = false; public KeyboardLayout(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); // TODO Auto-generated constructor stub } public KeyboardLayout(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub } public KeyboardLayout(Context context) { super(context); // TODO Auto-generated constructor stub } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // TODO Auto-generated method stub super.onMeasure(widthMeasureSpec, heightMeasureSpec); Log.d(TAG, "onMeasure-----------"); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { // TODO Auto-generated method stub super.onLayout(changed, l, t, r, b); Log.d(TAG, "onLayout-------------------"); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { // TODO Auto-generated method stub super.onSizeChanged(w, h, oldw, oldh); Log.d(TAG, "--------------------------------------------------------------"); Log.d(TAG, "w----" + w + "\n" + "h-----" + h + "\n" + "oldW-----" + oldw + "\noldh----" + oldh); if (null != mChangedListener && 0 != oldw && 0 != oldh) { if (h < oldh) { mShowKeyboard = true; } else { mShowKeyboard = false; } mChangedListener.onChanged(mShowKeyboard); Log.d(TAG, "mShowKeyboard----- " + mShowKeyboard); } } public void setOnSizeChangedListener(onSizeChangedListener listener) { mChangedListener = listener; } interface onSizeChangedListener{ void onChanged(boolean showKeyboard); } }
在主Activity裡創建一個Handler,當監聽到布局發生變化時,通過Handler更新UI。
package com.example.keyboardlistener; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; import android.view.ViewTreeObserver.OnPreDrawListener; import android.widget.Button; import com.example.keyboardlistener.KeyboardLayout.onSizeChangedListener; public class MainActivity extends Activity { private static final String TAG = "KeyboardLayoutTAG"; private KeyboardLayout mRoot; private Button mLogin; private int mLoginBottom; private static final int KEYBOARD_SHOW = 0X10; private static final int KEYBOARD_HIDE = 0X20; private boolean mGetBottom = true; private Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub super.handleMessage(msg); switch (msg.what) { case KEYBOARD_HIDE: mRoot.setPadding(0, 0, 0, 0); break; case KEYBOARD_SHOW: int mRootBottom = mRoot.getBottom(); Log.d(TAG, "the mLoginBottom is " + mLoginBottom); mRoot.setPadding(0, mRootBottom - mLoginBottom, 0, 0); break; default: break; } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getActionBar().hide(); mRoot = (KeyboardLayout) findViewById(R.id.root_view); mLogin = (Button) findViewById(R.id.login); mRoot.setOnSizeChangedListener(new onSizeChangedListener() { @Override public void onChanged(boolean showKeyboard) { // TODO Auto-generated method stub if (showKeyboard) { mHandler.sendMessage(mHandler.obtainMessage(KEYBOARD_SHOW)); Log.d(TAG, "show keyboard"); } else { mHandler.sendMessage(mHandler.obtainMessage(KEYBOARD_HIDE)); } } }); mRoot.getViewTreeObserver().addOnPreDrawListener(new OnPreDrawListener() { @Override public boolean onPreDraw() { // TODO Auto-generated method stub if (mGetBottom) { mLoginBottom = mLogin.getBottom();//獲取登錄按鈕的位置信息。 } mGetBottom = false; return true; } }); } // @Override // public boolean onCreateOptionsMenu(Menu menu) { // // Inflate the menu; this adds items to the action bar if it is present. // getMenuInflater().inflate(R.menu.main, menu); // return true; // } }第二種方法的實現:
借助ViewTreeObserver類對你想監聽的view添加OnGlobalLayoutListener監聽器,然後在onGlobalLayout()方法裡窗口的變化情況來判斷鍵盤是否被調出來了。下面是ViewTreeObserver監聽的部分的代碼:
mRoot.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() { @Override public void onGlobalLayout() { int offset = mRoot.getRootView().getHeight() - mRoot.getHeight(); //根據視圖的偏移值來判斷鍵盤是否顯示 if (offset > 300) { mHandler.sendMessage(mHandler.obtainMessage(KEYBOARD_SHOW)); } else { mHandler.sendMessage(mHandler.obtainMessage(KEYBOARD_HIDE)); } } });
其中Handler的具體實現同上述結構中的Handler一樣。
參考文章:InputMethod
例子源碼下載地址
大家都可以微信附近的人功能搜索附近正在使用微信的小伙伴,進行打招呼聊天,不過有些用戶反映自己無法被微信附近的人搜索到,微信附近的人看不到我怎麼辦,下面小編就
本文實例講述了Android開發之在程序中時時獲取logcat日志信息的方法。分享給大家供大家參考,具體如下:今天分享一個在軟件開發中很實用的例子,也是這幾天在通宵加班中
根據提供的課程信息,動態生成課程表。不同於網上流傳的課表形式,課程節數是固定,本課表的課程節數不固定。1、效果圖每天共有12節課,上課節數每天都不同。2、布局文件代碼周一
SlidingDrawer隱藏屏外的內容,並允許用戶通過handle以顯示隱藏內容。它可以垂直或水平滑動,它有倆個View組成,其一是可以拖動的handle,其二是隱藏內