2015年9月30
2019獨角獸企業(yè)重金招聘Python工程師標準>>>
在Android UI中,我們常常會使用EditText,當用戶點擊這個EditText時會觸發(fā)軟鍵盤,這個軟鍵盤會把EditText以下的界面擋住,有時候我們希望用戶看到完整的界面。
將布局的最外層添加一個ScrollView,當用戶點擊EditText時,將ScrollView滾動到底。(當你想讓一個高度值不足scrollview的子控件fillparent的時候,單獨的定義android:layout_height="fill_parent"是不起作用的,必須加上fillviewport屬性,當子控件的高度值大于scrollview的高度時,這個標簽就沒有任何意義了。)
想要實現(xiàn)效果我們還需要在清單文件的activity中設(shè)置windowSoftInputMode
<activity ?
? ? ? ? ? ? android:label="@string/app_name" ?
? ? ? ? ? ? android:name=".InputMethodActivity" ?
? ? ? ? ? ? ??
? ? ? ? ? ? <!--只有用戶點擊了編輯框才顯示軟鍵盤,并且會導(dǎo)致原有界面重新布局--> ?
? ? ? ? ? ? android:windowSoftInputMode="stateHidden|adjustResize" > ?
activity_layout的xml代碼:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
? ? ? ? ? ? xmlns:tools="http://schemas.android.com/tools"
? ? ? ? ? ? android:layout_width="match_parent"
? ? ? ? ? ? android:layout_height="match_parent"
? ? ? ? ? ? android:fillViewport="true"
? ? ? ? ? ? tools:context="com.example.mydemo.RegisterActivity">
? <LinearLayout
? ? ? android:layout_width="match_parent"
? ? ? android:layout_height="match_parent"
? ? ? android:gravity="center_vertical"
? ? ? android:orientation="vertical"
? ? ? android:paddingBottom="@dimen/activity_vertical_margin"
? ? ? android:paddingLeft="@dimen/activity_horizontal_margin"
? ? ? android:paddingRight="@dimen/activity_horizontal_margin"
? ? ? android:paddingTop="@dimen/activity_vertical_margin">
? ? <LinearLayout
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:orientation="horizontal">
? ? ? <TextView
? ? ? ? ? android:layout_width="100dp"
? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? android:gravity="center_horizontal"
? ? ? ? ? android:text="@string/tv_username"
? ? ? ? ? android:textSize="20sp"/>
? ? ? <EditText
? ? ? ? ? android:id="@+id/etUsername"
? ? ? ? ? android:layout_width="match_parent"
? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? android:textSize="20sp"/>
? ? </LinearLayout>
? ? <LinearLayout
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:orientation="horizontal">
? ? ? <TextView
? ? ? ? ? android:layout_width="100dp"
? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? android:gravity="center_horizontal"
? ? ? ? ? android:text="@string/tv_password"
? ? ? ? ? android:textSize="20sp"/>
? ? ? <EditText
? ? ? ? ? android:id="@+id/etPassword"
? ? ? ? ? android:layout_width="match_parent"
? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? android:inputType="textPassword"
? ? ? ? ? android:textSize="20sp"/>
? ? </LinearLayout>
? ? <LinearLayout
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:orientation="horizontal">
? ? ? <TextView
? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? android:gravity="center_horizontal"
? ? ? ? ? android:text="@string/tv_rePassword"
? ? ? ? ? android:textSize="20sp"/>
? ? ? <EditText
? ? ? ? ? android:id="@+id/etRePassword"
? ? ? ? ? android:layout_width="match_parent"
? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? android:inputType="textPassword"
? ? ? ? ? android:textSize="20sp"/>
? ? </LinearLayout>
? ? <Button
? ? ? ? android:layout_width="wrap_content"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:layout_gravity="center_horizontal"
? ? ? ? android:onClick="submit_register"
? ? ? ? android:text="@string/btn_register"
? ? ? ? />
? </LinearLayout>
</ScrollView>
在布局頁面中,我們在最外層使用了ScrollView,由于軟鍵盤的彈出,原有界面從新布局,使得用戶可以滾動界面。
android:windowSoftInputMode
activity主窗口與軟鍵盤的交互模式,可以用來避免輸入法面板遮擋問題,Android1.5后的一個新特性。
這個屬性能影響兩件事情:
【一】當有焦點產(chǎn)生時,軟鍵盤是隱藏還是顯示
【二】是否減少活動主窗口大小以便騰出空間放軟鍵盤
它的設(shè)置必須是下面列表中的一個值,或一個”state…”值加一個”adjust…”值的組合。在任一組設(shè)置多個值——多個”state…”values,例如&mdash有未定義的結(jié)果。各個值之間用|分開。例如:<activity android:windowSoftInputMode="stateVisible|adjustResize". . . >
在這設(shè)置的值(除"stateUnspecified"和"adjustUnspecified"以外)將覆蓋在主題中設(shè)置的值
各值的含義:
【A】stateUnspecified:軟鍵盤的狀態(tài)并沒有指定,系統(tǒng)將選擇一個合適的狀態(tài)或依賴于主題的設(shè)置
【B】stateUnchanged:當這個activity出現(xiàn)時,軟鍵盤將一直保持在上一個activity里的狀態(tài),無論是隱藏還是顯示
【C】stateHidden:用戶選擇activity時,軟鍵盤總是被隱藏
【D】stateAlwaysHidden:當該Activity主窗口獲取焦點時,軟鍵盤也總是被隱藏的
【E】stateVisible:軟鍵盤通常是可見的
【F】stateAlwaysVisible:用戶選擇activity時,軟鍵盤總是顯示的狀態(tài)
【G】adjustUnspecified:默認設(shè)置,通常由系統(tǒng)自行決定是隱藏還是顯示
【H】adjustResize:該Activity總是調(diào)整屏幕的大小以便留出軟鍵盤的空間
【I】adjustPan:當前窗口的內(nèi)容將自動移動以便當前焦點從不被鍵盤覆蓋和用戶能總是看到輸入內(nèi)容的部分
下文轉(zhuǎn)自?http://www.android100.org/html/201502/21/122225.html?(其中的\為圖片,我偷懶,沒拿過來了)
stateUnspecified,stateUnchanged,stateHidden,stateAlwaysHidden,stateVisible,stateAlwaysVisible,adjustUnspecified,adjustResize,adjustPan。
? ? 我們設(shè)置屬性的時候,可以在這9個值里面選擇一個,也可以用"state...|adjust"的形式進行設(shè)置。那么,這些取值到底是怎么影響到軟鍵盤與窗口之間的交互的呢?下面,我們就一個個的測試這9個取值,到底是如何影響軟鍵盤的顯示的。
1.stateUnspecified
? ? 中文意思是未指定狀態(tài),當我們沒有設(shè)置android:windowSoftInputMode屬性的時候,軟件默認采用的就是這種交互方式,系統(tǒng)會根據(jù)界面采取相應(yīng)的軟鍵盤的顯示模式,比如,當界面上只有文本和按鈕的時候,軟鍵盤就不會自動彈出,因為沒有輸入的必要。那么,當界面上出現(xiàn)了獲取了焦點的輸入框的時候,軟鍵盤會不會自動的彈出呢?這個還真不一定!比如,在下面的這個界面布局中,軟鍵盤并不會自動彈出。
\
?就是說,默認的,在這種界面情況下,系統(tǒng)并不確定用戶是否需要軟鍵盤,因此不會自動彈出。但是,為什么說不一定呢?這是因為,如果我們在這個布局的外面,包裹上一個
ScrollView,軟鍵盤就會自動的彈出來了!
如下,在這種布局文件下,軟鍵盤會自動的彈出
代碼如下:
? <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
? ? xmlns:tools="http://schemas.android.com/tools"
? ? android:layout_width="match_parent"
? ? android:layout_height="match_parent"
? ? android:orientation="vertical" >
?
? ? <ScrollView
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="match_parent" >
?
? ? ? ? <LinearLayout
? ? ? ? ? ? android:layout_width="match_parent"
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? android:orientation="vertical" >
?
? ? ? ? ? ? <Button
? ? ? ? ? ? ? ? android:layout_width="match_parent"
? ? ? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? ? ? android:onClick="toOther"
? ? ? ? ? ? ? ? android:text="跳轉(zhuǎn)" />
?
? ? ? ? ? ? <EditText
? ? ? ? ? ? ? ? android:layout_width="match_parent"
? ? ? ? ? ? ? ? android:layout_height="wrap_content" />
? ? ? ? </LinearLayout>
? ? </ScrollView>
?
</LinearLayout>
? ?這確實是一個很奇怪的判斷方式。因此,我們可以得出結(jié)論,當設(shè)置屬性為stateUnspecified的時候,系統(tǒng)是默認不彈出軟鍵盤的,但是當有獲得焦點的輸入框的界面有滾動的需求的時候,會自動彈出軟鍵盤。至于為什么非要強調(diào)要獲取焦點的輸入框,這是因為,如果不是輸入框獲取焦點,軟鍵盤也是不會自動彈出的,讓界面不自動彈出軟鍵盤的其中一個解決方案,就是在xml文件中,設(shè)置一個非輸入框控件獲取焦點,從而阻止鍵盤彈出。
? ? 2.stateUnchanged
? ? 中文的意思就是狀態(tài)不改變的意思,我們應(yīng)該怎么理解這句話呢?其實很好理解,就是說,當前界面的軟鍵盤狀態(tài),取決于上一個界面的軟鍵盤狀態(tài)。舉個例子,假如當前界面鍵盤是隱藏的,那么跳轉(zhuǎn)之后的界面,軟鍵盤也是隱藏的;如果當前界面是顯示的,那么跳轉(zhuǎn)之后的界面,軟鍵盤也是顯示狀態(tài)。
? ? 3.stateHidden
? ? 顧名思義,如果我們設(shè)置了這個屬性,那么鍵盤狀態(tài)一定是隱藏的,不管上個界面什么狀態(tài),也不管當前界面有沒有輸入的需求,反正就是不顯示。因此,我們可以設(shè)置這個屬性,來控制軟鍵盤不自動的彈出。
? ? 4.stateAlwaysHidden
? ? 這個屬性也可以讓軟鍵盤隱藏,但是我暫時還不知道和stateHidden屬性的區(qū)別,本來想去stackOverFlow上問一下的,但是,Great Wall,呵呵呵...祝愿病魔早日戰(zhàn)勝方校長
? ? 5.stateVisible
? ? 設(shè)置為這個屬性,可以將軟鍵盤召喚出來,即使在界面上沒有輸入框的情況下也可以強制召喚出來。
? ? 6.stateAlwaysVisible
? ? 這個屬性也是可以將鍵盤召喚出來,但是與stateVisible屬性有小小的不同之處。舉個例子,當我們設(shè)置為stateVisible屬性,如果當前的界面鍵盤是顯示的,當我們點擊按鈕跳轉(zhuǎn)到下個界面的時候,軟鍵盤會因為輸入框失去焦點而隱藏起來,當我們再次回到當前界面的時候,鍵盤這個時候是隱藏的。但是如果我們設(shè)置為stateAlwaysVisible,我們跳轉(zhuǎn)到下個界面,軟鍵盤還是隱藏的,但是當我們再次回來的時候,軟鍵盤是會顯示出來的。所以,這個Always就解釋了這個區(qū)別,不管什么情況到達當前界面(正常跳轉(zhuǎn)或者是上一個界面被用戶返回),軟鍵盤都是顯示狀態(tài)。
? ? 說到這里,我聯(lián)想到了上面的stateHidden和stateAlwaysHidden,我估計區(qū)別也是這樣的,就是說,stateAlwaysHidden無論如何都是隱藏的,但是如果在跳轉(zhuǎn)到下個界面的時候,軟鍵盤被召喚出來了,那么當下個界面被用戶返回的時候,鍵盤應(yīng)該是不會被隱藏的,但是,我還沒有找到能夠跳轉(zhuǎn)到下個界面,還讓當前界面軟鍵盤不消失的方法,所以暫時不能驗證。
? ? 7.adjustUnspecified
? ? 從這個屬性開始,就不是設(shè)置軟鍵盤的顯示與隱藏模式了,而是設(shè)置軟鍵盤與軟件的顯示內(nèi)容之間的顯示關(guān)系。當你跟我們沒有設(shè)置這個值的時候,這個選項也是默認的設(shè)置模式。在這中情況下,系統(tǒng)會根據(jù)界面選擇不同的模式。如果界面里面有可以滾動的控件,比如ScrowView,系統(tǒng)會減小可以滾動的界面的大小,從而保證即使軟鍵盤顯示出來了,也能夠看到所有的內(nèi)容。如果布局里面沒有滾動的控件,那么軟鍵盤可能就會蓋住一些內(nèi)容,我們從下面的圖中可以看出差別。
? ?沒有滾動控件,軟鍵盤下面的布局都被遮擋住了,若想修改,只能隱藏軟鍵盤,然后選擇。而且,重點注意一下上面的布局,當我們選擇的輸入框偏下的時候,上面的標題欄和布局被軟鍵盤頂上去了。記住這個特征,因為后面有個屬性和這個的效果不一樣。
\
布局里面有滑動控件,系統(tǒng)會自動的縮小整個界面的大小,因此,我們可以軟鍵盤上面的小區(qū)域中顯示所有的輸入框。
\?
? ? 這就是兩中顯示模式之間的差別。
? ? 8.adjustResize
? ? 這個屬性表示Activity的主窗口總是會被調(diào)整大小,從而保證軟鍵盤顯示空間。
? ? 我們先看顯示效果。
? ? 注意觀察這個上面的標題欄和按鈕,設(shè)置為adjustResize屬性之后,對于沒有滑動控件的布局,雖然還是不能選擇所有的輸入框,但是,窗口的顯示方式發(fā)生了變化,默認屬性時,整個布局是被頂上去了,但是設(shè)置為adjustResize屬性,布局的位置并沒有發(fā)生什么變化,這就是最大的區(qū)別。
\
而對于有滑動控件的布局來說,顯示效果和默認是一樣的。
\
? ? 9.adjustPan
? ? 如果設(shè)置為這個屬性,那么Activity的屏幕大小并不會調(diào)整來保證軟鍵盤的空間,而是采取了另外一種策略,系統(tǒng)會通過布局的移動,來保證用戶要進行輸入的輸入框肯定在用戶的失業(yè)范圍里面,從而讓用戶可以看到自己輸入的內(nèi)容。對于沒有滾動控件的布局來說,這個其實就是默認的設(shè)置,如果我們選擇的位置偏下,上面的標題欄和部分控件會被頂上去。但是對于有滾動控件的布局來說,則不太一樣,我們看下面的效果圖。
? ? 首先,這是軟鍵盤沒有彈出的時候,有滾動控件的顯示范圍,最下面顯示的是9.
\
? ? 當我們點擊5這個輸入框,我們會發(fā)現(xiàn)下面的現(xiàn)象。
? ? 最上面只能夠顯示到按鈕,標題欄已經(jīng)不能看到了。
\
? ? 而最下面也只能滑動到8,下面的內(nèi)容也不能夠滑動了。
\
? ?因此,我們就能夠理解這個屬性的作用了。
? ? 通過以上的實驗,我們可以得出結(jié)論,如果我們不設(shè)置"adjust..."的屬性,對于沒有滾動控件的布局來說,采用的是adjustPan方式,而對于有滾動控件的布局,則是采用的adjustResize方式。
Android中RelativeLayout各個屬性?
android:layout_above="@id/xxx" ?--將控件置于給定ID控件之上
android:layout_below="@id/xxx" ?--將控件置于給定ID控件之下
android:layout_toLeftOf="@id/xxx" ?--將控件的右邊緣和給定ID控件的左邊緣對齊
android:layout_toRightOf="@id/xxx" ?--將控件的左邊緣和給定ID控件的右邊緣對齊
android:layout_alignLeft="@id/xxx" ?--將控件的左邊緣和給定ID控件的左邊緣對齊
android:layout_alignTop="@id/xxx" ?--將控件的上邊緣和給定ID控件的上邊緣對齊
android:layout_alignRight="@id/xxx" ?--將控件的右邊緣和給定ID控件的右邊緣對齊
android:layout_alignBottom="@id/xxx" ?--將控件的底邊緣和給定ID控件的底邊緣對齊
android:layout_alignParentLeft="true" ?--將控件的左邊緣和父控件的左邊緣對齊
android:layout_alignParentTop="true" ?--將控件的上邊緣和父控件的上邊緣對齊
android:layout_alignParentRight="true" ?--將控件的右邊緣和父控件的右邊緣對齊
android:layout_alignParentBottom="true" --將控件的底邊緣和父控件的底邊緣對齊
android:layout_centerInParent="true" ?--將控件置于說父控件的中心位置
android:layout_centerHorizontal="true" ?--將控件置于水平方向的中心位置
android:layout_centerVertical="true" ?--將控件置于垂直方向的中心位置
其中很重要的android:layout_alignParentLeft="true" ?--將控件的左邊緣和父控件的左邊緣對齊有時候找不到知道為啥?
那是因為你的父控件不是相對布局,當你的父控件是相對布局,子控件才能顯示這個屬性。
轉(zhuǎn)載于:https://my.oschina.net/u/2473169/blog/512790
總結(jié)
- 上一篇: mysql集群之keepalived简单
- 下一篇: 【分享】iOS功能界面漂亮的弹出框