當前位置:首頁 » 安卓系統 » android失去焦點

android失去焦點

發布時間: 2022-12-31 02:11:20

⑴ android 怎樣edittext 鍵盤失去焦點時自動關閉



android 怎樣edittext 鍵盤失去焦點時自動關閉

軟鍵盤的原理

軟鍵盤其實是一個Dialog。InputMethodService為我們的輸入法創建了一個Dialog,並且對某些參數進行了設置,使之能夠在底部或者全屏顯示。當我們點擊輸入框時,系統會對當前的主窗口進行調整,以便留出相應的空間來顯示該Dialog在底部,或者全屏。

2.活動主窗口調整

Android定義了一個屬性windowSoftInputMode,
用它可以讓程序控制活動主窗口調整的方式。我們可以在配置文件AndroidManifet.xml中對Activity進行設置。這個屬性的設置將會影響兩件事情:

1>軟鍵盤的狀態——隱藏或顯示。

2>活動的主窗口調整——是否減少活動主窗口大小以便騰出空間放軟鍵盤或是否當活動窗口的部分被軟鍵盤覆蓋時它的內容的當前焦點是可見的。

故該屬性的設置必須是下面列表中的一個值,或一個「state…」值加一個「adjust…」值的組合。在任一組設置多個值,各個值之間用|分開。

"stateUnspecified":The state of the soft keyboard (whether it is hidden or
visible) is not specified. The system will choose an appropriate state or rely
on the setting in the theme. This is the default setting for the behavior of the
soft keyboard. 軟鍵盤的狀態(隱藏或可見)沒有被指定。系統將選擇一個合適的狀態或依賴於主題的設置。這個是軟體盤行為的默認設置。

"stateUnchanged":The soft keyboard is kept in whatever state it was last
in, whether visible or hidden, when the activity comes to the fore.
軟鍵盤被保持上次的狀態。

"stateHidden":The soft keyboard is hidden when the user chooses the
activity — that is, when the user affirmatively navigates forward to the
activity, rather than backs into it because of leaving another activity.
當用戶選擇該Activity時,軟鍵盤被隱藏。

"stateAlwaysHidden":The soft keyboard is always hidden when the activity's
main window has input focus. 軟鍵盤總是被隱藏的。

