Android 讓文本輸入框默認不獲取焦點


項目中有個檢索功能,頁面上有個EditText輸入框,打開頁面后,焦點默認在EditText上,這樣的話軟鍵盤默認就會顯示出來,占據大半個屏幕。

后來想辦法將這個給去掉了,原先考慮着將焦點賦給頁面上的其他組件(頁面上還有時間選擇組件、按鈕組件等),方法如下:

  <EditText
        android:id="@+id/topical_content"
        android:layout_width="260dip"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_toLeftOf="@id/topical_image"
        android:hint="請輸入主題"
        android:nextFocusLeft="@+id/其它控件ID"
        android:nextFocusUp="@+id/其它控件ID"
        android:singleLine="true"
        android:textSize="12sp"/>

另一種方法是在EditText前面放置一個看不到的LinearLayout,讓它率先獲取焦點,代碼如下:

  <LinearLayout
        android:layout_width="0px"
        android:layout_height="0px"
        android:focusable="true"
        android:focusableInTouchMode="true" />

還有一種方法是在manifest中設置對activity的控制(此方法從網上找的,效果沒有試驗,不知道可不可以,列出來給大家參考下)

<activity ...  android:windowSoftInputMode="stateAlwaysHidden|adjustResize" />

本人項目中用的是第二種方法,即在EditText前面加了一個Linearlayout,讓它讓率先獲取焦點,實現了我想要的結果。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM