2016-08-11 25 views
0

,我有以下結構的佈局(一些項目和屬性不再贅述):無法阻止的EditText從活動抓重點開始

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout android:orientation="vertical"> 

    <ScrollView android:orientation="vertical"> 

     <TableLayout android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 

      ... 

      <TableRow 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content"> 

       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="Label" android:textAppearance="?android:attr/textAppearanceMedium"/> 

       <RelativeLayout 
        android:descendantFocusability="beforeDescendants" 
        android:focusableInTouchMode="true" 
        android:layout_width="match_parent" 
        android:layout_weight="1" 
        android:layout_height="wrap_content"> 

        <Button 
         android:id="@+id/btnSaveStep" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_alignParentRight="true" 
         android:text="Save"/> 

        <EditText 
         android:id="@+id/etStep" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_toLeftOf="@id/btnSaveStep" 
         android:ems="10" 
         android:textAlignment="viewEnd" 
         android:inputType="numberDecimal" 
         android:selectAllOnFocus="true" 
         android:text="10"/> 

       </RelativeLayout> 

      </TableRow> 

      ... 

     </TableLayout> 
    </ScrollView> 

    <Button 
     android:id="@+id/btnProceed" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="right" 
     android:text="Proceed"/> 
</LinearLayout> 

注意RelativeLayoutandroid:descendantFocusability="beforeDescendants"android:focusableInTouchMode="true"屬性 - 他們沒有效果。我也試過this solution,我在佈局的許多地方放置了這個虛擬佈局 - 都無濟於事。

回答

1

將這個代碼清單檔案中的

<activity 
     android:name=".YourActivity" 
     android:label="@string/title_activity" 
     android:windowSoftInputMode="stateHidden" /> 
+0

這似乎所有的答案最乾淨的解決方案,要去嘗試它的明天。 –

+0

第二個想法是,鍵盤在啓動時不會發生。但EditText突出顯示(即專注)。 –

+1

editText.clearFocus() –

1

我有同樣的問題,並從字面上嘗試了一切。這到底是爲我工作的。

private class EditTextFocusListener implements View.OnFocusChangeListener { 

    @Override 
    public void onFocusChange(View v, boolean hasFocus) { 
     if (v.equals(inputEmail)) { 
      if (!hasFocus) hideKeyboard(inputEmail); 
     } else if (v.equals(inputPassword)) { 
      if (!hasFocus) hideKeyboard(inputPassword); 
     } 
    } 

    private void hideKeyboard(View v) { 
     InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE); 
     imm.hideSoftInputFromWindow(v.getWindowToken(), 0); 
    } 
} 

    EditTextFocusListener focusListener = new EditTextFocusListener(); 
    inputEmail.setOnFocusChangeListener(focusListener); 
    inputPassword.setOnFocusChangeListener(focusListener);