"stateVisible":The soft keyboard is visible when that's normally
appropriate (when the user is navigating forward to the activity's main window).
軟鍵盤是可見的。

"stateAlwaysVisible":The soft keyboard is made visible when the user
chooses the activity — that is, when the user affirmatively navigates forward to
the activity, rather than backs into it because of leaving another activity.
當用戶選擇這個Activity時,軟鍵盤是可見的。

"adjustUnspecified":It is unspecified whether the activity's main window
resizes to make room for the soft keyboard, or whether the contents of the
window pan to make the currentfocus visible on-screen. The system will
automatically select one of these modes depending on whether the content of the
window has any layout views that can scroll their contents. If there is such a
view, the window will be resized, on the assumption that scrolling can make all
of the window's contents visible within a smaller area. This is the default
setting for the behavior of the main window.
它不被指定是否該Activity主窗口調整大小以便留出軟鍵盤的空間,或是否窗口上的內容得到屏幕上當前的焦點是可見的。系統將自動選擇這些模式中一種主要依賴於是否窗口的內容有任何布局視圖能夠滾動他們的內容。如果有這樣的一個視圖,這個窗口將調整大小,這樣的假設可以使滾動窗口的內容在一個較小的區域中可見的。這個是主窗口默認的行為設置。也就是說,系統自動決定是採用平移模式還是壓縮模式,決定因素在於內容是否可以滾動。

"adjustResize":(壓縮模式)The activity's main window is always resized to make
room for the soft keyboard on screen. 當軟鍵盤彈出時,要對主窗口調整屏幕的大小以便留出軟鍵盤的空間。

"adjustPan":(平移模式:當輸入框不會被遮擋時,該模式沒有對布局進行調整,然而當輸入框將要被遮擋時,窗口就會進行平移。也就是說,該模式始終是保持輸入框為可見。)The
activity's main window is not resized to make room for the soft keyboard.
Rather, the contents of the window are automatically panned so that the current
focus is never obscured by the keyboard and users can always see what they are
typing. This is generally less desirable than resizing, because the user may
need to close the soft keyboard to get at and interact with obscured parts of
the window.
該Activity主窗口並不調整屏幕的大小以便留出軟鍵盤的空間。相反,當前窗口的內容將自動移動以便當前焦點從不被鍵盤覆蓋和用戶能總是看到輸入內容的部分。這個通常是不期望比調整大小,因為用戶可能關閉軟鍵盤以便獲得與被覆蓋內容的交互操作。。

3.偵聽軟鍵盤的顯示隱藏

有時候,藉助系統本身的機制來實現主窗口的調整並非我們想要的結果,我們可能希望在軟鍵盤顯示隱藏的時候,手動的對布局進行修改,以便使軟鍵盤彈出時更加美觀。這時就需要對軟鍵盤的顯示隱藏進行偵聽。

我們可以藉助軟鍵盤顯示和隱藏時,對主窗口進行了重新布局這個特性來進行偵聽。如果我們設置的模式為壓縮模式,那麼我們可以對布局的onSizeChanged函數進行跟蹤,如果為平移模式,那麼該函數可能不會被調用。

4.EditText默認不彈出軟體鍵盤

方法一:

在AndroidMainfest.xml中選擇哪個activity,設置windowSoftInputMode屬性為adjustUnspecified|stateHidden

例如:

android:label="@string/app_name"

android:windowSoftInputMode="adjustUnspecified|stateHidden"

android:configChanges="orientation|keyboardHidden">

方法二:

讓EditText失去焦點,使用EditText的clearFocus方法

例如:EditText edit=(EditText)findViewById(R.id.edit);

edit.clearFocus();

方法三:

強制隱藏Android輸入法窗口

例如:EditText edit=(EditText)findViewById(R.id.edit);

InputMethodManager imm =
(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);

imm.hideSoftInputFromWindow(edit.getWindowToken(),0);

5.EditText始終不彈出軟體鍵盤

例:EditText edit=(EditText)findViewById(R.id.edit);

edit.setInputType(InputType.TYPE_NULL);

⑵ android 怎麼讓edittext失去焦點

1、在EditText的父布局中的布局文件中設置這兩行代碼
android:focusable="true"
android:focusableInTouchMode="true"
2、在代碼中設置EditText焦點通過setFocusable()和setFocusableInTouchMode() 不過這個好像不怎麼管用
3、如果你是不是想跳轉到有EditText的界面時自動彈出軟鍵盤的話 可以在功能清單文件(AndroidManifest.xml) 中的相對應activity節點中設置軟鍵盤彈出模式
android:windowSoftInputMode="stateAlwaysHidden|adjustPan"

⑶ android代碼如何 讓 edittext失去焦點

EditText初始化時候失去焦點:只需要在布局文件中設置屬性

java">android:focusable="false"

也可以在代碼中由開發者根據需求設置一定的條件,當條件滿足後,動態的設置EditText失去焦點。

示例代碼:

EditTextet=(EditText)findViewById(R.id.et);
et.clearFocus();
et.setFocusable(false);


這種控制EditText的操作是Android程序中不經常用到的。多出現於EditText的輸入監聽回調方法中。

⑷ 怎麼讓android 頁面失去焦點

在網上找了好久,有點監聽軟鍵盤事件,有點調用 clearFouse()方法,但是測試了都沒有!xml中也找不到相應的屬性可以關閉這個默認行為

1 解決之道:在EditText的父級控制項中找一個,設置成

Android:focusable="true"
android:focusableInTouchMode="true"

這樣,就把EditText默認的行為截斷了!

<LinearLayout
style="@style/FillWrapWidgetStyle"
android:orientation="vertical"
android:background="@color/black"
android:gravity="center_horizontal"

android:focusable="true"
android:focusableInTouchMode="true"
>
<ImageView
android:id="@+id/logo"
style="@style/WrapContentWidgetStyle"
android:background="@drawable/dream_dictionary_logo"
/>
<RelativeLayout
style="@style/FillWrapWidgetStyle"
android:background="@drawable/searchbar_bg"
android:gravity="center_vertical"
>
<EditText
android:id="@+id/searchEditText"
style="@style/WrapContentWidgetStyle"
android:background="@null"
android:hint="Search"
android:layout_marginLeft="40dp"
android:singleLine="true"
/>

</RelativeLayout>

</LinearLayout>


2 還有一個方法也可以非常簡單的實現這個功能:

EditText對象的clearFocus();

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editMsgView.getWindowToken(), 0);(關閉軟鍵盤。。。)

3更多問題解決辦法請參考android學習手冊,例子、源碼、文檔全部搞定,採用androidstudo的目錄結構,360手機助手中下載。下面是截圖。

⑸ android 輸入框失去焦點,怎樣讓鍵盤不自

1. 在activity layout xml文件中,EditText的前面定義:
<LinearLayout android:focusable="true" android:focusableInTouchMode="true" android:layout_width="0px" android:layout_height="0px" />

2. 在EditText中添加nextFocusUp與nextFocusLeft

<EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/editText" android:nextFocusUp="@id/editText" android:nextFocusLeft="@id/editText" />

⑹ Android中EditText獲得焦點後馬上又失去了,可能是什麼造成的呢

有以下幾種情況:

  1. EditText處在一個可以滾動的控制項中,例如ListView等,當得到焦點後軟體鍵盤會彈出,這個時候滾動控制項會重繪,因此會失去焦點。

  2. EditText注冊了焦點事件,得到焦點後將焦點轉向了其它控制項。

  3. EditText禁止了獲取焦點。

