當前位置:首頁 » 安卓系統 » android隱藏edittext

android隱藏edittext

發布時間: 2023-05-11 02:32:06

A. android開發中,EditText點擊事件可以響應,就是不彈出軟鍵盤。怎麼解決,EditText是在Dialog中使用的

當點擊EditText時:

1、存儲當前的輸入類型
2、設置輸入類型為InputType.TYPE_NULL,禁止手機軟鍵盤
3、還原原來的輸入類型,因為這次操作完,還要允許用戶修改。
4、彈出日期選擇對話框,操作完後將結果顯示到EditText上。

隱藏軟體盤,代碼如下:

EditText et_start_time = (EditText) this.findViewById(R.id.et_start_time);
int inType = et_start_time.getInputType();
et_start_time.setInputType(InputType.TYPE_NULL);
et_start_time.onTouchEvent(event);
et_start_time.setInputType(inType);
et_start_time.setSelection(et_start_time.getText().length());

B. 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);

C. android 後面代碼怎麼設置edittext的值

1、EditText輸入的文字為密碼形式的設置

(1)通過.xml里設置:

把該EditText設為:android:password="true" // 以」.」形式顯示文本

(2)在代碼里設置:

通過設置EditText的setTransformationMethod()方法來實現隱藏密碼或這顯示密碼。

editText.setTransformationMethod(PasswordTransformationMethod.getInstance());//設置密碼為不可見。

2、(1)EditText輸入的文字為電話號碼

Android:phoneNumber=」true」 //輸入電話號碼

3、EditText字數限制的設置

(1)在.xml中設置:android:maxLength=「50」

(2)代碼中設置:

editText.setFilters(new InputFilter[]{newInputFilter.LengthFilter(100)});

4、EditText設置字體

android:typeface="monospace" //設置字型。字形有:normal, sans, serif,monospace

5、EditText是否可編輯

Android:editable // 是否可編輯

6、在EditText中軟鍵盤的調起、關閉

(1)EditText有焦點(focusable為true)阻止輸入法彈出

editText=(EditText)findViewById(R.id.txtBody);

editText.setOnTouchListener(new OnTouchListener(){

public boolean onTouch(View v, MotionEvent event){

editText.setInputType(InputType.TYPE_NULL); //關閉軟鍵盤

return false;

}

});

(2)當EidtText無焦點(focusable=false)時阻止輸入法彈出

InputMethodManager imm =

(InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);

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

(3)調用數字鍵盤並設置輸入類型和鍵盤為英文

etNumber.setInputType(InputType.TYPE_CLASS_NUMBER); //調用數字鍵盤

rlEditText.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);//設置輸入類型和鍵盤為英文 或者:android:inputType="textUri|textMultiLine"

(4)android:focusable="false"//鍵盤永遠不會彈出

<activity android:name=".AddLinkman"android:windowSoftInputMode="adjustUnspecified|stateHidden"/>//不自動彈出鍵盤

//關閉鍵盤(比如輸入結束後執行) InputMethodManager imm =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(etEditText.getWindowToken(), 0);

//自動彈出鍵盤

((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE)).toggleSoftInput(0,InputMethodManager.HIDE_NOT_ALWAYS);

etEditText.requestFocus();//讓EditText獲得焦點,但是獲得焦點並不會自動彈出鍵盤

7、android:layout_gravity和android:gravity的區別

(1)android:layout_gravity是本元素對父元素的重力方向。

(2)android:gravity是本元素所有子元素的重力方向。

8、android:padding和android:layout_margin區別

這兩個都可以設置邊距,但有細微的區別:

(1)android:padding是相對父view的邊距

(2)android:layout_margin是相對同一級View的邊距

例:LinearLayout是水平布局,下面有兩個按鈕,

(a)如果右邊的按鈕想距左邊的按鈕15px,因為這兩個按鈕是同一級的,應該用android:layout_margin;

(b)如果右邊的按鈕想距左邊的距離為350px,應該用android:padding

9、android:numeric//只接受數字

android:numeric來控制輸入的數字類型,一共有三種分別為integer(正整數)、signed(帶符號整數,有正負)和decimal(浮點數)。

D. Android設置EditText默認取消焦點

在Android的布局文見中,只要存在EditText,當顯示此布局的時候,EditText就會默認獲取焦點並打開軟鍵盤。

在EditText的父控制項中,添加兩個屬性即可:

添加完成後的寫法如下所示:

