Android 背景可滑動登入介面 「 實現不壓縮背景彈出鍵盤 」
Android 背景可滑動登入介面
廢話不多說,先看下實現後的效果:
實現思路
看到上邊 gif 圖的效果,主要列舉一下實現過程過程中遇到的難點。
- 如何使鍵盤彈出時候不遮擋底部登入佈局;
- 當鍵盤彈出的時候如何不壓縮背景圖片或者背景延伸至「螢幕以外」;
從 「 windowSoftInputMode 」 說起
相信大家都清楚,Google 官方提供給開發者控制軟鍵盤顯示隱藏的方法不多,「windowSoftInputMode」算是我們可控制的軟鍵盤彈出模式的方法之一。關於其屬性的說明Google 官方和網上的教程說了很多,他的屬性值由兩部分組成,形如「 stateHidden|adjustResize 」的格式,其前半部分(事實上也可寫在後邊)表示所設定的 Activity 進入時軟鍵盤的狀態,後半部分表示軟鍵盤彈出的時候頁面是如何調整的。
下邊分別列出幾個可選屬性及其含義:
屬性 | 含義 |
---|---|
stateUnspecified | 軟體彈出狀態為系統根據配置的頁面主題選擇預設的設定,也就是說為預設屬性值 |
stateUnchanged | 軟鍵盤被保持無論它上次在該頁面的狀態,也就是說退出該頁面的時候是可見或隱藏,當主窗口出現在前面時仍然是值錢的狀態。 |
stateHidden | 首次建立進入該頁面的時候,軟鍵盤被隱藏。 |
stateAlwaysHidden | 無論什麼情況下進入該頁面軟鍵盤總是隱藏狀態。 |
stateVisible | 首次建立進入該頁面的時候,軟鍵盤顯示。 |
stateAlwaysVisible | 無論什麼情況下進入該頁面軟鍵盤總是顯示狀態。 |
adjustUnspecified | 頁面適應軟鍵盤彈起的時候預設屬性,它包括兩種情況,一種是頁面不可滾動的時候,他壓縮佈局以保證獲取焦點的輸入框可見,一種是頁面可滾動的時候,他將向上滾動佈局,直到輸入框可見。 |
adjustResize | 頁面總是調整螢幕的大小以便留出軟鍵盤的空間。這一屬性會重新繪製view |
adjustPan | 設定成adjustPan的時候鍵盤會去找當前介面的焦點,並始終焦點放在鍵盤的上方,使獲得焦點的view可見。當前視窗的內容將自動移動以便當前焦點從不被鍵盤覆蓋和使用者能總是看到輸入內容的部分 |
通過上述列表我們可以瞭解到 windowSoftInputMode 的幾個屬性值的含義。我們可以根據具體的需求來選擇合適屬性。However ! 產品需求永遠比屬性來的奇葩。比如說我們想要實現的的這個效果:
- 軟鍵盤彈出不遮擋全部的輸入佈局,並不是單純的留出一個輸入框控制元件
- 軟鍵盤被彈起的時候背景不能被壓縮,或者向上滑動
首先看第一個需求:我們可以使用 adjustResize 屬性來達到效果,可以看到這樣圖片已經被自動向上移動了,ok,如果效果您還算滿意,那我就沒什麼好說的了,但是我們老闆和產品以及 UI 說這樣不好,背景不能壓縮也就是我們說的第二個需求。當時我心中就有一種 mmp 想對他們說。但是呢作為一個敢於挑戰的 Android 程式設計師來說這個小小的需求並不算什麼。
對於第二個需求,首先我們要了解為什麼圖片會被上滑,是因為我們配置了 adjustResize 屬性,系統自動根據鍵盤所需要的空間向上移動整個頁面的佈局,並調整頁面佈局的大小以滿足不被軟鍵盤隱藏的效果。舉個栗子:
手機螢幕的高為1920px,那麼整個Activity的佈局高度也為1920px。當設定該屬性後點擊介面中的EditText,此時彈出軟鍵盤其高度為800px。為了完整地顯示此軟鍵盤,系統會調整Activity佈局的高度為1920px-800px=1120px。
注意這裡說了會調整佈局的大小,根據以往的經驗,系統自動調節的佈局都不是我們想要的結果,比如各種可滑動 View 巢狀的問題。那麼這個需求能否依據這個思路來結局呢?
當 windowSoftInputMode 被設定為 adjustResize 時候,當佈局調整的時候被調整的佈局均會重繪製,並走了
onMeasure,onSizeChanged,onLayout
。
當 windowSoftInputMode 被設定為 adjustPan 時候,當佈局調整的時候被調整的佈局均會重繪製,並走了onMeasure, onLayout
。這裡只需要注意 兩者都走了 onMeasure 方法,至於 adjustPan 沒走 onSizeChanged ,我們會在之後關於軟鍵盤彈出的監控的文章中詳細說明。
那麼我們就利用其走了 onMeasure 方法,來「阻止」系統自動調整的佈局大小。由於我們背景用了 ViewPager,所以我們需要重寫 ViewPager 的 OnMeasure 方法。
public class AutoViewPager extends ViewPager {
private int mScreenHeight;
public AutoViewPager(Context context) {
this(context,null);
}
public AutoViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
mScreenHeight = DensityUtil.getHeight(getContext());
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
heightMeasureSpec = MeasureSpec.makeMeasureSpec(mScreenHeight, MeasureSpec.EXACTLY);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
DensityUtil.getHeight 方法是獲取螢幕高度的方法。
public static int getHeight(Context context) {
DisplayMetrics dm = new DisplayMetrics();
WindowManager mWm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
mWm.getDefaultDisplay().getMetrics(dm);
int screenHeight = dm.heightPixels;
return screenHeight;
}
經過這樣的設定我們就講背景 ViewPager 的高度寫死為螢幕的高度。這樣當鍵盤彈出的時候ViewPager 的大小就會變了。 經過測試我們這個方法就就可以組織背景向上移動了。其實我們並沒有組織系統對控制元件的重繪,而是改變了最終重繪的 ViewPager 的高度大小,給使用者的感覺就是我的背景沒有改變。
最後附送實現的佈局程式碼:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/rl_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.goldenalpha.stock.master.views.AutoViewPager
android:id="@+id/login_bg_banner"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<LinearLayout
android:id="@+id/ll_dot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal">
<ImageView
android:id="@+id/iv_dot_1"
android:layout_width="7dp"
android:layout_height="7dp"
android:layout_marginRight="8dp"
android:background="@drawable/banner_dot_shape_select"/>
<ImageView
android:id="@+id/iv_dot_2"
android:layout_width="7dp"
android:layout_height="7dp"
android:layout_marginRight="8dp"
android:background="@drawable/bander_dot_shape_noselect"/>
<ImageView
android:id="@+id/iv_dot_3"
android:layout_width="7dp"
android:layout_height="7dp"
android:background="@drawable/bander_dot_shape_noselect"/>
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:id="@+id/activity_login"
android:layout_width="match_parent"
android:layout_height="270dp"
android:layout_alignParentBottom="true"
android:paddingBottom="@dimen/login_margin_bottom"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@drawable/login_shape"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:id="@+id/rl_phone_name"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv_area_code"
style="@style/Text.normal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="@dimen/login_tv_margin_left"
android:padding="5dp"
android:text="+86">
<requestFocus/>
</TextView>
<View
android:layout_width="0.3dp"
android:layout_height="10dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginLeft="@dimen/login_line_margin"
android:layout_toRightOf="@id/tv_area_code"
android:background="@color/gray"/>
<EditText
android:id="@+id/et_phone_num"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/login_et_margin_left"
android:background="@null"
android:hint="請輸入您的手機號碼"
android:inputType="phone"
android:maxLength="11"
android:maxLines="1"
android:paddingBottom="20dp"
android:paddingTop="20dp"
android:textColor="@color/black"
android:textColorHint="@color/gray"
android:textCursorDrawable="@null"
android:textSize="@dimen/font_normal">
<requestFocus/>
</EditText>
</RelativeLayout>
<View
android:id="@+id/line_phone_num"
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:layout_below="@+id/rl_phone_name"
android:layout_centerHorizontal="true"
android:layout_marginLeft="@dimen/login_line_margin"
android:layout_marginRight="@dimen/login_line_margin"
android:background="@color/gray"/>
<RelativeLayout
android:id="@+id/rl_check_num"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/line_phone_num"
android:layout_alignRight="@+id/line_phone_num"
android:layout_below="@+id/line_phone_num">
<EditText
android:id="@+id/et_check_num"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_toLeftOf="@+id/btn_get_check"
android:background="@null"
android:hint="請輸入驗證碼"
android:inputType="number"
android:maxLength="4"
android:maxLines="1"
android:paddingBottom="20dp"
android:paddingLeft="120dp"
android:paddingTop="20dp"
android:textColor="@color/black"
android:textColorHint="@color/gray"
android:textCursorDrawable="@null"
android:textSize="@dimen/font_normal"/>
<com.goldenalpha.stock.master.views.CountDownButton
android:id="@+id/btn_get_check"
android:layout_width="@dimen/login_btn_check_width"
android:layout_height="@dimen/login_btn_check_height"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginBottom="@dimen/login_btn_check_margin_bottom"
android:layout_marginTop="@dimen/login_btn_check_margin_top"
android:gravity="center"
android:text="獲取驗證碼"
android:textColor="@color/gray"
android:textSize="@dimen/font_normal"
app:defaultBackgroundResource="@drawable/btn_check_gray_shape"/>
</RelativeLayout>
<View
android:id="@+id/line_check_num"
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:layout_below="@+id/rl_check_num"
android:layout_centerHorizontal="true"
android:layout_marginLeft="25.3dp"
android:layout_marginRight="25.3dp"
android:background="@color/driver_color"/>
</RelativeLayout>
<com.goldenalpha.stock.master.views.LoadingButton
android:id="@+id/btn_phone_login"
android:layout_width="@dimen/login_btn_phone_width"
android:layout_height="@dimen/login_btn_phone_height"
android:layout_gravity="center_horizontal"
android:layout_marginTop="23dp"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="23dp">
<ImageView
android:id="@+id/tv_wx_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/wx_login_selector"/>
</FrameLayout>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
清單檔案中的配置
<activity
android:name=".activities.LoginActivity"
android:launchMode="singleTask"
android:screenOrientation="portrait"
android:theme="@style/AppTheme"
android:windowSoftInputMode="stateHidden|adjustResize">
</activity>
後來又讀者反映按照的方式鍵盤還是會將佈局頂上去:
相關推薦
Android 背景可滑動登入介面 「 實現不壓縮背景彈出鍵盤 」
Android 背景可滑動登入介面 廢話不多說,先看下實現後的效果: 實現思路 看到上邊 gif 圖的效果,主要列舉一下實現過程過程中遇到的難點。 如何使鍵盤彈出時候不遮擋底部登入佈局; 當鍵盤彈出的時候如何不壓縮背景
Java登入介面的實現(註冊、登入、背景圖片)
*/ * Copyright (c) 2016,煙臺大學計算機與控制工程學院 * All rights reserved. * 檔名:text.java * 作者:常軒 * 微信公眾號:Worldhello * 完成日期:2016年12月27日 * 版本號:V1
Android 記住密碼和自動登入介面的實現(SharedPreferences 的用法)
SharedPreferences介紹: SharedPreferences是Android平臺上一個輕量級的儲存類,主要是儲存一些常用的配置引數,它是採用xml檔案存放資料的,檔案存放在"/data/data<package name>/shared_pref
pyqt5 登入介面的實現模板(加強版)
說明 本例,在登入介面第一版的基礎上,增加了主介面的登出功能和退出功能。 登出功能 # 動作一:登出 def on_printAction1_triggered(self): self.close() dialog
linux啟動後出現桌面背景但無登入介面解決辦法
(1)同時按下Ctrl Alt F2 進入命令列介面。 (2)root登入進去。 (3)執行init 3 (啟動級別:init 0,1,2,3,4,5,6 0:停機 1:單使用者形式,只root進行維護 2:多使用者,不能使用net file syst
登入介面如何實現記住使用者名稱和密碼 且實現自動登入
最近在一次練習中遇到了一個問題,如何在登入介面實現記住使用者名稱和密碼,並且可以進行勾選是否自動登入。 首先,給大家看下我的登陸介面。 很顯然,我佈置了checkbox,一個用於記住密碼,一個用於自動登入。 <label> <input type
android 橫向可滑動的圖片
忘記在哪個大神哪裡淘來的程式碼,如有侵權請告知,謝謝。 直接將下面程式碼放入專案 /* * Copyright (C) 2014 The Android Open Source Project. * * [email protected]
Java登入介面的實現
1用Java實現一個登入介面需要哪些API類?大家先看一下QQ的登入介面 截圖中,我們可以看到,上面的主要部分有:登入按鈕,輸入框(包括密碼輸入框和賬號輸入框) 使用者頭像 等等所以,要實現一個簡單的登入介面,我們要用到的API類有: JFrame:窗體容器元件類
新增附件元件->登入介面簡單實現
建立好一個Android Activity以後,我們可以直接在.xml檔案中拖動元件來進行介面的設定。當然拖動的時候不能準確的佈局。所以我們還可以在layout中自己寫程式碼新增元件,設定位置大小等等。 一,首先來介紹一下Android的一些元件吧。 開啟.xm
Android一個簡易的登入介面
在學習了TextView,EditText,Button和Realtivelayout,linearlayout等常見佈局後就可以實現一個簡單的登入介面設計。至於像QQ登入介面和其他比較好看的登入介面,還需要進一步學習一下才能設計出來。 1.介面佈局程式碼 <Li
Android學習之servlet登入、註冊實現
public class HttpLogin { public static String LoginByPost(String id,String password){ String address = "http://www.khqust.top/MyWebsite/an
Android開發——彈性滑動的兩種實現方式
0. 前言我們在Android開發——View滑動的三種實現方式中學習瞭如何進行View滑動,在第一種方法,利用ScrollBy和ScrollTo進行滑動時,滑動效果是瞬間完成的,為了更好的使用者體驗,
Android-SeekBar可滑動進度條
目標效果: 程式執行顯示進度條在中間,手指滑動可更改進度條位置,並且顯示在TextView上,當點選進度條按鈕或者拖動時,按鈕變為紅色,擡起手指變為黑色。 1.activity_main.xml頁面設定控制元件。 activity_main.xml頁面: <
Android:可滑動展開的側邊欄在功能區中切換有EditText的佈局時自動展開的問題解決
問題描述: 軟體框架採用的類似QQ的方式,通過左右滑動可展開和隱藏側邊欄。 在其中一個佈局中,有一個按鈕可以切換下方的編輯佈局。 當點選某一個EditText,會彈出系統的輸入法。 點選返回鍵,輸入法會隱藏。 點選上方按鈕,切換佈局。
Jmeter 從資料庫查詢多個欄位,依次傳給登入介面怎麼實現?
問題背景: 博文“Jmeter 如何把資料庫的資料依次獲取作為引數傳入下一個請求?附栗子”某天有人留言如下: 看了下當時寫的文章,如果從資料庫查詢多個欄位,依次傳給登入介面,確實不能合理實現,所以,特意整理出一篇文章 一、新增jdbc請求並設定變數,獲取資料庫多個欄位值 1、新增好JDBC Connect
虛擬機器上LINUX開機輸入密碼後回車,閃下黑屏,又回到登入介面了,不能進系統
經過資料查閱,下面將修復方法總結如下: 因為/etc/profile這個檔案一但被錯誤設定就會導致系統不能登陸,所以最終的解決方案就是修改這個檔案,然後重啟系統。步驟如下: 1:在啟動介面(出現3.2.1....時)輸入:e 回車進入-》選擇介面再
[解決辦法]ubuntu登入介面一直進不去,因為修改profile--(ubuntu安裝node.js)
今天第一次接觸linux系統,操作了沒兩下就把系統搞壞了 本想在ubuntu系統下安裝一個全域性的node.js,重啟電腦後打不開圖形介面了,嚇出一身汗。。。。。下面總結一下安裝node.js的過程與出現的問題及解決辦法。 正確的安裝node.js過程 1.先去官網下載no
android app 讀取本地.db檔案,實現不變資料本地化(附小demo)
今天是正月十三,作為壽星的我依然奮鬥在工作中,醉了。-------------喧囂的成人世界,守住本真的心。 在專案中,有一部分資料,雖然是從介面拿取的,但是這些資料有個特點,就是一般不會再去改變了。對於這樣的資料來說,我是絕對不會每次用都去調介面,請求資料需要時間,需
Android 選擇日期時間對話方塊(可選擇開始結束時間,已解決彈出鍵盤問題)
直接上程式碼: MainActivity: package wkk.demo6; import android.app.AlertDialog; import android.app.DatePickerDialog; import android.a
Android setOnCreateContextMenuListener實現長按ListItem彈出不同選單
前一段時間做專案需要做一個效果:一個ListActivity,長按不同的item彈出的選單不一樣;如下圖所示我們長按不同的item需要彈出不同標題的menu: 長按事件可以使用ListView的setOnItemLongClick函式進行關聯,但是在響應的onItemLongClick函式裡面