2017-05-08 30 views
-1

關於如何停止edittext獲取關注活動啓動的許多問題和解答。停止edittext在活動中使用滾動視圖獲得焦點 - android

但是,所有的解決方案都適用於我,edittext在啓動時不會獲得焦點,但是這會停止滾動視圖。

我嘗試了幾乎在帖子中的所有解決方案: Stop EditText from gaining focus at Activity startup 並且它不適用於活動中的滾動視圖。

這使我很麻煩。任何幫助都會很棒。

感謝

嘗試了以下內容:

  1. 虛擬項目,以防止AutoCompleteTextView從接收焦點
  2. 編輯文本的
  3. 套裝屬性:設置屬性機器人:可調焦= 「真」 和android:focusableInTouchMode = 「真」

這裏是我的xml:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/content_home" 


    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 

    tools:showIn="@layout/activity_home"> 


    <ScrollView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_alignParentTop="true" 
     android:fitsSystemWindows="false"> 


     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 


      <ProgressBar 
       android:id="@+id/progressBar" 
       android:layout_width="50dp" 
       android:layout_height="50dp" 
       android:layout_centerInParent="true" 
       android:visibility="gone" /> 


      <TextView 
       android:id="@+id/textViewLinkToOrder" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_centerHorizontal="true" 
       android:layout_marginBottom="20dp" 
       android:layout_marginTop="20dp" 
       android:gravity="center" 
       android:text="Click here to book travel insurance." 
       android:textColor="@color/colorPrimary" 
       android:textSize="24sp" 

       /> 

      <LinearLayout 
       android:id="@+id/buttons_linear_layout" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_below="@id/textViewLinkToOrder" 
       android:orientation="horizontal"> 

       <Button 
        android:id="@+id/choose_country_button" 
        android:layout_width="200dp" 
        android:layout_height="wrap_content" 
        android:layout_below="@id/textViewLinkToOrder" 
        android:layout_centerHorizontal="true" 
        android:layout_marginLeft="5dp" 
        android:layout_marginRight="5dp" 
        android:layout_weight="1" 
        android:background="@color/colorPrimary" 
        android:onClick="onChooseCountryButtonClick" 
        android:text="Choose country" 
        android:textColor="#ffff" /> 


       <Button 
        android:id="@+id/automatic_country_button" 
        android:layout_width="200dp" 
        android:layout_height="wrap_content" 
        android:layout_below="@id/choose_country_button" 
        android:layout_centerHorizontal="true" 
        android:layout_marginLeft="5dp" 
        android:layout_marginRight="5dp" 
        android:layout_weight="1" 
        android:background="@color/colorPrimary" 
        android:text="My Location" 
        android:textColor="#fff" /> 


      </LinearLayout> 


      <LinearLayout 
       android:id="@+id/linearLayoutAllDetails" 
       android:layout_width="match_parent" 

       android:layout_height="wrap_content" 
       android:layout_below="@id/buttons_linear_layout" 
       android:orientation="vertical"> 

       <TextView 
        android:id="@+id/textView_coumtry_name" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:gravity="center" 
        android:padding="5dp" 
        android:text="No country yet selected" 
        android:textColor="#000000" 
        android:textSize="30sp" 
        android:textStyle="bold" /> 

       <RelativeLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:background="#e8e8e8" 
        android:padding="10dp"> 

        <LinearLayout 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:orientation="horizontal"> 


         <LinearLayout 
          android:layout_width="0dp" 
          android:layout_height="wrap_content" 
          android:layout_weight="1" 
          android:orientation="horizontal"> 

          <EditText 
           android:id="@+id/editTextFromCurrency" 
           android:layout_width="80sp" 
           android:layout_height="50sp" 

           android:background="@drawable/edit_txt_bg" 
           android:ems="10" 
           android:inputType="number" 
           android:text="1" /> 

          <TextView 
           android:id="@+id/textViewFromCurrency" 
           android:layout_width="wrap_content" 
           android:layout_height="match_parent" 
           android:layout_marginLeft="10dp" 
           android:layout_marginStart="10dp" 
           android:gravity="center" 
           android:text="None" 
           android:textColor="#000000" 
           android:textSize="17sp" /> 

         </LinearLayout> 

         <LinearLayout 
          android:layout_width="0dp" 
          android:layout_height="wrap_content" 
          android:layout_weight="1" 
          android:orientation="horizontal"> 

          <EditText 
           android:id="@+id/editTextToCurrency" 
           android:layout_width="80sp" 
           android:layout_height="50sp" 
           android:background="@drawable/edit_txt_bg" 

           android:ems="10" 
           android:inputType="number" 
           android:text="1" /> 

          <TextView 
           android:id="@+id/textViewToCurrency" 
           android:layout_width="wrap_content" 
           android:layout_height="match_parent" 
           android:layout_marginLeft="10dp" 
           android:layout_marginStart="10dp" 
           android:gravity="center" 
           android:text="New Israeli Shekel" 
           android:textColor="#000000" 
           android:textSize="17sp" /> 
         </LinearLayout> 
        </LinearLayout> 
       </RelativeLayout> 


       <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:orientation="horizontal" 
        android:paddingBottom="20dp" 
        android:paddingLeft="10dp" 
        android:paddingRight="10dp" 
        android:paddingTop="20dp"> 

        <ImageView 
         android:layout_width="40dp" 
         android:layout_height="40dp" 
         android:src="@drawable/police" /> 

        <TextView 
         android:id="@+id/textView" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_gravity="center_vertical" 
         android:layout_marginLeft="10dp" 
         android:layout_marginRight="10dp" 
         android:text="Police" 
         android:textColor="#000000" 
         android:textSize="17sp" /> 

        <TextView 
         android:id="@+id/police_text_view" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_gravity="center_vertical" 
         android:layout_marginLeft="10dp" 
         android:layout_marginRight="10dp" 
         android:text="" 
         android:textColor="#000000" 
         android:textSize="17sp" /> 

        <View 
         android:layout_width="0dp" 
         android:layout_height="0dp" 
         android:layout_weight="1" /> 

        <ImageView 
         android:id="@+id/police_phone_button" 
         android:layout_width="30dp" 
         android:layout_height="30dp" 
         android:layout_gravity="center_vertical" 
         android:src="@android:drawable/sym_action_call" /> 

       </LinearLayout> 


       <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:orientation="horizontal" 
        android:paddingBottom="20dp" 
        android:paddingLeft="10dp" 
        android:paddingRight="10dp" 
        android:paddingTop="20dp"> 

        <ImageView 
         android:layout_width="40dp" 
         android:layout_height="40dp" 
         android:src="@drawable/ambulance" /> 

        <TextView 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_gravity="center_vertical" 
         android:layout_marginLeft="10dp" 
         android:layout_marginRight="10dp" 
         android:text="Ambulance" 
         android:textColor="#000000" 
         android:textSize="17sp" /> 

        <TextView 
         android:id="@+id/ambulance_text_view" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_gravity="center_vertical" 
         android:layout_marginLeft="10dp" 
         android:layout_marginRight="10dp" 
         android:text="" 
         android:textColor="#000000" 
         android:textSize="17sp" /> 

        <View 
         android:layout_width="0dp" 
         android:layout_height="0dp" 
         android:layout_weight="1" /> 

        <ImageView 
         android:id="@+id/ambulance_phone_button" 
         android:layout_width="30dp" 
         android:layout_height="30dp" 
         android:layout_gravity="center_vertical" 
         android:src="@android:drawable/sym_action_call" /> 

       </LinearLayout> 

       <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:orientation="horizontal" 
        android:paddingBottom="20dp" 
        android:paddingLeft="10dp" 
        android:paddingRight="10dp" 
        android:paddingTop="20dp"> 

        <ImageView 
         android:layout_width="40dp" 
         android:layout_height="40dp" 
         android:src="@drawable/israel_consulate" /> 

        <LinearLayout 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:orientation="vertical"> 

         <TextView 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:layout_gravity="center_vertical" 
          android:layout_marginLeft="10dp" 
          android:layout_marginRight="10dp" 
          android:text="Israel Consulate" 
          android:textColor="#000000" 
          android:textSize="17sp" /> 

         <TextView 
          android:id="@+id/israel_consulate_text_view" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:layout_gravity="center_vertical" 
          android:layout_marginLeft="10dp" 
          android:layout_marginRight="10dp" 
          android:text="" 
          android:textColor="#000000" 
          android:textSize="17sp" /> 
        </LinearLayout> 

        <View 
         android:layout_width="0dp" 
         android:layout_height="0dp" 
         android:layout_weight="1" /> 

        <ImageView 
         android:id="@+id/israel_phone_button" 
         android:layout_width="30dp" 
         android:layout_height="30dp" 
         android:layout_gravity="center_vertical" 
         android:src="@android:drawable/sym_action_call" 

         /> 
       </LinearLayout> 

       <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:orientation="horizontal" 
        android:paddingBottom="20dp" 
        android:paddingLeft="10dp" 
        android:paddingRight="10dp" 
        android:paddingTop="20dp"> 

        <ImageView 
         android:layout_width="40dp" 
         android:layout_height="40dp" 
         android:src="@drawable/chabad" /> 


        <LinearLayout 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:orientation="vertical"> 

         <TextView 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:layout_gravity="center_vertical" 
          android:layout_marginLeft="10dp" 
          android:layout_marginRight="10dp" 
          android:text="Chabad" 
          android:textColor="#000000" 
          android:textSize="17sp" /> 

         <TextView 
          android:id="@+id/chabad_number_text_view" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:layout_gravity="center_vertical" 
          android:layout_marginLeft="10dp" 
          android:layout_marginRight="10dp" 
          android:text="" 
          android:textColor="#000000" 
          android:textSize="17sp" /> 


         <TextView 
          android:id="@+id/chabad_address_text_view" 
          android:layout_width="150dp" 
          android:layout_height="wrap_content" 
          android:layout_gravity="center_vertical" 
          android:layout_marginLeft="10dp" 
          android:layout_marginRight="10dp" 
          android:text="" 
          android:textColor="#000000" 
          android:textSize="13sp" /> 

        </LinearLayout> 

        <View 
         android:layout_width="0dp" 
         android:layout_height="0dp" 
         android:layout_weight="1" /> 

        <ImageView 
         android:id="@+id/chabad_phone_button" 
         android:layout_width="30dp" 
         android:layout_height="30dp" 
         android:layout_gravity="center_vertical" 
         android:src="@android:drawable/sym_action_call" /> 

       </LinearLayout> 
      </LinearLayout> 
     </RelativeLayout> 
    </ScrollView> 