⑺ Android小知識點: 音頻焦點AudioFocus使用

背景:  之前雲閱讀570解決了 ,有聲書播放時候,簡訊中斷的問題。 前幾天採薇也有個類似的問題,  播放音頻中,收到干擾時候的處理。(在後台播放音頻的時候,這個時候另外的軟體也播放音頻、簡訊鈴聲  怎麼處理)

解決: 實現AudioManager.OnAudioFocusChangeListener介面實現onAudioFocusChange(int focusChange)方法

AUDIOFOCUS_GAIN:你已經獲得音頻焦點;

AUDIOFOCUS_LOSS:你已經失去音頻焦點很長時間了,必須終止所有的音頻播放。因為長時間的失去焦點後,不應該在期望有焦點返回,這是一個盡可能清除不用資源的好位置。例如,應該在此時釋放MediaPlayer對象;

AUDIOFOCUS_LOSS_TRANSIENT:這說明你臨時失去了音頻焦點,但是在不久就會再返回來。 此時,你必須終止所有的音頻播放,但是保留你的播放資源,因為可能不久就會返回來。

AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:這說明你已經臨時失去了音頻焦點,但允許你安靜的播放音頻(低音量) ,而不是完全的終止音頻播放。

   主要就是 對於3 與 4的處理。

⑻ android開發 如何讓Activity或者布局失去焦點,像彈出Dialog一樣。

你可以在整個布局文件中嵌套一個空的布局,讓這個空布局去獲取焦點,這樣你另一個布局中的控制項就不會獲取焦點了,這個我以前用過,肯定好用。

⑼ Android中EditText獲得焦點後馬上又失去了,可能是什麼造成的呢

中EditText獲得焦點後馬上又失去了有以下幾種情況:

  1. EditText處在一個可以滾動的控制項中,例如ListView等,當得到焦點後軟體鍵盤會彈出,這個時候滾動控制項會重繪,因此會失去焦點。

  2. EditText注冊了焦點事件,得到焦點後將焦點轉向了其它控制項。

  3. EditText禁止了獲取焦點。

⑽ android編程中,Activity默認的背景色是白色還是黑色啊

android編程中,Activity默認的背景色是白色,主要是因為其變成軟體默認的是白色。要想改變其背景顏色,只需在Background Color選項里進行更改即可。

當一個 Activity 實例被創建、銷毀或者啟動另外一個 Activity 時,它在這四種狀態之間進行轉換,這種轉換的發生依賴於用戶程序的動作。

(10)android失去焦點擴展閱讀

在android 中,Activity 擁有四種基本狀態:

1、Active/Running

一個新 Activity 啟動入棧後,它顯示在屏幕最前端,處理是處於棧的最頂端(Activity棧頂),此時它處於可見並可和用戶交互的激活狀態,叫做活動狀態或者運行狀態(active or running)。

2、Paused

當 Activity失去焦點, 被一個新的非全屏的Activity
或者一個透明的Activity
被放置在棧頂,此時的狀態叫做暫停狀態(Paused)。

此時它依然與窗口管理器保持連接,Activity依然保持活力(保持所有的狀態,成員信息,和窗口管理器保持連接),但是在系統內存極端低下的時候將被強行終止掉。所以它仍然可見,但已經失去了焦點故不可與用戶進行交互。

3、Stopped

如果一個Activity被另外的Activity完全覆蓋掉,叫做停止狀態(Stopped)。它依然保持所有狀態和成員信息,但是它不再可見,所以它的窗口被隱藏,當系統內存需要被用在其他地方的時候,Stopped的Activity將被強行終止掉。

4、Killed

如果一個Activity是Paused或者Stopped狀態,系統可以將該Activity從內存中刪除,Android系統採用兩種方式進行刪除,要麼要求該Activity結束,要麼直接終止它的進程。當該Activity再次顯示給用戶時,它必須重新開始和重置前面的狀態。

參考資料來源:網路—android編程

參考資料來源:網路—android開發

參考資料來源:網路—activity

熱點內容
scratch少兒編程課程 發布:2025-04-16 17:11:44 瀏覽:631
榮耀x10從哪裡設置密碼 發布:2025-04-16 17:11:43 瀏覽:360
java從入門到精通視頻 發布:2025-04-16 17:11:43 瀏覽:76
php微信介面教程 發布:2025-04-16 17:07:30 瀏覽:301
android實現陰影 發布:2025-04-16 16:50:08 瀏覽:789
粉筆直播課緩存 發布:2025-04-16 16:31:21 瀏覽:339
機頂盒都有什麼配置 發布:2025-04-16 16:24:37 瀏覽:204
編寫手游反編譯都需要學習什麼 發布:2025-04-16 16:19:36 瀏覽:804
proteus編譯文件位置 發布:2025-04-16 16:18:44 瀏覽:358
土壓縮的本質 發布:2025-04-16 16:13:21 瀏覽:584