2011-09-29 36 views
30

我有一個編輯短信:禁用自動對焦上編輯文本

<LinearLayout android:id="@+id/linearLayout7" android:layout_width="match_parent" android:layout_height="wrap_content"> 
     <EditText android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_weight="1" android:id="@+id/editText1" android:text="3"> 
      <requestFocus></requestFocus> 
     </EditText> 
     <Button android:text="Button" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/button2"></Button> 
    </LinearLayout> 

,然後下面

我有這個問題的一些其他的東西是,當應用程序啓動權的重點是輸入,我不想在應用程序啓動時專注於輸入權。

我試圖消除

<requestFocus></requestFocus> 

的事情,它沒有做任何事情。

+0

集的Android窗口軟輸入模式:可調焦=「假」。它不會專注於您的EditText –

+1

,這將永遠不可用?現在即使我點擊文字欄也不會關注。我想要點擊時的焦點,只是不會自動當應用程序啓動 – Saad

+1

您可以先獲取按鈕的ID,並設置您的代碼中的按鈕焦點.... – Hanry

回答

88

在EditText的父佈局中添加android:focusable="true"android:focusableInTouchMode="true"元素,如下所示;

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/linearLayout7" android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:focusable="true" android:focusableInTouchMode="true"> 

我想,它應該對你有幫助。

+7

nope,它沒有做任何事情 – Saad

+1

@Saad:它的工作原理。我在測試它之前發佈它在這裏。將元素保留在XML文件的頂部。順便說一句,你正在開發的API級別(或Android版本)? – Mudassir

+1

不錯。你能解釋什麼android:focusable和android:focusableInTouchMode用於(我的意思是差異和用法)。 – Anoop

3

沒有更多的工作...只需將android:windowSoftInputMode =「stateAlwaysHidden」 添加到Manifest.xml文件的活動標記。

+0

請解釋這是什麼以及它如何幫助OP。沒有解釋的建議並沒有多大幫助,即使修復可行。 – SuperBiasedMan

+0

https://developer.android.com/guide/topics/manifest/activity-element.html#wsoft –

+0

請參閱此..上述鏈接..。 –

1
​​
+0

但是這也沒有幫助從編輯文本字段獲取焦點,它只是改變設備上的元素加載方式,儘管它起作用 – HSL

0

只需添加以下到主佈局視圖XML標籤:

android:focusableInTouchMode="true" 
0

Lee

有幾種方式做到這一點,Mudassir有給一個辦法你要做到這一點。

如果這個想法不工作,然後做只是簡單的東西─

在你AndroidManifest>Activity添加這個簡單的代碼:

android:windowSoftInputMode="stateHidden" 

沒有什麼需要的EditText做的XML,一切都會工作並且不會出現焦點。

看看現實生活中的例子代碼:

<activity 
     android:name=".MainActivity" 
     android:windowSoftInputMode="stateHidden" 
     android:label="@string/app_name" 
     > 

     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
</activity> 
0
<LinearLayout 
    android:focusable="true" 
    android:focusableInTouchMode="true" 
    android:layout_width="0px" 
    android:layout_height="0px" /> 

<EditText android:text="" 
    android:id="@+id/EditText01" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:hint="@string/hint"> 
</EditText> 

沒有其他的編碼需要,這是一個簡單而明確的解決方案。

2

如果你想清除默認專注於編輯文本,然後使用下面的兩個屬性

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

父線性佈局內。

例如:

<LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:focusable="false" 
      android:focusableInTouchMode="true" 
      android:orientation="vertical"> 


    <EditText 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:hint="email id" 
     android:inputType="textEmailAddress" 
     android:maxLines="1" 
    /> 

</LinearLayout> 

如果你想隱藏的活動創造鍵盤使用清單文件活動標籤 內這些屬性,例如:如果要隱藏鍵盤

<activity 
    android:configChanges="screenSize|orientation|keyboardHidden" 
    android:screenOrientation="portrait" 
    android:name=".activities.LoginActivity" 
    android:windowSoftInputMode="stateHidden|adjustResize"/> 

點擊按鈕或發生某些事件時使用以下代碼

public void onClick(View v) { 
       try { 
        //InputMethodManager is used to hide the virtual keyboard from the user after finishing the user input 
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
        if (imm.isAcceptingText()) { 
         imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0); 
        } 
       } catch (NullPointerException e) { 
        Log.e("Exception", e.getMessage() + ">>"); 
       } 
        } 
       } catch (NullPointerException e) { 
        Log.e("Exception", e.getMessage() + ">>"); 
       } 

如果你想脫下從編輯文本字段的焦點離開活動後

@Override 
    protected void onResume() { 
     super.onResume(); 
     mEmail.clearFocus(); 
     mPassword.clearFocus(); 
} 

最後如果你想清除表單提交使用

@Override 
    protected void onResume() { 
     super.onResume(); 
     mEmail.getText().clear(); 
     mPassword.getText().clear(); 
    } 
1
在編輯文本字段中的數據

您可以使用隱藏隱藏鍵盤和android:focusable="false"android:focusableInTouchMode="true"

<activity 
    android:name=".MainActivity" 
    android:windowSoftInputMode="stateHidden" 
    android:label="@string/app_name" 
    > 

    <intent-filter> 
     <action android:name="android.intent.action.MAIN" /> 
     <category android:name="android.intent.category.LAUNCHER" /> 
    </intent-filter> 
</activity>