</RelativeLayout> 
+0

分享你的佈局和代碼你做了哪些停止從滾動滾動視圖 –

+0

你試過這個http://stackoverflow.com/questions/8100831/stop-scrollview-from-setting-focus-on-edittext? – yasin

+0

嘗試在框架佈局中嵌套您的滾動視圖。 –

回答

0

獲得所需結果的一種替代方法是隱藏設備的虛擬鍵盤。這將禁止EditText獲得焦點,從而不滾動屏幕。您可以使用此功能實現這一點:

public static void hideKeyboard(Activity activity) { 
    InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE); 
    //Find the currently focused view, so we can grab the correct window token from it. 
    View view = activity.getCurrentFocus(); 
    //If no view currently has focus, create a new one, just so we can grab a window token from it 
    if (view == null) { 
     view = new View(activity); 
    } 
    imm.hideSoftInputFromWindow(view.getWindowToken(), 0); 
} 

調用是這樣的:hideKeyboard(this);,優先您的onCreate()方法中。

如果用戶點擊EditText,則會再次顯示虛擬鍵盤。

+0

這並沒有隱藏鍵盤 – RJB

+0

也許這是因爲你有兩個EditText。嘗試排除一個僅用於測試的原因。這是防彈的,相信我。你可以實現一個函數,一旦使用第一個EditText,你將顯示第二個(非常簡單:從xml attribs開始第二個「隱藏」,然後是「setVisibility」)。 – statosdotcom

+0

用一個編輯文本進行測試,並沒有工作 – RJB

相關問題