Android實(shí)現(xiàn)輸入法彈出時(shí)把布局頂上去和登錄按鈕頂上去的解決方法
背景:在寫登錄界面時(shí),老板就覺(jué)得在輸入密碼的時(shí)候談出來(lái)的輸入法軟鍵盤把登錄按鈕遮擋住了(入下圖所示,不爽),連輸入框都被擋了一半,于是不滿意了,要叫我改,于是我看QQ的登錄效果,我就去研究了一下,彈出輸入法整個(gè)布局上來(lái)了,終于讓老板滿意了。
(如上圖這樣,老板不滿意的,呵呵)
1,咱們就解決問(wèn)題吧。
我看了很多博客和問(wèn)答,很多人都說(shuō)直接在在AndroidManifest.xml中給這個(gè)Activity設(shè)置 <activity android:windowSoftInputMode="stateVisible|adjustPan" ...>這樣就好使了,這個(gè)是否在逗,整個(gè)布局向上移動(dòng)并不明顯,反正我的是不好使,不知道那些博主是怎么弄好使的。不過(guò),看評(píng)論,也有很多人說(shuō)不好使。那就做一個(gè)大家都好使的代碼出來(lái)。先看效果。
哈哈,大家有沒(méi)有看到,連登錄按鈕都一起跑上去了,應(yīng)該是頂上去的。老板再也不用擔(dān)心登錄按鈕被覆蓋掉了。
那咱們就上代碼啦:代碼不多,全在布局上,就可以解決。
<?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:fillViewport="true" android:fadeScrollbars="true"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:gravity="top|center_horizontal" android:orientation="vertical" android:background="@color/red2" android:visibility="visible"> <ImageView <!--這個(gè)其實(shí)是我放布局中間的控件,我隨便寫的,放任何控件都可以--> android:layout_width="200dp" android:layout_height="160dp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:alwaysDrawnWithCache="true" android:gravity="center|center_horizontal" android:orientation="vertical" android:visibility="visible" android:background="@color/abc_search_url_text_normal"> <EditText android:background="@color/white" android:layout_width="200dp" android:layout_height="60dp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:background="@color/cornsilk" android:gravity="top|center_horizontal" android:orientation="vertical" android:visibility="visible"> <Button android:layout_marginTop="20dp" android:gravity="center" android:text="登錄" android:layout_width="200dp" android:layout_height="50dp" /> </LinearLayout> </LinearLayout> </ScrollView>
對(duì)上面就是所有視線代碼了,外面一個(gè)scrollview,包含一個(gè)LinearLayout,在中間包含了三個(gè)LinearLayout,當(dāng)然了包含三個(gè)什么容器控件都行,但是一定要用權(quán)重(layout_weight)來(lái)約束,這是重點(diǎn),我只說(shuō)了一遍,還有就是LinearLayout內(nèi)部的布局盡量用wrap_content,即時(shí)要固定高度也要適當(dāng),調(diào)節(jié)調(diào)節(jié)就好了。
使用的時(shí)候很簡(jiǎn)單,就只有上面一段布局,然而根本用不著神馬AndroidManifest.xml中給這個(gè)Activity設(shè)置
<activity android:name=".view.activity.multisend.MultiChatActivity" android:windowSoftInputMode="stateVisible|adjustResize"/>
對(duì)于這段代碼,是可以將底部如果有輸入框(最好用FrameLayout包裹),可以向上移動(dòng)的,類似于QQ輸入框??梢圆挥肧crollView而且輸入框向上滾動(dòng)時(shí),整個(gè)布局不會(huì)向上滾動(dòng)。
2,最后再提供一個(gè)思路,這個(gè)思路來(lái)自于“卷皮”,卷皮的登錄效果,他的設(shè)計(jì)思路是,在點(diǎn)擊EditText輸入框的時(shí)候,我第一個(gè)猜測(cè)是:得到了EditText輸入焦點(diǎn),或者是:猜測(cè)是監(jiān)聽到鍵盤彈出的焦點(diǎn)之后,卷皮頂上那個(gè)背景就把它慢慢變小隱藏起來(lái),導(dǎo)致下面的兩個(gè)輸入框滾動(dòng)到頂部去了,就方便用戶輸入了。這個(gè)思路也很好的解決用戶直接可以輸入的問(wèn)題。
3,目前很多項(xiàng)目要解決這個(gè)問(wèn)題的方法就是如上面2解決方案所示的,logo逐漸縮小,然后scroll會(huì)滾動(dòng)上去。
csdn這個(gè)編輯器越來(lái)越爛了,,圖片都搞不上來(lái)了
布局看看:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/root" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/white" android:clipToPadding="true" android:fitsSystemWindows="true" android:orientation="vertical"> <ImageView android:id="@+id/logo" android:layout_width="100dp" android:layout_height="100dp" android:layout_centerHorizontal="true" android:layout_gravity="center" android:layout_marginTop="80dp" android:background="@null" android:scaleType="centerCrop" android:src="@mipmap/ic_launcher"/> <ScrollView android:id="@+id/scrollView" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_alignParentTop="true" android:layout_marginLeft="15dp" android:layout_marginRight="15dp" android:fillViewport="true" android:scrollbarThumbVertical="@android:color/transparent" android:scrollbars="vertical"> <LinearLayout android:id="@+id/content" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="55dp" android:layout_marginTop="200dp" android:gravity="center_vertical" android:orientation="horizontal" android:paddingLeft="13dp"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="15dp" android:src="@drawable/ic_mobile_flag"/> <EditText android:id="@+id/et_mobile" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="@null" android:hint="請(qǐng)輸入用戶名" android:inputType="textVisiblePassword" android:maxLength="11" android:singleLine="true" android:text="" android:textColor="@color/_9" android:textColorHint="@color/_9" android:textSize="14dp"/> <ImageView android:id="@+id/iv_clean_phone" android:layout_width="40dp" android:layout_height="fill_parent" android:scaleType="centerInside" android:src="@drawable/ic_clear" android:visibility="gone"/> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="1px" android:background="@color/e"/> <LinearLayout android:layout_width="match_parent" android:layout_height="55dp" android:gravity="center_vertical" android:orientation="horizontal" android:paddingLeft="13dp"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="15dp" android:src="@drawable/ic_password_flag"/> <EditText android:id="@+id/et_password" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="@null" android:hint="請(qǐng)輸入密碼" android:inputType="textPassword" android:maxLength="30" android:singleLine="true" android:text="" android:textColor="@color/_9" android:textColorHint="@color/_9" android:textSize="14dp"/> <ImageView android:id="@+id/clean_password" android:layout_width="40dp" android:layout_height="fill_parent" android:scaleType="centerInside" android:src="@drawable/ic_clear" android:visibility="gone"/> <ImageView android:id="@+id/iv_show_pwd" android:layout_width="40dp" android:layout_height="fill_parent" android:scaleType="centerInside" android:src="@drawable/pass_gone"/> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="1px" android:background="@color/e"/> <Button android:id="@+id/btn_login" android:layout_width="match_parent" android:layout_height="45dp" android:layout_marginBottom="10dp" android:layout_marginTop="21dp" android:background="@drawable/bg_btn_login_selected" android:text="@string/login" android:textColor="@color/white" android:textSize="18dp"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/regist" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right" android:layout_marginBottom="10dp" android:layout_weight="1" android:text="注冊(cè)新用戶" android:textColor="#b0b8b2" android:textSize="14dp"/> <TextView android:id="@+id/forget_password" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right" android:layout_marginBottom="10dp" android:layout_marginLeft="21dp" android:text="@string/login_forget_pwd" android:textColor="#b0b8b2" android:textSize="14dp"/> </LinearLayout> </LinearLayout> </ScrollView> <LinearLayout android:id="@+id/service" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:gravity="center" android:orientation="horizontal" android:padding="10dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right" android:text="聯(lián)系客服" android:textColor="#b0b8b2" android:textSize="14dp"/> <View android:layout_width="1dp" android:layout_height="match_parent" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:background="@color/e"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right" android:text="關(guān)于我們" android:textColor="#b0b8b2" android:textSize="14dp"/> </LinearLayout> </RelativeLayout>
然后java代碼是,
private int screenHeight = 0;//屏幕高度 private int keyHeight = 0; //軟件盤彈起后所占高度 private float scale = 0.6f; //logo縮放比例 private int height = 0; private void initView() { screenHeight = this.getResources().getDisplayMetrics().heightPixels; //獲取屏幕高度 keyHeight = screenHeight / 3;//彈起高度為屏幕高度的1/3 } /** * 禁止鍵盤彈起的時(shí)候可以滾動(dòng) */ mScrollView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { return true; } }); mScrollView.addOnLayoutChangeListener(new ViewGroup.OnLayoutChangeListener() { @Override public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) { /* old是改變前的左上右下坐標(biāo)點(diǎn)值,沒(méi)有old的是改變后的左上右下坐標(biāo)點(diǎn)值 現(xiàn)在認(rèn)為只要控件將Activity向上推的高度超過(guò)了1/3屏幕高,就認(rèn)為軟鍵盤彈起*/ if (oldBottom != 0 && bottom != 0 && (oldBottom - bottom > keyHeight)) { Log.e("wenzhihao", "up------>" + (oldBottom - bottom)); int dist = mContent.getBottom() - bottom; if (dist > 0) { ObjectAnimator mAnimatorTranslateY = ObjectAnimator.ofFloat(mContent, "translationY", 0.0f, -dist); mAnimatorTranslateY.setDuration(300); mAnimatorTranslateY.setInterpolator(new LinearInterpolator()); mAnimatorTranslateY.start(); RxAnimationUtils.zoomIn(mLogo, 0.6f, dist); } } else if (oldBottom != 0 && bottom != 0 && (bottom - oldBottom > keyHeight)) { Log.e("wenzhihao", "down------>" + (bottom - oldBottom)); if ((mContent.getBottom() - oldBottom) > 0) { ObjectAnimator mAnimatorTranslateY = ObjectAnimator.ofFloat(mContent, "translationY", mContent.getTranslationY(), 0); mAnimatorTranslateY.setDuration(300); mAnimatorTranslateY.setInterpolator(new LinearInterpolator()); mAnimatorTranslateY.start(); //鍵盤收回后,logo恢復(fù)原來(lái)大小,位置同樣回到初始位置 RxAnimationUtils.zoomOut(mLogo, 0.6f); } } } }); mBtnLogin.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { RxKeyboardUtils.hideSoftInput(mContext); } }); /** * 縮小 * * @param view */ public static void zoomIn(final View view, float scale, float dist) { view.setPivotY(view.getHeight()); view.setPivotX(view.getWidth() / 2); AnimatorSet mAnimatorSet = new AnimatorSet(); ObjectAnimator mAnimatorScaleX = ObjectAnimator.ofFloat(view, "scaleX", 1.0f, scale); ObjectAnimator mAnimatorScaleY = ObjectAnimator.ofFloat(view, "scaleY", 1.0f, scale); ObjectAnimator mAnimatorTranslateY = ObjectAnimator.ofFloat(view, "translationY", 0.0f, -dist); mAnimatorSet.play(mAnimatorTranslateY).with(mAnimatorScaleX); mAnimatorSet.play(mAnimatorScaleX).with(mAnimatorScaleY); mAnimatorSet.setDuration(300); mAnimatorSet.start(); } /** * f放大 * * @param view */ public static void zoomOut(final View view, float scale) { view.setPivotY(view.getHeight()); view.setPivotX(view.getWidth() / 2); AnimatorSet mAnimatorSet = new AnimatorSet(); ObjectAnimator mAnimatorScaleX = ObjectAnimator.ofFloat(view, "scaleX", scale, 1.0f); ObjectAnimator mAnimatorScaleY = ObjectAnimator.ofFloat(view, "scaleY", scale, 1.0f); ObjectAnimator mAnimatorTranslateY = ObjectAnimator.ofFloat(view, "translationY", view.getTranslationY(), 0); mAnimatorSet.play(mAnimatorTranslateY).with(mAnimatorScaleX); mAnimatorSet.play(mAnimatorScaleX).with(mAnimatorScaleY); mAnimatorSet.setDuration(300); mAnimatorSet.start(); }
這段代碼大體就是這么實(shí)現(xiàn)的,動(dòng)態(tài)處理sroll向上滾動(dòng)問(wèn)題,logo動(dòng)態(tài)縮小即可解決
總結(jié)
以上所述是小編給大家介紹的Android實(shí)現(xiàn)輸入法彈出時(shí)把布局頂上去和登錄按鈕頂上去的解決方法,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- Android自定義View實(shí)現(xiàn)垂直時(shí)間軸布局
- android layout 按比例布局的代碼
- Android中RecyclerView布局代替GridView實(shí)現(xiàn)類似支付寶的界面
- android動(dòng)態(tài)加載布局文件示例
- android Activity相對(duì)布局的使用方法
- Android TabLayout(選項(xiàng)卡布局)簡(jiǎn)單用法實(shí)例分析
- Android布局——Preference自定義layout的方法
- Android應(yīng)用借助LinearLayout實(shí)現(xiàn)垂直水平居中布局
- android LinearLayout和RelativeLayout組合實(shí)現(xiàn)精確布局方法介紹
- Android實(shí)現(xiàn)Z軸布局效果
相關(guān)文章
android studio library 模塊中正確引用aar的實(shí)例講解
下面小編就為大家分享一篇android studio library 模塊中正確引用aar的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01Android activity和view判斷滑動(dòng)
這篇文章主要介紹了Android activity和view判斷滑動(dòng)的相關(guān)資料,需要的朋友可以參考下2017-06-06Android實(shí)現(xiàn)懸浮窗的簡(jiǎn)單方法實(shí)例
相信大家應(yīng)該也都發(fā)現(xiàn)了,現(xiàn)在很多應(yīng)用都使用到懸浮窗,例如微信在視頻的時(shí)候,點(diǎn)擊Home鍵,視頻小窗口仍然會(huì)在屏幕上顯示,下面這篇文章主要給大家介紹了關(guān)于Android實(shí)現(xiàn)懸浮窗的簡(jiǎn)單方法,需要的朋友可以參考下2021-09-09android 中 SQLiteOpenHelper的封裝使用詳解
這篇文章主要介紹了android 中 SQLiteOpenHelper的封裝使用詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-02-02詳解Android中Activity運(yùn)行時(shí)屏幕方向與顯示方式
本文主要對(duì)如何控制Android中Activity運(yùn)行時(shí)屏幕方向與顯示方式進(jìn)行詳細(xì)全面的實(shí)例講解。具有很好的參考價(jià)值,需要的朋友一起來(lái)看下吧2016-12-12Flutter?Ping檢查服務(wù)器通訊信號(hào)強(qiáng)度實(shí)現(xiàn)步驟
這篇文章主要為大家介紹了Flutter?Ping檢查服務(wù)器通訊信號(hào)強(qiáng)度實(shí)現(xiàn)步驟詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06Android中的sqlite查詢數(shù)據(jù)時(shí)去掉重復(fù)值的方法實(shí)例
今天小編就為大家分享一篇關(guān)于Android中的sqlite查詢數(shù)據(jù)時(shí)去掉重復(fù)值的方法實(shí)例,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-01-01