經試驗,在EditText標簽中添加這兩行只會讓軟鍵盤隱藏,但是游標默認還是在編輯框中閃爍。
按照上文中的寫法可以默認隱藏游標。

E. android editText 輸入法把選項卡頂上去了怎麼辦

在清單文件AndroidManifest.xml中修改acitivty的windowInputMode為adjustPan即可

windowInputMode有以下幾個取值
1.stateUnspecified
中文意思是未指定狀態,當我們沒有設置android:windowSoftInputMode屬性的時候,軟體默認採用的就是這種交互方式,系統會根據界面採取相應的軟鍵盤的顯示模式,比如,當界面上只有文本和按鈕的時候,軟鍵盤就不會自動彈出,因為沒有輸入的必要。
2.stateUnchanged
中文的意思就是狀態不改變的意思,我們應該怎麼理解這句話呢?其實很好理解,就是說,當前界面的軟鍵盤狀態,取決於上一個界面的軟鍵盤狀態。假如當前界面鍵盤是隱藏的,那麼跳轉之後的界面,軟鍵盤也是隱藏的;如果當前界面是顯示的,那麼跳轉之後的界面,軟鍵盤也是顯示狀態。
3.stateHidden
顧名思義,如果我們設置了這個屬性,那麼鍵盤狀態一定是隱藏的,不管上個界面什麼狀態,也不管當前界面有沒有輸入的需求,反正就是不顯示。因此,我們可以設置這個屬性,來控制軟鍵盤不自動的彈出。
4.stateAlwaysHidden
這個屬性也可以讓軟鍵盤隱藏
5.stateVisible
設置為這個屬性,可以將軟鍵盤召喚出來,即使在界面上沒有輸入框的情況下也可以強制出來。
6.stateAlwaysVisible
這個屬性也是可以將鍵盤召喚出來,但是與stateVisible屬性有小小的不同之處。舉個例子,當我們設置為stateVisible屬性,如果當前的界面鍵盤是顯示的,當我們點擊按鈕跳轉到下個界面的時候,軟鍵盤會因為輸入框失去焦點而隱藏起來,當我們再次回到當前界面的時候,鍵盤這個時候是隱藏的。但是如果我們設置為stateAlwaysVisible,我們跳轉到下個界面,軟鍵盤還是隱藏的,但是當我們再次回來的時候,軟鍵盤是會顯示出來的。所以,這個Always就解釋了這個區別,不管什麼情況到達當前界面(正常跳轉或者是上一個界面被用戶返回),軟鍵盤都是顯示狀態。
7.adjustUnspecified
從這個屬性開始,就不是設置軟鍵盤的顯示與隱藏模式了,而是設置軟鍵盤與軟體的顯示內容之間的顯示關系。當沒有設置這個值的時候,這個選項也是默認的設置模式。在這中情況下,系統會根據界面選擇不同的模式。如果界面裡面有可以滾動的控制項,比如ScrowView,系統會減小可以滾動的界面的大小,從而保證即使軟鍵盤顯示出來了,也能夠看到所有的內容。如果布局裡面沒有滾動的控制項,那麼軟鍵盤可能就會蓋住一些內容,
8.adjustResize
這個屬性表示Activity的主窗口總是會被調整大小,從而保證軟鍵盤顯示空間。
9.adjustPan
如果設置為這個屬性,那麼Activity的屏幕大小並不會調整來保證軟鍵盤的空間,而是採取了另外一種策略,系統會通過布局的移動,來保證用戶要進行輸入的輸入框肯定在用戶的失業范圍裡面,從而讓用戶可以看到自己輸入的內容。

熱點內容
內置存儲卡可以拆嗎 發布:2025-05-18 04:16:35 瀏覽:336
編譯原理課時設置 發布:2025-05-18 04:13:28 瀏覽:379
linux中進入ip地址伺服器 發布:2025-05-18 04:11:21 瀏覽:613
java用什麼軟體寫 發布:2025-05-18 03:56:19 瀏覽:32
linux配置vim編譯c 發布:2025-05-18 03:55:07 瀏覽:107
砸百鬼腳本 發布:2025-05-18 03:53:34 瀏覽:945
安卓手機如何拍視頻和蘋果一樣 發布:2025-05-18 03:40:47 瀏覽:742
為什麼安卓手機連不上蘋果7熱點 發布:2025-05-18 03:40:13 瀏覽:803
網卡訪問 發布:2025-05-18 03:35:04 瀏覽:511
接收和發送伺服器地址 發布:2025-05-18 03:33:48 瀏覽:372