EditText焦點控制,edittext焦點

來源:互聯網
上載者:User

EditText焦點控制,edittext焦點

1.EditText預設狀態是會擷取焦點,這使得某些情況下會妨礙開發人員實現的功能,例如開發搜尋功能,加入EditText後,預設情況下頁面顯示出來,EditText也獲得焦點了,但實際應用中使用者還沒有操作搜尋功能,要實現EditText預設情況下失去焦點,方法如下:

在EditText父容器中添加        

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

即可。例如:

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="@dimen/fragment_look_list_et_layout_height"
        android:layout_marginTop="1dip"
        android:background="#f0f0f0"
        android:focusable="true"
        android:focusableInTouchMode="true"

        android:gravity="center"
        android:paddingLeft="@dimen/fragment_look_list_et_padding"
        android:paddingRight="@dimen/fragment_look_list_et_padding" >


        <EditText
            android:id="@+id/et_search"
            android:layout_width="match_parent"
            android:layout_height="@dimen/fragment_look_list_et_height_width"
            android:layout_gravity="center"
            android:singleLine="true"
            android:ellipsize="start"
            android:background="@drawable/shape_look_list_et_bg"
            android:gravity="center_vertical"
            android:textSize="14sp"
            android:paddingRight="@dimen/fragment_look_list_et_padding_right"
            android:paddingLeft="@dimen/fragment_look_list_et_padding_left" />

</FrameLayout>

2.解決了預設情況事焦點的問題後,我們通常還希望用代碼動態控制住失去焦點(擷取焦點很簡單了,點擊就行了),例如搜尋方塊下面是一個ListVIew列表,當我們滑動列表時我們希望,EditText失去焦點,方法如下:

(1)首先在AndroidManiFest.xml的activity中加上android:windowSoftInputMode="stateHidden|adjustResize"

例如:

        <activity
            android:name=".activity.MainActivity"
            android:label="@string/app_name" 
            android:windowSoftInputMode="stateHidden|adjustResize"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

       (2)EditText失去焦點代碼:

                        //EditText失去焦點
searchET.clearFocus();



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.